#include <bits/stdc++.h>
using namespace std;
long long a[10005],n,x,top1=1,end1=1,t;
void move1(){
if(t==0){cin >> a[1];t=1;top1=1;end1=1;return;}
cin >> a[end1+1];end1++;
return;
}
void move2(){
if(t==0||top1>end1)cout << "ERR_CANNOT_POP" << endl;
else top1++;
}
void move3(){
if(t==0||top1>end1)cout << "ERR_CANNOT_QUERY" << endl;
else cout << a[top1] << endl;
}
void move4(){
if(t==0||top1>end1)cout << 0 << endl;
else cout << end1-top1+1 << endl;
}
int main(){
cin >> n;
for(int i=1;i<=n;i++){
cin >> x;
if(x==1)move1();
if(x==2)move2();
if(x==3)move3();
if(x==4)move4();
}
for(int i=1;i<=n;i++)cout << a[i] << endl;
return 0;
}