#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
/*
只用考虑怎么移动栈顶指针就行了
*/
const int N = 100010;
int m;
int s[N];
int h;
int main()
{
cin >> m;
while (m -- )
{
string op;
int x;
cin >> op;
if(op == "push")
{
cin >> x;
s[h ++ ] = x;
}
else if(op == "pop")
{
h -- ;
}
else if(op == "query")
{
cout << s[h - 1] << endl;
}
else
{
if(h == 0)
puts("YES");
else
puts("NO");
}
}
return 0;
}