#include <fstream>
#include <bitset>
#include <queue>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int N, tip, x, nr;
bitset < 200005 > out;
struct cmp
{
bool operator()(const pair <int, int> &a, const pair <int, int> &b)const
{ return a.first>b.first; }
};
priority_queue <pair <int, int>, vector <pair <int, int> >, cmp> Q;
int main()
{
f>>N;
for (int i=1; i<=N; ++i)
{
f>>tip;
if (tip==1) f>>x, Q.push(make_pair(x, ++nr));
else if (tip==2) f>>x, out[x]=1;
else
{
while (out[Q.top().second]) Q.pop();
g<<Q.top().first<<'\n';
}
}
return 0;
}