#include <bits/stdc++.h>
#define endl '\n'
#define LL long long
#define LD long double
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define debug(x) cerr << #x << " is " << x << endl;
using namespace std;
int const MAXN = 2e6 + 9;
set<pair<int,int>> have;
set<pair<int,int>> st;
int main(){
    ios_base::sync_with_stdio (0),cin.tie(0);
    cout.tie(0);
    vector<int> out;
    int q;
    cin >> q;
    int id = 0;
    for(int i = 0; i < q; i++){
        int t;
        cin >> t;
        if(t == 1){
            int m;
            cin >> m;
            id++;
            have.insert({id, m});
            st.insert({m, -id});
        }
        if(t == 2){
            auto x = (have.begin());
            out.push_back(x->first);
            have.erase(have.begin());
            st.erase({x->second,-x->first});
        }
        if(t == 3){
            auto x = st.end();
            x--;
            int it = -x->second;
            out.push_back(it);
            have.erase({-x->second,x->first});
            st.erase(x);
        }
    }
    for(auto x : out) cout << x << " ";
}