#include<iostream>
using namespace std;
struct node {
int data;
node *next;
};
node *head, *p, *r, *s, *t;
int n, x, y;
bool cz(int x, node*& prev) {
prev = NULL;
t = head;
while (t != NULL && t->data != x) {
prev = t;
t = t->next;
}
return t != NULL;
}
int main() {
head = new node;
head->next = NULL;
r = head;
cin >> n;
p = new node;
p->data = 1;
p->next = NULL;
head->next = p;
for (int i = 1; i <= n; i++) {
cin >> x;
if (x == 1) {
cin >> x >> y;
node* prev = NULL;
if (cz(x, prev)) {
s = new node;
s->data = y;
s->next = prev->next->next;
prev->next->next = s;
}
} else if (x == 2) {
cin >> x;
node* prev = NULL;
if (cz(x, prev) && prev->next->next != NULL) {
cout << prev->next->next->data << endl;
} else {
cout << 0 << endl;
}
} else if (x == 3) {
cin >> x;
node* prev = NULL;
if (cz(x, prev) && prev->next->next != NULL) {
node* toDelete = prev->next->next;
prev->next->next = toDelete->next;
delete toDelete;
}
}
}
return 0;
}
