#include <bits/stdc++.h>
#define int long long
#define f(i,a,n) for (int i = a; i < n; ++i)
#define f1(i,a,n) for(int i=n-1;i>=a;i--)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define fastio ios_base::sync_with_stdio(false), cin.tie(NULL);
#define w1 solve();
#define mod (int)(1e9 + 7)
#define prq priority_queue
#define gcd __gcd
#define sz(x) (int)x.size()
#define w(t) int t; cin>>t; while (t--) { solve(); }
#define N 1000001

using namespace std;

void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifdef Avash_Local_Machine
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpi;
typedef vector<string> vs;
typedef vector<char> vc;

const int INF = (int)(1e17);
const double PI = 3.141592653589793;

int dx[4] = { -1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
string path = "URDL";


void solve()
{
	int q;
	cin >> q;
	char c; int n , id = 1;
	set<pii> set1, set2;
	//set1 for price,id
	//set2 for id,price
	while (q--)
	{
		cin >> c;
		if (c == '1')
		{
			cin >> n;
			set1.insert(mp(n, id));
			set2.insert(mp(id, n));
			++id;
		}
		if (c == '2')
		{
			auto it = *set2.begin();
			cout << it.ff << "\n";
			auto ind = set1.lower_bound(mp(it.ss, it.ff));
			set1.erase(ind);
			set2.erase(set2.begin());
		}
		if (c == '3')
		{
			auto it = *set1.rbegin();
			// debug(it);
			auto ind = set1.lower_bound(mp(it.ff, -INF));
			// debug(ind);
			cout << (*ind).ss << "\n";
			auto ind1 = set2.lower_bound(mp((*ind).ss, (*ind).ff));
			// debug(*ind1);
			set2.erase(ind1);
			set1.erase(ind);
			// debug(set2);
		}
	}
}

int32_t main()
{

	fastio
	w1
	// w(t)
	return 0;
}