#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> intpair;

#define MAXN 500005 

int main(){
	
	#ifndef ONLINE_JUDGE
    	freopen("input", "r", stdin);
      //to check for segfault in sublime, uncomment the following line
    	freopen("output", "w", stdout);
    	freopen("error", "w", stderr);
	#endif
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll q;
    cin>>q;

    ll curr=1;
    ll currUnserved=1;

    ll money[MAXN];
    bool served[MAXN];
    for(int i=0;i<MAXN;i++){
        money[i]=-1;
        served[i]=false;
    }
    set<intpair> mySet;

    while(q--){
        ll t;
        cin>>t;


        if(t==1){
            ll m;
            cin>>m;

            money[curr]=m;

            mySet.insert({-m, curr});
            curr++;
        }
        else if (t==2){
            while(served[currUnserved])currUnserved++;
            cout<<currUnserved<<" ";
            mySet.erase(mySet.find(make_pair(-money[currUnserved], currUnserved)));
            served[currUnserved]=true;
            while(served[currUnserved])currUnserved++;
        }
        else{
            auto temp = mySet.begin();
            cout<<temp->second<<" ";
            served[temp->second]=true;
            mySet.erase(temp);
        }
    }
}
