#include<bits/stdc++.h>

using namespace std;

#define endl "\n"
#define pb push_back


typedef long long ll;

const int mod=1e9+7;




void test_case(){
	
	int n;
	cin>>n;
	int idx=0,id=0;
	vector<bool> v(500005, false);
	v[0]=true;
	priority_queue<pair<int,int>> q;
	for(int i=0;i<n;i++){
		int x;
		cin>>x;
		if (x==1){
			int val;
			cin>>val;
			id++;			
			q.push({val,-id});
		}
		else if (x==2){
			while (v[idx]){
				idx++;
			}
			v[idx]=1;
			cout<<idx<<endl;
		}
		else{
			while (v[-q.top().second]){
				q.pop();
			}
			int tmp=-q.top().second;
			v[tmp]=1;
			cout<<tmp<<endl;
		}
		
	}
	
	

}

int main(){
	int t=1;
	//cin>>t;
	for(int i=0;i<t;i++){
		test_case();
	}

}