import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
Deque<Integer> s = new LinkedList<>();
String[] ins;
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
ins = br.readLine().split(" ");
if (ins[0].equals("push")) {
s.add(Integer.parseInt(ins[1]));
}
else if (ins[0].equals("pop")) {
if (s.isEmpty()) {
sb.append(-1).append('\n');
} else {
sb.append(s.remove()).append('\n');
}
}
else if (ins[0].equals("size")) {
sb.append(s.size()).append('\n');
} else if (ins[0].equals("empty")) {
if (s.isEmpty()) {
sb.append("1").append('\n');
} else {
sb.append("0").append('\n');
}
}
else if (ins[0].equals("top")) {
if (s.isEmpty()) {
sb.append("-1").append('\n');
} else {
sb.append(s.peek()).append('\n');
}
}
else if(ins[0].equals("front")) {
if (s.isEmpty()) {
sb.append(-1).append('\n');
}
else {
sb.append(s.peek()).append('\n');
}
}
else if(ins[0].equals("back")) {
if (s.isEmpty()) {
sb.append(-1).append('\n');
}
else {
sb.append(s.peekLast()).append('\n');
}
}
else {
System.out.println("ERROR");
}
}
System.out.println(sb.toString());
br.close();
return;
}
}