#include<stdio.h>
int i, j, k, n, m;
int queue[101] = {};
int rear = -1, front = -1;
// front가 input, rear가 output
void push(int data)
{
front ++;
queue[front] = data;
}
int f()
{
if (front != -1)
{
return queue[front];
}
else
{
return -1;
}
}
int back()
{
if (rear != -1)
{
return queue[rear];
}
else
{
return -1;
}
}
void pop()
{
}
int main()
{
int i, j, k, n, m;
int queue[101] = {};
int rear = -1, front = -1;
char str[11] = {};
scanf("%d", &n);
for (i = 1 ; i <= n ; i ++)
{
gets(str);
if (str[1] == 'p' && str[2] == 'u')
{
int x = str[7] - '0';
push(x);
}
else if (str[1] == 'f')
{
printf("%d\n", f());
}
else if (str[1] == 'b')
{
printf("%d\n", back());
}
else if (str[2] == 'o')
{
pop();
}
}
return 0;
}