#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int head, idx, e[N], ne[N];
//插入
void init(int x, int y)
{
e[idx] = y;
ne[idx] = ne[x];
ne[x] = idx++;
}
//判断
void find(int x)
{
if (ne[x] == -1)
{
cout << "0" << endl;
}
else
{
cout << e[ne[x]] << endl;
}
}
//删除
void remove(int x)
{
int i = ne[x];
if (i == head)
{
// 如果要删除的是头节点后面的节点,更新头节点
head = ne[i];
}
else if (ne[ne[x]] == head)
{
idx--;
}
else
{
ne[x] = ne[ne[x]];
}
}
int main()
{
head = -1;
idx = 0;
e[idx] = 1, ne[idx] = head, head = idx++;
int n = 0, a = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a;
int x = 0, y = 0;
switch (a)
{
case 1:
cin >> x >> y;
init(x, y);
break;
case 2:
cin >> x;
find(x);
break;
case 3:
cin >> x;
remove(x);
break;
}
}
return 0;
}