#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue>
#define C(x) ((int)(T[T[(x)].f].s[1]==(x)))
#define S(x) T[(x)].s
#define L(x) T[(x)].s[0]
#define Ln(x) T[T[(x)].s[0]]
#define R(x) T[(x)].s[1]
#define Rn(x) T[T[(x)].s[1]]
#define F(x) T[(x)].f
#define W(x) T[(x)].w
#define Z(x) T[(x)].size
using namespace std;
typedef long long LL;
const int MAXN=300;
const int INF=2147483647;
const int NINF=-1<<30;
const int ERR=-1;
struct Node{
int size,w,f;int s[2];//0-L,1-R;
Node():size(0),w(0),f(0){s[0]=s[1]=0;}
};
Node T[MAXN*3];
int root,sz;
void link(int s,int f,int t){F(s)=f;S(f)[t]=s;}
void Up(int x);void Down(int x);void Join(int f,int s);
int Max(int x);int Min(int x);
void Rot(int x,int c){
int f=F(x);
link(x,F(f),C(f));
link(S(x)[1^c],f,c);
link(f,x,1^c);
Up(f);
}
void Splay(int x,int g){
for(Down(x);F(x)!=g;){
int f=F(x);
if(F(f)==g) Rot(x,C(x));else{
if(C(x)==C(f)) Rot(f,C(f));
else Rot(x,C(x));
Rot(x,C(x));
}
}
Up(x);
if(!g)root=x;
}
void Up(int x){Z(x)=Z(L(x))+Z(R(x))+1;}
void Down(int x){/*Do YOLO*/}
void Insert(int &s,int f,int v) {
if(s==0) {
s=++sz;W(s)=v;Z(s)=1;F(s)=f;int x=s;Splay(x,0);
return;
}
Z(s)++;
if(W(s)>v) Insert(L(s),s,v);
else Insert(R(s),s,v);
}
int Search(int s,int v) {
if(s==0)return ERR;
if(W(s)==v)return Splay(s,0),s;
if(W(s)>v)return Search(L(s),v);
return Search(R(s),v);
}
void Join(int l,int r){
int lm=l;if (!l){root=r;F(r)=0; return; }
while(R(lm))lm=R(lm);
Splay(lm,F(l));
R(lm)=r;
F(r)=lm;
F(lm)=0;
Up(lm);
root=lm;
}
int Delete(int s,int v){
int x=Search(s,v);
if(x==ERR)return ERR;
//Splay(x,0);
Join(L(x),R(x));
return 0;
}
int Select(int s,int k) {
int Lt=Z(L(s));
if(k==Lt)return Splay(s,0),W(s);
if(Lt>k)return Select(L(s),k);
return Select(R(s),k-Lt-1);
}
int Rank(int s,int v) {
int Lt=Z(L(s));
if(W(s)==v)return Splay(s,0),Lt;
if(W(s)>v) return Rank(L(s),v);
return Rank(R(s),v)+Lt+1;
}
inline bool getint(int &x){
int y,flag;
while(isspace(y=getchar()));
if(y==-1) return false;
if(y=='-') flag=-1,x=0;
else flag=1,x=y-'0';
while(isdigit(y=getchar())) x=x*10+(y-'0');
x*=flag;
return true;
}
int main(){
int c;
int x;
//freopen("SBT.in6","r",stdin);
//freopen("SBT.outsplay6","w",stdout);
while(getint(c)) {
switch(c) {
case 0:
getint(x);
Insert(root,0,x);
break;
case 1:
getint(x);
Delete(root,x);
break;
case 2:
getint(x);
printf("%d\n",Search(root,x));
break;
case 3:
getint(x);
printf("%d\n",Select(root,x)); //x<S(root)
break;
case 4:
getint(x);
printf("%d\n",Rank(root,x)); //Search(x)>0
break;
default:
printf("\n\n\n");
}
}
return 0;
}