#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int main()
{
    int n;
    while (cin >> n) {
        if (n == 0) {
            break;
        }
        char type;
        stack<int> Mystack;
        int x;
        for (int i = 0; i < n; i++)
        {
            cin >> type;
            if (type == 'A') {
                if (Mystack.empty()) {
                    cout << "E" << endl;
                }
                else
                {
                    cout << Mystack.top() << endl;
                }
            }
            else if (type == 'P') {
                cin>>x;
                Mystack.push(x);
            }
            else if (type == 'O') {
                if(!Mystack.empty()){
                    Mystack.pop();    
                }        
            }
        }
        cout << "\n";

    }


    return 0;
}