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