#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int max_n = 5e5 + 5;
struct Fhq {
int ch[2], val, key, sze;
} fhq[max_n * 50];
int tot = 0, root[max_n];
inline int New_Code(int val) {
int ans = ++tot;
fhq[ans].ch[0] = fhq[ans].ch[1] = 0;
fhq[ans].val = val;
fhq[ans].key = rand();
fhq[ans].sze = 1;
return ans;
}
inline int Copy_Code(int x) {
int ans = ++tot;
fhq[ans] = fhq[x];
return ans;
}
inline void Update(int x) {
fhq[x].sze = fhq[fhq[x].ch[0]].sze + fhq[fhq[x].ch[1]].sze + 1;
}
void Split(int p, int val, int &x, int &y) {
if (p == 0) {
x = y = 0;
return;
}
if (fhq[p].val <= val) {
x = Copy_Code(p);
Split(fhq[p].ch[1], val, fhq[x].ch[1], y);
Update(x);
} else {
y = Copy_Code(p);
Split(fhq[p].ch[0], val, x, fhq[y].ch[0]);
Update(y);
}
}
//Merge可以不复制节点
int Merge(int x, int y) {
if (x == 0 || y == 0)return x + y;
if (fhq[x].key < fhq[y].key) {
x = Copy_Code(x);//可删除
fhq[x].ch[1] = Merge(fhq[x].ch[1], y);
Update(x);
return x;
} else {
y = Copy_Code(y);//可删除
fhq[y].ch[0] = Merge(x, fhq[y].ch[0]);
Update(y);
return y;
}
}
inline int Insert(int p, int val) {
int np = New_Code(val);
int x = 0, y = 0;
Split(p, val, x, y);
return Merge(x, Merge(np, y));
}
inline int Delete(int p, int val) {
int x = 0, y = 0, z = 0;
Split(p, val - 1, x, y);
Split(y, val, y, z);
if (y != 0)y = Merge(fhq[y].ch[0], fhq[y].ch[1]);
return Merge(x, Merge(y, z));
}
int Get_Min_Num(int p, int val) {
if (p == 0)return 0;
if (fhq[p].val >= val)return Get_Min_Num(fhq[p].ch[0], val);
else return fhq[fhq[p].ch[0]].sze + 1 + Get_Min_Num(fhq[p].ch[1], val);
}
int Get_Rank(int p, int x) {
if (p == 0)return 0x3f3f3f3f;
int num = fhq[fhq[p].ch[0]].sze + 1;
if (num < x)return Get_Rank(fhq[p].ch[1], x - num);
else if (num > x)return Get_Rank(fhq[p].ch[0], x);
else return fhq[p].val;
}
int Get_Pre(int p, int val) {
if (p == 0)return -((1ll << 31) - 1);
if (fhq[p].val < val)return max(fhq[p].val, Get_Pre(fhq[p].ch[1], val));
else return Get_Pre(fhq[p].ch[0], val);
}
int Get_Suf(int p, int val) {
if (p == 0)return (1ll << 31) - 1;
if (fhq[p].val > val)return min(fhq[p].val, Get_Suf(fhq[p].ch[0], val));
else return Get_Suf(fhq[p].ch[1], val);
}
signed main() {
// freopen("in", "r", stdin), freopen("out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int t, op, val;
scanf("%d %d %d", &t, &op, &val);
root[i] = root[t];
if (op == 1)root[i] = Insert(root[i], val);
if (op == 2)root[i] = Delete(root[i], val);
if (op == 3)printf("%d\n", Get_Min_Num(root[i], val) + 1);
if (op == 4)printf("%d\n", Get_Rank(root[i], val));
if (op == 5)printf("%d\n", Get_Pre(root[i], val));
if (op == 6)printf("%d\n", Get_Suf(root[i], val));
}
return 0;
}