#include <bits/stdc++.h>
using namespace std;
struct Node {
    int id, value;
};
struct Cmp1 {
    bool operator () (const Node &t1, const Node &t2) {
        return t1.id < t2.id;
    }
};
struct Cmp2 {
    bool operator () (const Node &t1, const Node &t2) {
        return t1.value == t2.value ? t1.id < t2.id : t1.value > t2.value;
    }
};

set<Node, Cmp1> s1;
set<Node, Cmp2> s2;
int main() {
    int q; cin >> q;
    int L = 0;
    while(q--) {
        int op; cin >> op;
        if(op == 1) {
            int x; cin >> x;
            ++L;
            s1.insert({L, x});
            s2.insert({L, x});
        } else if(op == 2) {
            auto it = s1.begin();
            cout << it -> id << " ";
            s1.erase(*it);
            s2.erase(*it);
        } else {
            auto it = s2.begin();
            cout << it -> id << " ";
            s1.erase(*it);
            s2.erase(*it);
        }
    }
    return 0;
}