#include<bits/stdc++.h>

using namespace std;

const int N = 5e5 + 10;

int read()
{
    char s = getchar();
    int f = 1, x = 0;
    while(s < '0' || s > '9'){if(s == '-')f = -1;s= getchar();}
    while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1)+ (s ^ 48); s = getchar();} 
    x *= f;
    return x;
}

bool b[N];
int n;

int main()
{
    n = read();
    priority_queue<pair<int, int> >q1;
    queue<int>q2;
    int num = 0;
    for(int i = 1; i <= n; i++)
    {
        int x = read();
        if(x == 1)
        {
            int m = read();
            num++;
            q1.push(make_pair(m, -num));
            q2.push(num);
        }
        if(x == 2)
        {
            while(b[q2.front()])
                q2.pop();
            b[q2.front()] = 1;
            cout << q2.front() << ' ';
        }
        if(x == 3)
        {
            int a;
            while(1)
            {
                a = -q1.top().second;
                if(!b[a])
                    break;
                q1.pop();
            }
            b[a] = 1;
            cout << a << ' ';
        }
    }
    return 0;
}