#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *next;
}a[1000005];
node *p;
int q,n=1;
node* serch(int x){
node *p=&a[1];
while(p->next!=NULL){
if(p->data==x) break;
p=p->next;
}
return p;
}
void insert(int x,int y){
node* p=serch(x);
a[++n]={y, p->next};
p->next=&a[n];
}
void Delete(int x){
node *p=serch(x);
if(p==NULL||p->next==NULL)return;
p->next=p->next->next;
}
int main(){
cin>>q;
a[1]={1,NULL};
int op,x,y;
while(q--){
cin>>op>>x;
if(op==1){
cin>>y;
insert(x, y);
}else if(op==2){
node *p=serch(x);
if(p==NULL||p->next==NULL)x=0;
else x=p->next->data;
cout<<x<<endl;
}else if(op==3){
Delete(x);
}
}
}