rt
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int head, e[N], ne[N], idx, ans[N], i;
//初始化
void init() {
head = -1;
idx = 0;
}
//将x插入到k后面
void add(int k, int x) {
e[idx] = x, ne[idx] = ne[k], ne[k] = idx ++;
}
//删除k后面的一个节点
void remove(int k) {
ne[k] = ne[ne[k]];
}
//询问
void ask(int x) {
ans[i] = e[ne[x]];
i ++;//将答案存到ans中
}
int main() {
int q;
cin >> q;
while(q--) {
int y, x, op;
cin >> op;
if (op == 1) {
cin >> x >> y;
add(x, y);
}
if (op == 2) {
cin >> x;
ask(x);
}
if (op == 3) {
cin >> x;
remove(x);
}
}
for (int j = 0; j < i; j ++ ) {
cout << ans[j] << endl;
}
return 0;
}