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