#include"bits/stdc++.h"
using namespace std;
#define MOD 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long 
#define u_map unordered_map<int,int>
#define vll vector<ll>
#define vi vector<int>
#define up upper_bound
#define lb lower_bound 
#define tt int t;cin>>t;while(t--)
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
static int mod=5;
long long power(long long x, long long y) 
{ 
   long long ans= 1;
    while (y > 0) { 
        if (y & 1)   ans = (ans * x); 
        y = y >> 1; 
        x = (x * x); 
    }
    return ans; 
} 
bool prime(long long  n){
    for(long long i=2;i<=sqrt(n);i++){
        if(n%i==0) false;
    }
     return true;
}

int main() 
{ 
    #ifndef ONLINE_JUDGE

   freopen("input.txt", "r", stdin);

   freopen("output.txt", "w", stdout);

    #endif // ONLINE_JUDGE
    
        fio
        
        int q;
        int i=1;
        set<pair<int,int>>s1,s2;
        vi ans;
        int x,y;
        cin>>q;for(int j=0;j<q;j++){
            int n;
            cin>>n;
           set<pair<int,int>>::iterator it;
            switch(n){
                case 1:
                int m;cin>>m;
                s1.insert(mp(i,m));
                s2.insert(mp(m,i));
                i++;
                break;
                case 2:
                it=s1.begin();
                x=it->first;
                y=it->second;
                s1.erase(it);
                ans.pb(x);
                it=s2.upper_bound({y,x});
                it--;
                s2.erase(it);
                break;
                case 3:
                it =s2.end();
                it--;
                 x=it->first;
                it=s2.lower_bound({x,1});
                x=it->first;
                 y=it->second;
                 s2.erase(it);
                ans.pb(y);
                it=s1.lower_bound({y,x});
                s1.erase(it);
                break;
            }
        }
         for(int i=0;i<ans.size();i++) cout<<ans[i]<<" ";
          
    
    return 0; 
} 