#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
struct Tree{
int inf=1e9,idx=0;
struct node{
int x,tim;
bool operator <(node o) const{
if(x != o.x) return x < o.x;
else return tim < o.tim;
}
};
tree<node, null_type, less<node>,rb_tree_tag,tree_order_statistics_node_update> T;
void insert(int x){T.insert({x,++idx});}
bool have(int x){
node cur={x,0};
int order=T.order_of_key(cur);
if(order+1>(int)T.size()) return 0;
auto it=T.find_by_order(order);
if((*it).x!=x) return 0;
return 1;
}
void erase(int x){
if(!have(x))return;
node cur={x,0};
int order=T.order_of_key(cur);
auto it=T.find_by_order(order);
T.erase(it);
}
int getrank(int x){return T.order_of_key({x,0})+1;}
int getbyrank(int rank){
if((int)T.size()<rank)return -inf;
return (*T.find_by_order(rank-1)).x;
}
int getpre(int x){
int rank=getrank(x);
if(rank==1)return -inf;
return getbyrank(rank-1);
}
int getnex(int x){
int rank=getrank(x+1);
if(rank>(int)T.size())return -inf;
return getbyrank(rank);
}
}tr;
int n,op,x;
signed main()
{
#ifdef ONLINE_JUDGE
oj=0;
#endif
cin>>n;
while(n--){
cin>>op>>x;
switch(op){
case 1:
tr.insert(x);
break;
case 2:
tr.erase(x);
break;
case 3:
cout<<tr.getrank(x)<<endl;
break;
case 4:
cout<<tr.getbyrank(x)<<endl;
break;
case 5:
cout<<tr.getpre(x)<<endl;
break;
default:
cout<<tr.getnex(x)<<endl;
}
}
return 0;
}
(用的是pbds)