#include <bits/stdc++.h>
using namespace std;
list<int> l;
int n;
int main(){
cin >> n;
l.push_back(1);
for(int i = 0;i < n;i++){
int x; cin >> x;
if(x == 1){
int a, b; cin >> a >> b;
list<int>::iterator it = l.begin();
for(;it != l.end();it++)
if(*it == a){ it++; l.insert(it, b);}
}
else if(x == 2){
int a; cin >> a;
list<int>::iterator it = l.begin();
for(;it != l.end();it++){
if(*it == a){
it++;
if(it == l.end()) cout << 0 << endl;
else cout << *it << endl;
break;
}
}
}
else{
int a; cin >> a;
list<int>::iterator it = l.begin();
for(;it != l.end();it++)
if(*it == a){
it++;
l.erase(it);
break;
}
}
}
return 0;
}