#include<iostream>
using namespace std;
const int N=100010;
int st[N];
int m,cnt;
int main()
{
cin>>m;
while(m--)
{
string op;
cin>>op;
if(op=="push") cin>>st[cnt++];
else if(op=="pop") cnt--;
else if(op=="query") cout<<st[cnt-1]<<endl;
else if(op=="empty") cout<<(cnt==0?"YES":"NO")<<endl;
}
}