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

int main() {
    int n;
    cin >> n;
    string want[10000] = {};
    int i, j;
    int sum = 0;
    stack<int> s;
    string str = "\n";
    getline(cin, str);
    for (i = 0; i < n; i++)
    {
        getline(cin, want[i]);
    }
    for (i = 0; i < n; i++)
    {
        //for(j=0;j<want[i].size();j++)
        //{
        if (want[i][0] == 'A')
        {
            if (!empty(s))
            {
                cout << s.top() << endl;
            }
            else {
                cout << 'E' << endl;
            }

        }
        if (want[i][0] == 'P')
        {
            for (j = 2; j < want[i].size(); j++)
            {
                sum = sum + pow(10, want[i].size() - j-1) * (want[i][j] - '0');
            }
            s.push(sum);
            sum = 0;
        }
        if (want[i][0] == 'O')
        {
            if (!empty(s))
            {
                s.pop();
            }
            else {
                continue;
            }
        }
        //}
    }

}
// 64 位输出请用 printf("%lld")