#include<bits/stdc++.h>
using namespace std;
const int N=0;
const int M=1e6+1;
int n,id,x,y,pre[1000005],nxt[1000005];
void link (int x,int y){
nxt[x]=y;
pre[y]=x;
}
void del (int x){
link(pre[x],nxt[x]);
}
void insert_l (int x,int y){
link(pre[y],x);
link(x,y);
}
void insert_r (int x,int y){
link(x,nxt[y]);
link(y,x);
}
int main (){
cin>>n;
link(N,1);
link(1,M);
while (n--){
cin>>id;
if (id==1){
cin>>x>>y;
link(x,y);
}else if (id==2){
cin>>x;
cout<<nxt[x]<<'\n';
}else{
cin>>x;
del(nxt[x]);
}
}
return 0;
}