#include <cstdio>
#include <stack>
#include <iostream>

using namespace std;

int main(){
    int n;
    stack<int>use1;
    char str;
    int num;
    while(scanf("%d\n",&n) != EOF){
        for (int i =0;i<n;i++){
            scanf("%c",&str);
            if (str == 'P') {
                    scanf("%d", &num);
                    use1.push(num);
            }
            else if (str == 'O' && use1.size() != 0){
                use1.pop();
            }
            else if (str == 'A'){
                if (use1.size() ==0){
                    printf("E\n");
                }
                if (use1.size() != 0){
                    printf("%d\n",use1.top());
                }
            }
        }
    }
    return 0;
}