/*

**     **   ***   ******** **   **  ****  **     **   ***    **    **  ****
***    **  *****  ******** **   **  ****  ***    **  *****    **  **   *****
****   ** **   **    **    **   **   **   ****   ** **         ****    **  **
** **  ** **   **    **    *******   **   ** **  ** **          **     **   *
**  ** ** **   **    **    *******   **   **  ** ** **  ***     **     **   *
**   **** **   **    **    **   **   **   **   **** **   **    ****    **  **
**    ***  *****     **    **   **  ****  **    ***  *****    **  **   *****
**     **   ***      **    **   **  ****  **     **   ***    **    **  ****

Instead of hacking my Solution.
Go and solve a PROBLEM. :)

*/

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;

#define endl               '\n'
#define F                  first
#define S                  second
#define debug(x)           cerr << #x << " = " << x << endl
#define Size(x)            x.size()
#define all(x)             x.begin() ,x.end()
#define pb                 push_back
#define ppb                pop_back

const ll MAXN = 5e5 + 10;
const ll INF = 2e18;
const ld DINF = 1.79769e+308;

ll q, n, m, cnt = 1, ptr = 1;
bool a[MAXN];
bool cmp(pll a, pll b){
	if ( a.F == b.F)
		return a.S < b.S;
	return a.F > b.F;
}
set <pll,decltype(cmp)*> st(cmp);

void solve(){
	
	cin >> q;
	 
	for ( int i = 0; i < q; i++){
		cin >> n;
		if ( n == 1){
			cin >> m;
			a[cnt] = 1;
			st.insert({m, cnt});
			cnt++;
		}
		else if ( n == 2){
			while(!a[ptr]){
				ptr++;
			}
			cout << ptr << ' ';
			a[ptr] = 0;
		}
		else{
			bool b = 1;
			while(b){
				auto it = st.begin();
				if (a[(*it).S]){
					cout << (*it).S << ' ';
					a[(*it).S] = 0;
					b = 0;
				}
				st.erase(it);
			}
		}
	}
}

int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // Comment this line if you use printf, scanf.
	
	solve();
	
	return 0;
}
/*  
	Wrong answer reasons:
	* Int overflow ( Use f*cking ll)
	* Array bounds ( Get more Array size)
	* Special cases Ex: n=1 
	* Wrong approach ( Instead of stay on it, change it :/ )
	* ....
*/
 

