#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <iomanip>
#include <cstring>
#include <ctime>
#include <unordered_map>
using namespace std;
const int o = 1e7 + 10;
int head, e[o], ne[o],idx;//链表是乱序的,也就是说head指向的是第一个数,但是head不一定在第一位
//head 头结点下标
//e[i] 第i个节点的值
//ne[i] 第i个节点的下一个节点的索引
//idx 储存当前用到了哪个点
void num()
{
head = 1;
idx = 1;
}
void add(int y, int x)
{
e[idx] = y;
ne[idx] = ne[x];
ne[x] = idx++;
}
void next(int x)
{
if (ne[x] >= idx)
{
cout << 0 << endl;
}
cout << e[ne[x]] << endl;
}
void remove(int k)
{
ne[k] = ne[ne[k]];
}
int main()
{
num();//初始化链表
int q;
cin >> q;
int x, y;
while (q--)
{
int t;
cin >> t;
if (t == 1)
{
cin >> x >> y;
add(y, x);
}
else if (t == 2)
{
cin >> x;
next(x);
}
else
{
cin >> x;
remove(x);
}
}
}