#include <bits/stdc++.h>
using namespace std;
struct node{
int v;
node *next;
};
int main(){
node *head=new node({1,0});
int n,x,y,z;
cin>>n;
for(int i=1;i<=n;i++){
cin>>x>>y;
if(x==1){
cin>>z;
node *p=head;
while(p->v!=y&&p!=NULL){
p=p->next;
}
node *s=new node({z,p->next});
p->next=s;
}else if(x==2){
node *p=head;
while(p->v!=y&&p!=NULL){
p=p->next;
}
if(p->next==0){
cout<<0;
}else{
cout<<p->next->v;
}
cout<<'\n';
}else{
node *p=head;
while(p->v!=y&&p!=NULL){
p=p->next;
}
p->next=p->next->next;
}
}
return 0;
}