#include<iostream>
#include<cstdio>
#include<cstdlib>
#define MAXN 500010
using namespace std;
int read(){
int f=1,x=0;char c;
do{c=getchar();if(c=='-')f=-1;}while(c<'0'||c>'9');
do{x=x*10+c-'0';c=getchar();}while(c>='0'&&c<='9');
return f*x;
}
int rt[MAXN],sz[MAXN<<5],x,y,z,tot,val[MAXN<<5],dat[MAXN<<5],ch[MAXN<<5][2];
int newnode(int x){sz[++tot]=1;val[tot]=x;dat[tot]=rand();return tot;}
void pushup(int x){sz[x]=1+sz[ch[x][0]]+sz[ch[x][1]];}
int clone(int k){
sz[++tot]=sz[k];val[tot]=val[k];dat[tot]=dat[k];
ch[tot][0]=ch[k][0];ch[tot][1]=ch[k][1];return tot;
}
void split(int cur,int k,int &x,int &y){
if(!cur){x=y=0;return;}
cur=clone(cur);
if(val[cur]<=k){x=cur;split(ch[cur][1],k,ch[cur][1],y);}
else{y=cur;split(ch[cur][0],k,x,ch[cur][0]);}
pushup(cur);
}
int merge(int x,int y){
if(!x||!y)return x+y;
if(dat[x]>dat[y]){ch[x][1]=merge(ch[x][1],y);pushup(x);return x;}
else{ch[y][0]=merge(x,ch[y][0]);pushup(y);return y;}
}
int kth(int cur,int k){
while(true){
if(k<=sz[ch[cur][0]])cur=ch[cur][0];
else if(k==sz[ch[cur][0]]+1)return cur;
else{k-=sz[ch[cur][0]]+1;cur=ch[cur][1];}
}
}
int main(){
#ifdef WINE
freopen("data.in","r",stdin);
#endif
int n;n=read();
for(int i=1;i<=n;i++){
int r,k,a;r=read();k=read();a=read();
rt[i]=rt[r];
if(k==1){
split(rt[i],a,x,y);
rt[i]=merge(merge(x,newnode(a)),y);
}
if(k==2){
split(rt[i],a,x,z);
split(x,a-1,x,y);
y=merge(ch[y][0],ch[y][1]);
rt[i]=merge(merge(x,y),z);
}
if(k==3){
split(rt[i],a-1,x,y);
printf("%d\n",sz[x]+1);
rt[i]=merge(x,y);
}
if(k==4)printf("%d\n",val[kth(rt[i],a)]);
if(k==5){
split(rt[i],a-1,x,y);
if(!x) printf("%d\n",(1<<31)^1);
else printf("%d\n",val[kth(x,sz[x])]);
rt[i]=merge(x,y);
}
if(k==6){
split(rt[i],a,x,y);
if(!y)printf("%d",~(1<<31));
else printf("%d\n",val[kth(y,1)]);
rt[i]=merge(x,y);
}
}
return 0;
}