#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vdb;
#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
const int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0}, dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void DBG() {
	cerr << "]" << endl;
}
template<class H, class... T> void DBG(H h, T... t) {
	cerr << to_string(h);
	if(sizeof...(t))
		cerr << ", ";
	DBG(t...);
}

#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)

void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x) read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x) read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v)) res += char('0' + v[i]);
    return res;
}
template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S) res += char('0' + b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f) res += ' ';
        f = 0;
        res += to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
inline int bitCount(int number) {
    return __builtin_popcount(number);
}
inline int bitCount(ll number) {
    return __builtin_popcount(number);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t)) write(' ');
    print(t...);
}
template<class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int MOD = 1e9 + 7 ; //1073741824;
const int N = 2e6 + 5;
const ll INF = 9e19;
const ld PI = acos((ld)-1);
const int M = 501;
pii a[N];
int type[N], num[N];
bool used[N];
struct PairCmp
{
    bool operator()(const pii& a, const pii& b) const
    {
        if(a.ff > b.ff) return 0;
        else if(a.ff < b.ff) return 1;
        else {
            if(a.ss > b.ss) return 1;
            else return 0;
        }
    }
};
set<pii, PairCmp> f;
set<pii> s;
void solve() {
    int q;
    read(q);
    int cnt = 1;
    for(int i = 1; i <= q; ++i) {
        read(type[i]);
        if(type[i] == 1) {
            read(num[i]);
            f.ins(mp(num[i], cnt));
            s.ins(mp(cnt, num[i]));
            cnt++;
        }
        else if(type[i] == 2) {
            auto x = *s.begin();
            print(x.ff);
            auto pos = f.find(mp(x.ss, x.ff));
            f.erase(pos);
            s.erase(s.begin());
            //EACH(pairs, f) print(pairs.ff, pairs.ss);
        }
        else {
            auto x = *f.rbegin();
            print(x.ss);
            auto pos = s.find(mp(x.ss, x.ff));
            s.erase(pos);
            set<pii>::iterator it = f.end();
            --it;
            f.erase(it);
            //EACH(pairs, s) print(pairs.ff, pairs.ss);
        }
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case #", i + 1, ": ");
        solve();
    }
    r0;
}
