#include <bits/stdc++.h>
using namespace std;
const int logn = 20, maxn = logn << logn;
int root[maxn], to[maxn][2], cnt[maxn];
int sz = 1, rt = 1;
void add(int x)
{
int cur = root[rt] = sz++;
memcpy(to[cur], to[root[rt - 1]], sizeof(to[cur]));
cnt[cur] = cnt[root[rt - 1]];
rt++;
cnt[cur]++;
for(int i = logn - 1; i >= 0; i--)
{
bool c = x & (1 << i);
if(to[cur][c])
{
memcpy(to[sz], to[to[cur][c]], sizeof(to[sz]));
cnt[sz] = cnt[to[cur][c]];
}
cur = to[cur][c] = sz++;
cnt[cur]++;
}
}
void remove(int k)
{
rt -= k;
}
int xor_max(int l, int r, int x)
{
int a = root[l], b = root[r];
int y = 0;
for(int i = logn - 1; i >= 0; i--)
{
bool c = !(x & (1 << i));
if(cnt[to[a][c]] < cnt[to[b][c]])
{
y |= c << i;
a = to[a][c];
b = to[b][c];
}
else if(cnt[to[a][!c]] < cnt[to[b][!c]])
{
y |= (!c) << i;
a = to[a][!c];
b = to[b][!c];
}
else break;
}
return y;
}
int order(int l, int r, int x)
{
int a = root[l], b = root[r];
int ans = 0;
for(int i = logn - 1; i >= 0; i--)
{
bool c = x & (1 << i);
if(c) ans += cnt[to[b][0]] - cnt[to[a][0]];
a = to[a][c];
b = to[b][c];
}
ans += cnt[b] - cnt[a];
return ans;
}
int kth_stat(int l, int r, int k)
{
int a = root[l], b = root[r];
int ans = 0;
for(int i = logn - 1; i >= 0; i--)
{
if(k > cnt[to[b][0]] - cnt[to[a][0]])
{
k -= cnt[to[b][0]] - cnt[to[a][0]];
ans |= 1 << i;
a = to[a][1];
b = to[b][1];
}
else
{
a = to[a][0];
b = to[b][0];
}
}
return ans;
}
main()
{
//freopen("input.txt", "r", stdin);
ios::sync_with_stdio(0);
cin.tie(0);
int m;
cin >> m;
while(m--)
{
int type;
cin >> type;
if(type == 0)
{
int x;
cin >> x;
add(x);
}
else if(type == 1)
{
int l, r, x;
cin >> l >> r >> x;
cout << xor_max(l - 1, r, x) << "\n";
}
else if(type == 2)
{
int k;
cin >> k;
remove(k);
}
else if(type == 3)
{
int l, r, x;
cin >> l >> r >> x;
cout << order(l - 1, r, x) << "\n";
}
else
{
int l, r, k;
cin >> l >> r >> k;
cout << kth_stat(l - 1, r, k) << "\n";
}
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IGxvZ24gPSAyMCwgbWF4biA9IGxvZ24gPDwgbG9nbjsKCmludCByb290W21heG5dLCB0b1ttYXhuXVsyXSwgY250W21heG5dOwppbnQgc3ogPSAxLCBydCA9IDE7Cgp2b2lkIGFkZChpbnQgeCkKewogICAgaW50IGN1ciA9IHJvb3RbcnRdID0gc3orKzsKICAgIG1lbWNweSh0b1tjdXJdLCB0b1tyb290W3J0IC0gMV1dLCBzaXplb2YodG9bY3VyXSkpOwogICAgY250W2N1cl0gPSBjbnRbcm9vdFtydCAtIDFdXTsKICAgIHJ0Kys7CiAgICBjbnRbY3VyXSsrOwogICAgZm9yKGludCBpID0gbG9nbiAtIDE7IGkgPj0gMDsgaS0tKQogICAgewogICAgICAgIGJvb2wgYyA9IHggJiAoMSA8PCBpKTsKICAgICAgICBpZih0b1tjdXJdW2NdKQogICAgICAgIHsKICAgICAgICAgICAgbWVtY3B5KHRvW3N6XSwgdG9bdG9bY3VyXVtjXV0sIHNpemVvZih0b1tzel0pKTsKICAgICAgICAgICAgY250W3N6XSA9IGNudFt0b1tjdXJdW2NdXTsKICAgICAgICB9CiAgICAgICAgY3VyID0gdG9bY3VyXVtjXSA9IHN6Kys7CiAgICAgICAgY250W2N1cl0rKzsKICAgIH0KfQoKdm9pZCByZW1vdmUoaW50IGspCnsKICAgIHJ0IC09IGs7Cn0KCmludCB4b3JfbWF4KGludCBsLCBpbnQgciwgaW50IHgpCnsKICAgIGludCBhID0gcm9vdFtsXSwgYiA9IHJvb3Rbcl07CiAgICBpbnQgeSA9IDA7CiAgICBmb3IoaW50IGkgPSBsb2duIC0gMTsgaSA+PSAwOyBpLS0pCiAgICB7CiAgICAgICAgYm9vbCBjID0gISh4ICYgKDEgPDwgaSkpOwogICAgICAgIGlmKGNudFt0b1thXVtjXV0gPCBjbnRbdG9bYl1bY11dKQogICAgICAgIHsKICAgICAgICAgICAgeSB8PSBjIDw8IGk7CiAgICAgICAgICAgIGEgPSB0b1thXVtjXTsKICAgICAgICAgICAgYiA9IHRvW2JdW2NdOwogICAgICAgIH0KICAgICAgICBlbHNlIGlmKGNudFt0b1thXVshY11dIDwgY250W3RvW2JdWyFjXV0pCiAgICAgICAgewogICAgICAgICAgICB5IHw9ICghYykgPDwgaTsKICAgICAgICAgICAgYSA9IHRvW2FdWyFjXTsKICAgICAgICAgICAgYiA9IHRvW2JdWyFjXTsKICAgICAgICB9CiAgICAgICAgZWxzZSBicmVhazsKICAgIH0KICAgIHJldHVybiB5Owp9CgppbnQgb3JkZXIoaW50IGwsIGludCByLCBpbnQgeCkKewogICAgaW50IGEgPSByb290W2xdLCBiID0gcm9vdFtyXTsKICAgIGludCBhbnMgPSAwOwogICAgZm9yKGludCBpID0gbG9nbiAtIDE7IGkgPj0gMDsgaS0tKQogICAgewogICAgICAgIGJvb2wgYyA9IHggJiAoMSA8PCBpKTsKICAgICAgICBpZihjKSBhbnMgKz0gY250W3RvW2JdWzBdXSAtIGNudFt0b1thXVswXV07CiAgICAgICAgYSA9IHRvW2FdW2NdOwogICAgICAgIGIgPSB0b1tiXVtjXTsKICAgIH0KICAgIGFucyArPSBjbnRbYl0gLSBjbnRbYV07CiAgICByZXR1cm4gYW5zOwp9CgppbnQga3RoX3N0YXQoaW50IGwsIGludCByLCBpbnQgaykKewogICAgaW50IGEgPSByb290W2xdLCBiID0gcm9vdFtyXTsKICAgIGludCBhbnMgPSAwOwogICAgZm9yKGludCBpID0gbG9nbiAtIDE7IGkgPj0gMDsgaS0tKQogICAgewogICAgICAgIGlmKGsgPiBjbnRbdG9bYl1bMF1dIC0gY250W3RvW2FdWzBdXSkKICAgICAgICB7CiAgICAgICAgICAgIGsgLT0gY250W3RvW2JdWzBdXSAtIGNudFt0b1thXVswXV07CiAgICAgICAgICAgIGFucyB8PSAxIDw8IGk7CiAgICAgICAgICAgIGEgPSB0b1thXVsxXTsKICAgICAgICAgICAgYiA9IHRvW2JdWzFdOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBhID0gdG9bYV1bMF07CiAgICAgICAgICAgIGIgPSB0b1tiXVswXTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gYW5zOwp9CgogbWFpbigpCiB7CiAgICAgLy9mcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKTsKICAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgICBjaW4udGllKDApOwogICAgIGludCBtOwogICAgIGNpbiA+PiBtOwogICAgIHdoaWxlKG0tLSkKICAgICB7CiAgICAgICAgIGludCB0eXBlOwogICAgICAgICBjaW4gPj4gdHlwZTsKICAgICAgICAgaWYodHlwZSA9PSAwKQogICAgICAgICB7CiAgICAgICAgICAgICBpbnQgeDsKICAgICAgICAgICAgIGNpbiA+PiB4OwogICAgICAgICAgICAgYWRkKHgpOwogICAgICAgICB9CiAgICAgICAgIGVsc2UgaWYodHlwZSA9PSAxKQogICAgICAgICB7CiAgICAgICAgICAgICBpbnQgbCwgciwgeDsKICAgICAgICAgICAgIGNpbiA+PiBsID4+IHIgPj4geDsKICAgICAgICAgICAgIGNvdXQgPDwgeG9yX21heChsIC0gMSwgciwgeCkgPDwgIlxuIjsKICAgICAgICAgfQogICAgICAgICBlbHNlIGlmKHR5cGUgPT0gMikKICAgICAgICAgewogICAgICAgICAgICAgaW50IGs7CiAgICAgICAgICAgICBjaW4gPj4gazsKICAgICAgICAgICAgIHJlbW92ZShrKTsKICAgICAgICAgfQogICAgICAgICBlbHNlIGlmKHR5cGUgPT0gMykKICAgICAgICAgewogICAgICAgICAgICAgaW50IGwsIHIsIHg7CiAgICAgICAgICAgICBjaW4gPj4gbCA+PiByID4+IHg7CiAgICAgICAgICAgICBjb3V0IDw8IG9yZGVyKGwgLSAxLCByLCB4KSA8PCAiXG4iOwogICAgICAgICB9CiAgICAgICAgIGVsc2UKICAgICAgICAgewogICAgICAgICAgICAgaW50IGwsIHIsIGs7CiAgICAgICAgICAgICBjaW4gPj4gbCA+PiByID4+IGs7CiAgICAgICAgICAgICBjb3V0IDw8IGt0aF9zdGF0KGwgLSAxLCByLCBrKSA8PCAiXG4iOwogICAgICAgICB9CiAgICAgfQogfQ==