#include<cstdio>
#include<deque>
using namespace std;
int top[1000001],prv[1000001];
int main(int,char**){
deque<int> queue;
int test,len=0,sum=0,num=1;
scanf("%*d %d",&test);
while(test--){
int got,sym;
scanf("%d",&sym);
if(sym==1){
scanf("%d",&got);
top[++len]=got;
prv[len]=prv[len-1]+got;
prv[len+1]=0;
while(!queue.empty()){
if(top[queue.back()]>got){
break;
};
queue.pop_back();
};
queue.push_back(len);
}else if(sym==2){
scanf("%d",&got);
sum+=got;
while(sum>=prv[num]){
++num;
};
while(!queue.empty()&&queue.front()<num){
if(queue.front()>=num){
break;
};
queue.pop_front();
};
}else if(sym==3){
scanf("%d",&got);
int low=num-1,hih=len;
while(low+1<hih){
int mid=(low+hih)>>1;
if(prv[mid]-sum>=got){
hih=mid;
}else{
low=mid;
};
};
printf("%d\n",got-prv[hih-1]+sum);
}else{
printf("%d\n",top[queue.front()]);
};
};
return 0;
};
WA on 11
RE on 12,17,20