#include<iostream>
#include<cstdio>
using namespace std;
template<int,int>
int nn(int a,int b){
return 0;
}
struct node{
int data;
node *next;
};
void tc(node*abc,int x,int y){
node*q=new node,*p=abc;
q->data=y;
while(p->data!=x)p=p->next;
q->next=p->next;
p->next=q;
if(p->next!=NULL)p=p->next;
}
int main(){
int a,n,x,y;
node *head = new node,*p;
head->data = 1;
head->next = NULL;
scanf("%d",&a);
for(int i = 1;i <= a;i++){
scanf("%d",&n);p = head;
if(n == 1){
scanf("%d %d",&x,&y);
tc(p,x,y);
}else if(n == 2){
scanf("%d",&x);
while(p->data!=x)p=p->next;
int ans=p->next?p->next->data:0;
printf("%d\n",ans);
}else if(n == 3){
scanf("%d",&x);
while(p->data != x)p = p->next;
p->next = p->next->next;
}
}
return 0;
}