#include<iostream>
#include<cstring>
using namespace std;
int main(){
int a[100];
int n;
char c;
cin>>n;
int top=-1;
while(n--){
cin>>c;
if(c=='P'){
int x;
cin>>x;
a[++top]=x;
}
if(c=='O'){
while(-1!=top){
top--;}
}
if(c=='A'){
if(-1==top){cout<<'E';}
else
{printf("%d\n",a[top]);}
}
}
system ("pause");
return 0;
}