#include<iostream>
#include<queue>
#include<cstring>

#define MAX 500005
using namespace std;

struct people{
	int id;
	int money;
};
bool operator < (people a,people b)
{
	if(a.money != b.money){
		return a.money < b.money;
	}else{
		return a.id > b.id; 
	} 
}
priority_queue<people> que;
bool sever[MAX];
int main( )
{
	ios::sync_with_stdio(false);
	int q;
	memset(sever,false,sizeof(sever));
	cin >> q;
	int id = 0;
	int have_sever = 0;
	while(q--){
		int _type;
		cin >> _type;
		if(_type == 1){
			int m;
			cin >> m;
			people p;
			p.id = id;
			p.money = m;
			que.push(p);
			id++;
		}else if(_type == 2){
			while(sever[have_sever]){
				have_sever++;
			}
			cout << have_sever + 1 << endl;
			sever[have_sever] = true;
		}else if(_type == 3){
			people p = que.top();
			que.pop();
			while(sever[p.id]){
				p = que.top();
				que.pop();
			}
			sever[p.id] = true;
			cout << p.id + 1<< endl;
		}
	}
	return 0;
}


 
