#include<bits/stdc++.h>
using namespace std;
int head,idx,e[100001]={1,0},ne[100001];
void a(int x,int y)
{
e[idx]=y;
ne[idx]=ne[x];
ne[x]=idx++;
}
void b(int x)
{
ne[x]=ne[ne[x]];
}
void c()
{
head=-1;
idx=0;
}
int main()
{
int q;
c();
cin>>q;
while (q--)
{
int p,x,y;
cin>>p;
if (p==1)
{
cin>>x>>y;
a(x-1,y);
}
else if (p==2)
{
cin>>x;
if (x==0) cout<<"0\n";
else cout<<e[ne[x-1]]<<endl;
}
else
{
cin>>x;
b(x);
}
}
return 0;
}