#include<iostream>
using namespace std;
const int N = 100010;
//tt永远指向栈顶
int stk[N];
int tt =0;
void push(int x){
//注意是前置++
stk[++tt] = x;
}
void pop(){
tt--;
}
int query(){
return stk[tt];
}
int empty(){
if(tt == 0)return 1;
else return 0 ;
}
int main(){
int m;
cin >> m;
while(m--){
string op;
cin >> op;
int x;
if(op == "push"){
cin >> x;
//在栈顶插入一个元素
push(x);
}
else if(op == "pop"){
pop();
}
else if(op == "empty"){
if(empty())cout << "YES" << endl;
else cout << "NO" << endl;
}
else if(op == "query"){
cout << query() << endl;
}
}
return 0;
}