using System;
class p3378
{
public static int n, op, size = 0;
public static int[] heap = new int[100010];
public static void put(int d)
{
int son, fa;
size++;
heap[size] = d;
son = size;
while (son > 1)
{
fa = son >> 1;
if (heap[son] >= heap[fa]) break;
int t = heap[son];
heap[son] = heap[fa];
heap[fa] = t;
son = fa;
}
}
public static void del()
{
int son, fa;
heap[1] = heap[size];
size--;
fa = 1;
while (fa * 2 <= size)
{
son = fa * 2;
if (heap[son] > heap[son + 1]) son++;
if (heap[son] >= heap[fa]) break;
int t = heap[son];
heap[son] = heap[fa];
heap[fa] = t;
fa = son;
}
}
public static int get()
{
int son, fa, res;
res = heap[1];
heap[1] = heap[size];
size--;
fa = 1;
while (fa * 2 <= size)
{
son = fa * 2;
if (heap[son] > heap[son + 1]) son++;
if (heap[son] >= heap[fa]) break;
int t = heap[son];
heap[son] = heap[fa];
heap[fa] = t;
fa = son;
}
return res;
}
static void Main(string[] args)
{
n = int.Parse(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
string m;
m=Console.ReadLine();
string[] cnt = m.Split(' ');
op = int.Parse(cnt[0]);
int x;
switch (op)
{
case 1:
x = int.Parse(cnt[1]);
put(x);
break;
case 2:
x = get();
Console.WriteLine(x);
break;
case 3:
del();
break;
}
}
}
}