#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#define N 100010
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long LL;
typedef long double LD;
int n;
struct Splay
{
  //tp 0-lc 1-rc
  int lc[3*N],rc[3*N],tp[3*N],s[3*N],fa[3*N],key[3*N],ct[3*N];
  int root,len,cnt,inf;
  void init(){root=len=cnt=0;inf=2e9;}
  void lt(int x)
  {
    int y=fa[x];tp[x]=tp[y];fa[x]=fa[y];tp[y]=1;fa[y]=x;
    s[x]=s[y];s[y]=s[rc[y]]+s[rc[x]]+ct[y];
    if (rc[x]!=0) {fa[rc[x]]=y;tp[rc[x]]=0;}
    lc[y]=rc[x];rc[x]=y;
    if (fa[x]==0) root=x;
  }
  void rt(int x)
  {
    int y=fa[x];tp[x]=tp[y];fa[x]=fa[y];tp[y]=0;fa[y]=x;
    s[x]=s[y];s[y]=s[lc[y]]+s[lc[x]]+ct[y];
    if (lc[x]!=0) {fa[lc[x]]=y;tp[lc[x]]=1;}
    rc[y]=lc[x];lc[x]=y;
    if (fa[x]==0) root=x;
  }
  void splay(int x)
  {
    int t=x;
    while (t!=root)
    {
      if (tp[t]==0) lt(t);else rt(t);
    }
  }
  void Ins(int x,int Key)
  {
    if (root==0) {cnt++;key[++len]=Key;fa[len]=0;root=len;s[root]=1;ct[root]=1;return;}
    s[x]++;
    if (Key<key[x])
    {
      if (lc[x]==0) 
      {
        lc[x]=++len;fa[lc[x]]=x;s[lc[x]]=1;tp[lc[x]]=0;key[lc[x]]=Key;ct[lc[x]]=1;
        splay(lc[x]);cnt++;
      }
      else Ins(lc[x],Key);
    }else
    {
      if (rc[x]==0) 
      {
        rc[x]=++len;fa[rc[x]]=x;s[rc[x]]=1;tp[rc[x]]=1;key[rc[x]]=Key;ct[rc[x]]=1;
        splay(rc[x]);cnt++;
      }
      else Ins(rc[x],Key);
    }
  }
  void ins(int Key)
  {
    int pos=find(root,Key);
    if (pos==inf) Ins(root,Key);
    else
    {
      splay(pos);ct[pos]++;s[pos]++;
    }
  }
  int select(int x,int ks)
  {
    int ls=s[lc[x]];
    if (ks<=ls) return select(lc[x],ks);
    if (ks<=ls+ct[x]) return x;
    return select(rc[x],ks-ls-ct[x]);
  }
  int find(int x,int Key)
  {
    if (x==0) return inf;
    if (Key==key[x]) {splay(x);return x;}
    if (Key<key[x]) 
    {
      if (lc[x]==0) return inf;
      return find(lc[x],Key);
    }else
    {
      if (rc[x]==0) return inf;
      return find(rc[x],Key);
    }
  }
  int getmin(int x)
  {
    if (lc[x]==0) {splay(x);return x;}
    return getmin(lc[x]);
  }
  int getmax(int x)
  {
    if (rc[x]==0) {splay(x);return x;}
    return getmax(rc[x]);
  }
  int Del(int Key)
  {
    int x=find(root,Key);
    if (x==inf) return 0;cnt--;
    if (cnt==0) {root=0;return 1;}
    splay(x);
    if (lc[x]==0) root=rc[x];
    else
    {
      int maxpos=getmax(lc[x]);
      splay(x);
      root=lc[x];int Rroot=rc[x];
      splay(maxpos);s[root]+=s[Rroot];
      fa[Rroot]=root;rc[root]=Rroot;
    }
    fa[root]=0;
  }
  void del(int Key)
  {
    int pos=find(root,Key);
    if (ct[pos]==1) Del(Key);
    else
    {
      splay(pos);ct[pos]--;s[pos]--;cnt--;
    }
  }
  int rank(int x,int Key)
  {
    if (Key==key[x]) return s[lc[x]]+1;
    if (Key<key[x]) return rank(lc[x],Key);
    if (Key>key[x]) return rank(rc[x],Key)+s[lc[x]]+ct[x];
  }
}tree;
int main()
{
  ios::sync_with_stdio(false);
  scanf("%d",&n);int i,op,num;
  FOR(i,1,n)
  {
    scanf("%d%d",&op,&num);
    if (op==1)
    {
      tree.ins(num);
    }
    if (op==2)
    {
      tree.del(num);
    }
    if (op==4)
    {
      printf("%d\n",tree.key[tree.select(tree.root,num)]);
    }
    if (op==3) 
    {
      printf("%d\n",tree.rank(tree.root,num));
    }
    if (op==6)
    {
      tree.ins(num);
      int pos=tree.find(tree.root,num);
      pos=tree.getmin(tree.rc[pos]);
      printf("%d\n",tree.key[pos]);
      tree.del(num);
    }
    if (op==5)
    {
      tree.ins(num);
      int pos=tree.find(tree.root,num);
      pos=tree.getmax(tree.lc[pos]);
      printf("%d\n",tree.key[pos]);
      tree.del(num);
    }
  }
  return 0;
}