import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
Scanner kb = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int N = kb.nextInt();
for(int i=0; i<N; i++) {
int order = kb.nextInt();
switch(order) {
case 1:
stack.push(kb.nextInt());
break;
case 2:
if(stack.isEmpty()) {
sb.append(-1).append('\n');
}else {
sb.append(stack.pop()).append('\n');
}
break;
case 3:
sb.append(stack.size()).append('\n');
break;
case 4:
if(stack.isEmpty()) {
sb.append(1).append('\n');
}else {
sb.append(0).append('\n');
}
break;
case 5:
if(stack.isEmpty()) {
sb.append(-1).append('\n');
}else {
sb.append(stack.peek()).append('\n');
}
break;
}
}
System.out.println(sb.toString());
}
}