#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int INF = 1e9;
const ll LINF = 1e18;
const int MX = 6e6 + 5;
struct Node {
int nxt[2];
int cnt = 0;
ll sum = 0;
int mark = 0;
Node() {
memset(nxt, -1, sizeof nxt);
}
};
int sz;
Node trie[MX];
void addNumber(int x) {
int v = 0;
for (int i = 29; i >= 0; i--) {
int c = (x >> i) & 1;
if (trie[v].nxt[c] == -1) {
trie[v].nxt[c] = ++sz;
}
v = trie[v].nxt[c];
trie[v].cnt++;
trie[v].sum += x;
}
trie[v].mark++;
}
// Kiểm tra số nguyên x có tồn tại trong cây trie hiện tại hay không
bool exist(int x) {
int v = 0;
for (int i = 29; i >= 0; i--) {
int c = (x >> i) & 1;
int nxt_v = trie[v].nxt[c];
if (nxt_v == -1 || trie[nxt_v].cnt == 0) return false;
v = nxt_v;
}
return (trie[v].mark > 0);
}
void removeNumber(int x) {
if (!exist(x)) return;
int v = 0;
for (int i = 29; i >= 0; i--) {
int c = (x >> i) & 1;
v = trie[v].nxt[c];
trie[v].cnt--;
trie[v].sum -= x;
}
trie[v].mark--;
}
// Tính tổng các số nguyên x <= mx có trong cây trie
ll getSum(int mx) {
if (mx < 0) return 0;
int v = 0; ll ans = 0;
for (int i = 29; i >= 0; i--) {
int c_mx = (mx >> i) & 1;
int nxt_v0 = trie[v].nxt[0], nxt_v1 = trie[v].nxt[1];
if (c_mx == 0) {
v = nxt_v0;
}
else {
if (nxt_v0 != -1) ans += trie[nxt_v0].sum;
v = nxt_v1;
}
if (v == -1) break;
}
if (v != -1) ans += trie[v].sum;
return ans;
}
// Tìm số nhỏ thứ k
int findKth(int k) {
int v = 0, ans = 0;
for (int i = 29; i >= 0; i--) {
for (int c = 0; c <= 1; c++) {
int nxt_v = trie[v].nxt[c];
if (nxt_v == -1) continue;
if (k <= trie[nxt_v].cnt) {
v = nxt_v;
ans |= c * (1 << i);
break;
}
else {
k -= trie[nxt_v].cnt;
}
}
}
return ans;
}
// Tìm số nguyên x có trong cây trie sao cho (x ^ a) lớn nhất
int findMaxXor(int a) {
int v = 0, ans = 0;
// Tại mỗi bước, ưu tiên đi vào nhánh cho ra xor bằng 1
for (int i = 29; i >= 0; i--) {
int c_a = (a >> i) & 1;
int nxt_v0 = trie[v].nxt[c_a], nxt_v1 = trie[v].nxt[c_a ^ 1];
if (nxt_v1 != -1 && trie[nxt_v1].cnt > 0) {
v = nxt_v1;
ans |= (1 << i);
}
else {
v = nxt_v0;
}
}
return ans;
}
int q;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> q;
while (q--) {
int type; cin >> type;
if (type == 1) {
int x; cin >> x;
addNumber(x);
}
if (type == 2) {
int x; cin >> x;
removeNumber(x);
}
if (type == 3) {
int l, r;
cin >> l >> r;
ll ans = getSum(r) - getSum(l - 1);
cout << ans << '\n';
}
if (type == 4) {
int k; cin >> k;
int ans = findKth(k);
cout << ans << '\n';
}
if (type == 5) {
int a; cin >> a;
int ans = findMaxXor(a);
cout << ans << '\n';
}
}
}