#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5,inf=2147483647;
int tot,root[N];
struct node{
int val,pri,ls,rs,sz;
}tr[N<<6];
int New(int x)
{
++tot;
tr[tot].val=x;
tr[tot].pri=rand();
tr[tot].ls=tr[tot].rs=0;
tr[tot].sz=1;
return tot;
}
void pushup(int p){tr[p].sz=tr[tr[p].ls].sz+tr[tr[p].rs].sz+1;}
void split(int p,int x,int &L,int &R)
{
if(!p)
{
L=R=0;
return ;
}
if(tr[p].val<=x)
{
tr[++tot]=tr[p];
L=tot;
split(tr[tot].rs,x,tr[tot].rs,R);
pushup(L);
}
else
{
tr[++tot]=tr[p];
R=tot;
split(tr[tot].ls,x,L,tr[tot].ls);
pushup(R);
}
}
int merge(int L,int R)
{
if(!L||!R)
return L+R;
if(tr[L].pri<=tr[R].pri)
{
tr[++tot]=tr[L];
tr[tot].rs=merge(tr[tot].rs,R);
pushup(tot);
return tot;
}
else
{
tr[++tot]=tr[R];
tr[tot].ls=merge(L,tr[tot].ls);
pushup(tot);
return tot;
}
}
int kth(int p,int k)
{
if(tr[tr[p].ls].sz+1==k)
return tr[p].val;
if(tr[tr[p].ls].sz+1>k)
return kth(tr[p].ls,k);
return kth(tr[p].rs,k-tr[tr[p].ls].sz-1);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;++i)
{
int v,op,x,L,R,p;
cin>>v>>op>>x;
root[i]=root[v];
if(op==1)
{
split(root[i],x,L,R);
root[i]=merge(merge(L,New(x)),R);
}
if(op==2)
{
split(root[i],x,L,R);
split(L,x-1,L,p);
p=merge(tr[p].ls,tr[p].rs);
root[i]=merge(merge(L,p),R);
}
if(op==3)
{
split(root[i],x-1,L,R);
cout<<tr[L].sz+1<<'\n';
root[i]=merge(L,R);
}
if(op==4)
cout<<kth(root[i],x)<<'\n';
if(op==5)
{
split(root[i],x-1,L,R);
if(!L)
cout<<-inf<<'\n';
else
cout<<kth(L,tr[L].sz)<<'\n';
root[i]=merge(L,R);
}
if(op==6)
{
split(root[i],x,L,R);
if(!R)
cout<<inf<<'\n';
else
cout<<kth(R,1)<<'\n';
root[i]=merge(L,R);
}
}
}