RT,TLE

#include<bits/stdc++.h>
using namespace std;
int t;
int to,k;
struct node{
int x;
int del;
};
deque<node> q;
vector<node> a;
int main(){
cin>>t;
while(t -- ){
cin>>to;
if(to == 1){
cin>>k;
q.push_back({k,0});
}else if(to == 2){
cin>>k;
while(k >= q.front().x-q.front().del){
k -= q.front().x-q.front().del;
q.pop_front();
}
node f = q.front();
q.pop_front();
f.del = k;
q.push_front(f);
}else if(to == 3){
cin>>k;
a.clear();
while(k > q.front().x-q.front().del){
k -= q.front().x-q.front().del;
a.push_back(q.front());
q.pop_front();
}
cout<<k+q.front().del<<"\n";
for(int i = a.size()-1; i >= 0; i -- ){
q.push_front(a[i]);
}
}else{
int maxn = 0;
a.clear();
while(!q.empty()){
maxn = max(maxn,q.front().x);
a.push_back(q.front());
q.pop_front();
}
cout<<maxn<<"\n";
for(int i = a.size(); i >= 0; i -- ){
q.push_front(a[i]);
}
}
}
}