import java.util.Scanner;
import java.util.Stack;
public class StkQueue {
private Stack<Integer> head = new Stack<>();
private Stack<Integer> tail = new Stack<>();
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
StkQueue queue = new StkQueue();
int N = sc.nextInt();
for(int i=0;i<N;i++)
{
int c = sc.nextInt();
switch(c){
case 1:
queue.enqueue(sc.nextInt());
break;
case 2:
queue.dequeue();
break;
case 3 :
System.out.println(queue.peep());
break;
}
}
}
public void enqueue(int a){
tail.push(a);
}
public int dequeue()
{
//tail = new Stack<>();
if(!head.isEmpty())
return head.pop();
else{
while(!tail.isEmpty()){
head.push(tail.pop());
}
}
int val = head.pop();
return val;
}
public int peep(){
if(!head.isEmpty())
return head.peek();
else{
while(!tail.isEmpty()){
head.push(tail.pop());
}
}
int val = head.peek();
return val;
}
}