// "C:\Users\ianli\Desktop\CP\template\"
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define MTP make_tuple
typedef long long int ll;

int main() {
	//ios::sync_with_stdio(false);
	//cin.tie(0);

	int q, idx = 1;
	vector<int> ans;
	queue<pair<int, int>> qt;
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> sqt;
	priority_queue<pair<int, int>> pq, spq;
	scanf("%d", &q);
	for (int i = 1; i <= q; i++) {
		int type;
		scanf("%d", &type);
		if (type == 1) {
			int m;
			scanf("%d", &m);
			pq.push(MP(m, -idx));
			qt.push(MP(idx, m));
			idx++;
		}
		else if (type == 2) {
			while (!sqt.empty() && qt.front().F == sqt.top().F) {
				sqt.pop();
				qt.pop();
			}
			ans.PB(qt.front().F);
			spq.push(MP(qt.front().S, qt.front().F));
			qt.pop();
		}
		else {
			while (!spq.empty() && -pq.top().S == spq.top().S) {
				pq.pop();
				spq.pop();
			}
			ans.PB(-pq.top().S);
			sqt.push(MP(-pq.top().S, pq.top().F));
			pq.pop();
		}
	}
	printf("%d", ans[0]);
	for (int i = 1; i < int(ans.size()); i++) printf(" %d", ans[i]); printf("\n");

}
