#include<bits/stdc++.h>
using namespace std;
int n;
stack<int> q,qs;
void pushq(int ph){
stack<int> uq;
while(qs.top()>ph){
uq.push(qs.top());
qs.pop();
}
qs.push(ph);
while(!uq.empty()){
qs.push(uq.top());
uq.pop();
}
}
void delq(int dl){
stack<int> uq;
while(qs.top()>dl){
uq.push(qs.top());
qs.pop();
}
qs.pop();
while(!uq.empty()){
qs.push(uq.top());
uq.pop();
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
int t;
cin>>t;
if(t==0){
int x;
cin>>x;
q.push(x);
pushq(x);
}
else if(t==1&&!q.empty()){
delq(q.top());
q.pop();
}
else if(t==2){
if(qs.empty()) cout<<0<<endl;
else cout<<qs.top()<<endl;
}
}
}
大佬们求调QAQ