#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define dbg(msg, var) cout << msg << " " << var << endl;
//

const int MAX = 500010;
const ll MOD = (int)1e9 +7;
const int INF = 1e9;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-8;

int vis[MAX];

int32_t main() {
    sws;
    int qu;
    cin >> qu;

    queue<int> q;
    priority_queue<pii> pq;

    int idx = 1;
    while(qu--) {
        int tipo;
        cin >> tipo;

        if(tipo == 1) {
            int m;
            cin >> m;

            q.push(idx);
            pq.push(mp(m, -idx));
            idx++;
        }
        else if(tipo == 2) {
            int p = q.front();
            q.pop();
            while(vis[p]) {
                p = q.front();
                q.pop();
            }
            vis[p] = 1;
            cout << p << " ";
        }
        else {
            auto p = pq.top();
            pq.pop();
            while(vis[-p.ss]) {
                p = pq.top();
                pq.pop();
            }
            vis[-p.ss] = 1;
            cout << -p.ss << " ";
        }
    }
    cout << endl;

    // verificar limites, long long
    return 0;
}
