#include <bits/stdc++.h>
using namespace std;
const int N=1e7+10;
int e[N],ne[N],idx=1,hx=1;
void insert(int x,int y){
e[idx]=y;
ne[idx]=hx;
hx=idx++;
}
void query(int x){
if (ne[x] > idx)
{
cout << 0 << endl;
}
else cout<<e[ne[x]]<<endl;
}
void pop(int x){
ne[x]=ne[ne[x]];
}
int main(){
int q;
cin>>q;
e[0]=1;
ne[0]=hx;
while(q--)
{
int a;
cin>>a;
if(a == 1)
{
int x,y;
cin>>x>>y;
insert(x,y);
}
else if( a == 2)
{
int w;
cin>>w;
query(w);
}
else if(a == 3)
{
int s;
cin>>s;
pop(s);
}
}
return 0;
}