#include <bits/stdc++.h>

using namespace std;

struct CustomerM
{
	int id, m;

	bool operator<(const CustomerM &c) const
	{
		return id < c.id;
	}
};

struct CustomerP
{
	int m, id;

	bool operator<(const CustomerP &c) const
	{
		return m > c.m or (m == c.m && id < c.id);
	}
};

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int q;
	cin >> q;
	set<CustomerM> id_m;
	set<CustomerP> m_id;
	for (int i = 0, id = 1; i < q; i++)
	{
		int querry;
		cin >> querry;
		if (querry == 1)
		{
			int m;
			cin >> m;
			id_m.insert({id, m});
			m_id.insert({m, id});
			id++;
		}
		else if (querry == 2)
		{
			auto customer = *id_m.begin();
			id_m.erase({customer.id, customer.m});
			m_id.erase({customer.m, customer.id});
			cout << customer.id << ' ';
		}
		else
		{
			auto customer = *m_id.begin();
			id_m.erase({customer.id, customer.m});
			m_id.erase({customer.m, customer.id});
			cout << customer.id << ' ';
		}
	}
}