#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
#define int long long
const int inf=1e12;
typedef pair<int,int>pii;
typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update>Tree;
Tree tr;
int n,opt,x;
int tr_clock=0;
signed main()
{
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>opt>>x;
tr_clock++;
if(opt==1)
{
tr.insert(make_pair(x,tr_clock));
}
if(opt==2)
{
Tree::iterator it;
it=tr.upper_bound(make_pair(x,0));
tr.erase(it);
}
if(opt==3)
{
int k=tr.order_of_key(make_pair(x,0))+1;
cout<<k<<"\n";
}
if(opt==4)
{
Tree::iterator it;
it=tr.find_by_order(x-1);
cout<<(*it).first<<"\n";
}
if(opt==5)
{
Tree::iterator it;
it=tr.upper_bound(make_pair(x,0));
it--;
cout<<(*it).first<<"\n";
}
if(opt==6)
{
Tree::iterator it;
it=tr.upper_bound(make_pair(x,inf));
cout<<(*it).first<<"\n";
}
}
}