#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1000010;
int h[N], cnt;
int n, m,ans;
void down(int u)
{
int t = u;
if (u * 2 <= cnt && h[u * 2] < h[t])t = u * 2;
if (u * 2 + 1 <= cnt && h[u * 2 + 1] < h[t])t = u * 2 + 1;
if (u != t)
{
swap(h[t], h[u]);
down(t);
}
}
void up(int u)
{
while (u / 2 && h[u / 2] > h[u])
{
swap(h[u / 2], h[u]);
u /= 2;
}
}
int main()
{
cin.tie(0);cout.tie(0);
ios_base::sync_with_stdio(false);
cin >> n;
while (n--)
{
int w,x;
cin >> w;
if (w == 1)
{
cin >> x;
h[++cnt] = x;
up(cnt);
}
else if (w == 2)
{
cout << h[1];
puts("");
}
else if (w == 3)
{
h[1] = h[cnt--];
down(1);
}
}
}