#include<bits/stdc++.h>
using namespace std;
int n;
int next[1000001];
void insert(int x,int y){
next[y]=next[x];
next[x]=y;
}
void remove(int x){
next[x]=next[next[x]];
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=0;i<n;i++){
int s,x,y;
cin>>s;
if(s==1){
cin>>x>>y;
insert(x,y);
}
if(s==2){
cin>>x;
cout<<next[x]<<endl;
}
if(s==3){
cin>>x;
remove(x);
}
}
}