#include <bits/stdc++.h>
using namespace std;
int a[10005],x,b=1,c=1,n;
int main(){
cin >> n;
for (int i = 1; i <= n; i++){
cin >> x;
if (x==1){
cin >> a[b];
++b;
} else if (x==2){
++c;
} else if (x==3){
if (c==b){
cout << "ERR_CANNOT_POP" << endl;
} else
cout << a[c] << endl;
} else {
if (c==b){
cout <<"ERR_CANNOT_QUERY" << endl;
} else
cout << b-c << endl;
}
}
return 0;
}