#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int main(){
    stack<int> Stack;
    int n;
    char op;
    int number;
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;++i){
            scanf("%c",&op);
            if(op=='P'){
                scanf("%d",&number);
                Stack.push(number);
            }
            else if(op=='O'&&!Stack.empty()){
                Stack.pop();
            }
            else if(op=='A'){
                if(!Stack.empty()){
                    printf("%d\n",Stack.top());
                }
                else{
                    cout<<'E'<<endl;
                }
            }
        }
    }
    return 0;
}