//Code by Patcas Csaba

#include <vector>
#include <string> 
#include <set> 
#include <map> 
#include <queue> 
#include <bitset> 
#include <stack>
#include <list>

#include <numeric> 
#include <algorithm> 

#include <cstdio>
#include <fstream>
#include <iostream> 
#include <sstream> 
#include <iomanip>

#include <cctype>
#include <cmath> 
#include <ctime>
#include <cassert>

using namespace std;

#define LL long long
#define PII pair <int, int>
#define VB vector <bool>
#define VI vector <int>
#define VD vector <double>
#define VS vector <string>
#define VPII vector <pair <int, int> >
#define VVI vector < VI >
#define VVB vector < VB >

#define FORN(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define FORI(it, X) for(__typeof((X).begin()) it = (X).begin(); it !=(X).end(); ++it) 
#define REPEAT do{ 
#define UNTIL(x) }while(!(x)); 

#define SZ size()
#define BG begin() 
#define EN end() 
#define CL clear()
#define X first
#define Y second
#define RS resize
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(), x.end()

#define IN_FILE "a.in"
#define OUT_FILE "a.out"

int nq, c, val;
int heap[262145], dequeIndex[262145], n;
int heapIndex[100001], startIndex = 0, endIndex = -1;

void swapStuff(int index1, int index2)
{
	swap(heap[index1], heap[index2]);
	swap(dequeIndex[index1], dequeIndex[index2]); 
	swap(heapIndex[dequeIndex[index1]], heapIndex[dequeIndex[index2]]); 
}

void upHeap(int index)
{
	while (index > 1 && heap[index] > heap[index / 2])
	{
		swapStuff(index, index / 2);
		index /= 2;
	}
}

void downHeap(int index)
{
	while (index * 2 <= n)
	{
		int bigger = index * 2;
		if (index * 2 + 1 <= n && heap[index * 2] < heap[index * 2 + 1]) bigger = index * 2 + 1;
		if (heap[index] < heap[bigger])
		{
			swapStuff(index, bigger);
			index = bigger;
		}
		else return;
	}
}

void add(int value)
{
	++n;
	heap[n] = value;
	++endIndex;
	heapIndex[endIndex] = n;
	dequeIndex[n] = endIndex;
	upHeap(n);
}

void remove(int index)
{
	++startIndex;
	swapStuff(index, n);
	--n;
	downHeap(index);
}

int main()
{
	//Read data
	//freopen(IN_FILE, "r", stdin);
	//freopen(OUT_FILE, "w", stdout);

	//Solve
	cin >> nq;
	n = 0;
	FORN(test, nq) 
	{
		cin >> c;
		if (c == 1) 
		{
			cin >> val;
			add(val);
		}
		else
		{
			if (c == 2) remove(heapIndex[startIndex]);
			else cout << heap[1] << endl;
		}
	}

	//Write data

	return 0;
}