import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
Deque<Integer> queue = new LinkedList<>();
for(int i=0; i<n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
String order = st.nextToken();
int addNumber = 0;
switch(order) {
case "push":
addNumber = Integer.parseInt(st.nextToken());
queue.add(addNumber);
break;
case "pop":
if(queue.isEmpty()) sb.append("-1").append('\n');
else {
sb.append(queue.remove()).append('\n');
}
break;
case "size":
sb.append(queue.size()).append('\n');
break;
case "empty":
if(queue.isEmpty()) sb.append("1").append('\n');
else {
sb.append("0").append('\n');
}
break;
case "front":
if(queue.isEmpty()) sb.append("-1").append('\n');
else {
sb.append(queue.peek()).append('\n');
}
break;
case "back":
if(queue.isEmpty()) sb.append("-1").append('\n');
else {
sb.append(queue.peekLast()).append('\n');
}
break;
}
}
System.out.println(sb.toString());
}
}