#include <bits/extc++.h>
#define lowbit(x) (x) & -(x)
const int maxn = 2e7 + 7;
const int p = 1e7;
using namespace std;
int n;
int bit[maxn];
auto add(int pos, int x) -> void
{
while (pos < maxn)
{
bit[pos] += x;
pos += lowbit(pos);
}
}
auto query(int pos) -> int
{
int res = 0;
while (pos > 0)
{
res += bit[pos];
pos -= lowbit(pos);
}
return res;
}
auto rankof(int x) -> int
{
return query(x - 1) + 1;
}
auto getrank(int x) -> int
{
int t = 0;
for (int i = 25; i >= 0; i--)
{
t += (1 << i);
if (t > n || bit[t] >= x)
{
t -= (1 << i);
}
else
{
x -= bit[t];
}
}
return bit[t + 1];
}
auto main(int argc, char *args[]) -> int
{
cin >> n;
while (n--)
{
int op, x;
cin >> op >> x;
x += p;
if (op == 1)
{
add(x, 1);
}
else if (op == 2)
{
add(x, -1);
}
else if (op == 3)
{
cout << rankof(x) << endl;
}
else if (op == 4)
{
cout << getrank(x) << endl;
}
else if (op == 5)
{
cout << getrank(rankof(x) - 1) << endl;
}
else if (op == 6)
{
cout << getrank(rankof(x) + 1) << endl;
}
}
}