#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int q[N], hh, tt = -1;
int main(){
int m;
cin>>m;
while(m--){
string op;
int x;
cin>>op;
if(op == "push"){
cin>>x;
q[++tt] = x;
}
else if(op == "pop"){
hh++;
}
else if(op == "empty"){
cout<<(tt >= hh ? "NO" : "YES")<<endl;
}
else if(op == "query"){
cout<<q[hh]<<endl;
}
}
return 0;
}