#include <iostream>
#include <stack>
using namespace std;

int main() {
    int n;
    while (cin >> n) {
        stack<int> s;
        char c;
        int a;
        for(int i = 0; i < n; i++) {
            cin>>c;
            if(c=='P')
            {
                cin>>a;
                s.push(a);
            }
            else if(c=='O'&&!s.empty())
                s.pop();
            else if(c=='A')
            {
                if(s.empty())
                    cout<<"E"<<endl;
                else
                    cout<<s.top()<<endl;
            }
        }
    }
}