#include<iostream>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
queue<int> q;
int main(){
int n,a,z;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a;
if(a==1){
cin>>z;
q.push(z);
}
if(a==2){
if(pq.size()){
cout<<pq.top()<<endl;
pq.pop();
}else{
cout<<q.front()<<endl;
q.pop();
}
}else{
while(!q.empty()){
pq.push(q.front());
q.pop();
}
}
}
return 0;
}