//土尔逊Torson 编写于2023/5/03
#define _CRT_SECURE_NO_WARNINGS
#include <stack>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstdio>

using namespace std;

int main() {
	char buf05601[50];
	int n;
	while (scanf("%d",&n) !=EOF && n != 0) {
		string subNum;
		stack<int> standard;
		fgets(buf05601, 50, stdin);
		string str05601 = buf05601;
		while (n--) {
			fgets(buf05601, 50, stdin);
			string str05601 = buf05601;
			str05601.pop_back();
			if (str05601[0] == 'P') {
				subNum = str05601.substr(2);
				standard.push(stoi(subNum));//stoi --> string to int
			}
			else if (str05601[0] == 'O') {
				if (!standard.empty()) {
					standard.pop();
					continue;
				}
			}
			else if (str05601[0] == 'A') {
				if (!standard.empty()) {
					printf("%d\n", standard.top());
				}
				else if (standard.empty()) {
					printf("E\n");
				}
			}
		}

	}
	system("pause");
	return EXIT_SUCCESS;
}

// 64 位输出请用 printf("%lld")