import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static class node{
int v,p,size,cnt;
int[] s =new int[2];
node(int v,int p) {
this.p=p;
this.v=v;
cnt=size=1;
}
node(){
}
}
static int root;
static int idx;
static ArrayList<node> a =new ArrayList();
static int t(int a,int b) {
if(a>b) return 1;
else return 0;
}
static void pushup(int x) {
a.get(x).size=a.get(a.get(x).s[0]).size+a.get(a.get(x).s[1]).size+a.get(x).cnt;
}
static void rotate(int x) {
int y=a.get(x).p, z=a.get(y).p;
int k=0;
if(a.get(y).s[1]==x) k=1;
if(a.get(z).s[0]==y){
a.get(z).s[0]=x;
a.get(x).p=z;
}
else {
a.get(z).s[1]=x;
a.get(x).p=z;
}
a.get(y).s[k]=a.get(x).s[k^1];
if(a.get(x).s[k^1]!=0) a.get(a.get(x).s[k^1]).p=y;
a.get(x).s[k^1]=y;
a.get(y).p=x;
pushup(y);pushup(x);
}
static void splay(int x,int k) {
while(a.get(x).p!=k) {
int y=a.get(x).p,z=a.get(y).p;
if(z!=k){
boolean t=(a.get(y).s[0]==x)^(a.get(z).s[0]==y);
if(t) rotate(x);
else rotate(y);
}
rotate(x);
}
if(k==0) root=x;
}
static void find(int v) {
int x=root;
while(a.get(x).v!=v && a.get(x).s[t(v,a.get(x).v)]!=0) {
x=a.get(x).s[t(v,a.get(x).v)];
}
splay(x,0);
}
static int getpre(int v) {
find(v);
int x=root;
if(a.get(x).v<v) return x;
x=a.get(x).s[0];
while(a.get(x).s[1]!=0) x=a.get(x).s[1];
splay(x,0);
return x;
}
static int getsuc(int v) {
find(v);
int x=root;
if(a.get(x).v>v) return x;
x=a.get(x).s[1];
while(a.get(x).s[0]!=0) x=a.get(x).s[0];
splay(x,0);
return x;
}
static void del(int v) {
int pre=getpre(v);
int suc=getsuc(v);
splay(pre,0);
splay(suc,pre);
int del = a.get(suc).s[0];
if(a.get(del).cnt>1) {
a.get(del).cnt--;
splay(del,0);
}
else {
a.get(suc).s[0]=0;
splay(suc,0);
}
}
static int getrank(int v) {
insert(v);
int res= a.get(a.get(root).s[0]).size;
del(v);
return res;
}
static int getv(int k) {
k++;
int x=root;
while(true) {
int y=a.get(x).s[0];
if(a.get(y).size+a.get(x).cnt<k) {
k-=a.get(y).size+a.get(x).cnt;
x=a.get(x).s[1];
}
else if(a.get(y).size>=k) x=y;
else break;
}
return a.get(x).v;
}
static void insert(int v) {
int x=root,p=0;
while(x!=0&&a.get(x).v!=v) {
p=x;
x=a.get(x).s[t(v,a.get(x).v)];
}
if(x!=0) a.get(x).cnt++;
else {
x=++idx;
node n =new node(v,p);
a.add(n);
a.get(p).s[t(v,a.get(p).v)]=x;
}
splay(x,0);
}
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
node no =new node();
a.add(no);
insert(1000000000); insert(-1000000000);
int n =in.nextInt();
for(int i=0;i<n;i++) {
int op =in.nextInt(),x=in.nextInt();
if(op==1) insert(x);
if(op==2) del(x);
if(op==3) System.out.println(getrank(x));
if(op==4) System.out.println(getv(x));
if(op==5) System.out.println(a.get(getpre(x)).v);
if(op==6) System.out.println(a.get(getsuc(x)).v);
}
}
}