//KY179 堆栈的使用
#include <iostream>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
int n, x;
char op;
int main()
{
    while(cin>>n){
        stack<int>s;
        for(int i=1;i<=n;i++){
            cin>>op;
            if(op=='P') {cin>>x;s.push(x);}
            else if(op=='O'){
                if(s.size()) s.pop();
            }
            else{
                if(s.size()) cout<<s.top()<<"\n";
                else cout<<"E\n";
            }
        }
    }
    return 0;
}