#include    <bits/stdc++.h>
using       namespace std;
#define     endl "\n"
#define     pb push_back
#define     ll long long
#define     int ll
#define     mod 1000000007
#define     DEBUG cout<<"Hello1\n";
#define     all(v) (v).begin(),(v).end()
#define     clr(x) memset(x,0,sizeof(x))
#define     deb(...) debug(#__VA_ARGS__, __VA_ARGS__);
const int   N = 1e6 + 5;
const ll    inf = 1e18 + 5;
typedef     pair<int, int> pii;
void        fileIO();

template<class T> void print_container(T &container);
template<class T> void print_pair_container(T &container);
template<class T> void printArr(T &a, int n);
template<typename T> void debug(string s, T x);
template<typename T, typename... Args>
void debug(string s, T x, Args... args);

int q, m;

struct cmp {
	bool operator() (pii a, pii b) const {
		if (a.first == b.first) {
			return a.second < b.second;
		} else {
			return a.first > b.first;
		}
	}
};

void solve() {
	cin >> q;

	int curr = 1;
	set<pii>  m;
	set<pii, cmp> p;

	while (q--) {
		int type;
		cin >> type;
		if (type == 1) {
			int cost;
			cin >> cost;
			p.insert({cost, curr});
			m.insert({curr++, cost});
		}
		else if (type == 2) {
			auto x = *m.begin();
			m.erase(x);

			cout << x.first << " ";
			p.erase({x.second, x.first});
		} else {
			auto x = *p.begin();
			p.erase(x);

			cout << x.second << " ";
			m.erase({x.second, x.first});
		}
	}


}

int32_t main()
{
	fileIO();

	int t = 1;
	// cin >> t;
	while (t--)
		solve();

	return 0;
}






void fileIO() {

#ifndef ONLINE_JUDGE
	freopen("../../input.txt", "r", stdin);
	freopen("../../output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
}


template<typename T> void debug(string s, T x)
{
	cout << s << " = " << x << "\n";
}
template<typename T, typename... Args> void debug(string s, T x, Args... args)
{
	cout << s.substr(0, s.find(',')) << " = " << x << " | ";
	debug(s.substr(s.find(',') + 2), args...);
}

template<class T> void print_container(T &container)
{
	for (auto &t : container)
	{
		cout << t << " ";
	}
	cout << endl;
}
template<class T> void print_pair_container(T &container)
{
	for (auto &t : container)
	{
		cout << t.first << " " << t.second << endl;
	}
	cout << endl;
}
template<class T> void printArr(T &a, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}

