#include <string>
#include <iostream>
using namespace std;
const int maxn = 100010;
int st[maxn], top;
int main(int argc, char const *argv[])
{
int m; cin >> m;
string op;
while (m--) {
cin >> op;
if (op == "push") {
int x;
cin >> x;
st[top++] = x;
} else if (op == "pop") {
top--;
} else if (op == "empty") {
cout << (!top ? "YES" : "NO") << endl;
} else if (op == "query") {
cout << st[top - 1] << endl;
}
}
return 0;
}