//#include <bits/stdc++.h>
//
//using namespace std;
//ifstream f("heapuri.in");
//ofstream g("heapuri.out");
//const int N = 100010;
//int n,m,op,x,lg;
//bitset<N> valid;
//priority_queue<pair<int,int>> pq;
//int main()
//{
// f>>n;
// for(int i=1;i<=n;i++)
// {
// f>>op;
// if(op==1)
// {
// f>>x;
// m++;
// valid[m]=1;
// pq.push(make_pair(-x,m));
// }
// else
// if(op==2)
// {
//
// f>>x;
// valid[x]=0;
//
// }
// else
// {
// while(!valid[pq.top().second])
// pq.pop();
// g<<-pq.top().first<<'\n';
// }
// }
// return 0;
//}
#include <bits/stdc++.h>
/// Problema heap-uri arhiva educationala Infoarena
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
const int N = 100010;
bitset<N> used;
int t,v,q,tip;
priority_queue<pair<int,int>> pq;
int main()
{
f>>q;
for(;q;q--)
{
f>>tip;
if(tip==1)
{
t++;used[t]=1;
f>>v;
pq.push(make_pair(-v,t));
}
else if(tip==2)
{
f>>t;used[t]=0;
}
else
{
while(!used[pq.top().second])
pq.pop();
g<<-pq.top().first<<'\n';
}
}
return 0;
}