#include <iostream>
#include <cstdio>
using namespace std;
const int N =1E5 + 10;
int stk[N], tt;
int main(){
int n; scanf("%d", &n);
while(n -- ){
string op; cin >> op;
if(op == "push"){
int x; scanf("%d", &x);
stk[tt ++ ] = x;
}else if(op == "pop") tt -- ;
else if(op == "query") cout << stk[tt - 1] << endl;
else{
if(tt == 0) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
return 0;
}