public static void main(String args[]) throws IOException {
		StreamTokenizer reader = new StreamTokenizer(new BufferedReader(
				new InputStreamReader(System.in)));

		MyQueue<Integer> queue = new MyQueue<Integer>();

		while (reader.nextToken() != StreamTokenizer.TT_EOF) {
			int n = (int) reader.nval;

			String cmd = null;
			for (int i = 0; i < n; i++) {
				reader.nextToken();
				cmd = reader.sval;

				if (cmd.equals("PUSH")) {
					reader.nextToken();
					queue.appendTail((int) reader.nval);
				} else if (cmd.equals("POP")) {
					Integer head = queue.removeHead();
					if (head == null) {
						System.out.println(-1);
					} else {
						System.out.println(head.intValue());
					}
				}
			}

		}

	}