#8差1ms超
#include<bits/stdc++.h>
using namespace std;
int n;
struct node {
int s;
node *next;
} *head, *tail, a[1000005];
int main() {
scanf ("%d", &n);
head = tail = NULL;
a[1].next = NULL;
a[1].s = 1;
head = &a[1];
while (n--) {
int op, x, y;
scanf ("%d", &op);
if (op == 1) {
scanf ("%d%d", &x, &y);
for (node *v = head; v; v = v->next) {
if (v->s == x) {
a[y].s = y;
a[y].next = v->next;
v->next = &a[y];
if (v == tail) {
tail = &a[y];
}
break;
}
}
} else if (op == 2) {
scanf ("%d", &x);
for (node *v = head; v; v = v->next) {
if (v->s == x) {
if (v->next){
printf ("%d\n", v->next->s);
}
else{
printf ("0\n");
}
}
}
}
else{
scanf ("%d", &x);
for (node *v = head; v; v = v->next) {
if (v->s == x) {
node *t = v->next;
v->next = v->next->next;
if (t == tail){
tail = v;
}
break;
}
}
}
}
return 0;
}