#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
int ch[maxn][2], cnt[maxn], tot;
int vis[maxn];
void init() {
  tot = 0;
  memset(ch, -1, sizeof ch);
  memset(cnt, 0, sizeof cnt);
}
int a[20];
void binary(int x) {
  for (int i = 18; i >= 0; i--) {
    if (x & (1 << i)) a[i] = 1;
  }
}
void insert(int x) {
  int p = 0;
  for (int i = 18; i >= 0; i--) {
    if (ch[p][a[i]] == -1) {
      ch[p][a[i]] = ++tot;
    }
    p = ch[p][a[i]];
  }
}
int x, k, s;
bool f;
int v;
void find(int pos, int res, int p) {
  if (f) return;
  if (res + x > s) return;
  if (pos == -1) {
    if (res % k == 0 && res + x <= s) {
      f = 1;
      v = res;
    }
    return;
  }
  if (ch[p][!a[pos]] != -1) {
    find(pos - 1, res + !a[pos] * (1 << pos), ch[p][!a[pos]]);
  }
  if (ch[p][a[pos]] != -1) {
    find(pos - 1, res + a[pos] * (1 << pos), ch[p][a[pos]]);
  }
}
int main() {
  init();
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int op;
    scanf("%d", &op);
    if (op == 1) {
      scanf("%d", &x);
      vis[x] = 1;
      memset(a, 0, sizeof a);
      binary(x);
      insert(x);
    } else {
      scanf("%d%d%d", &x, &k, &s);
      if (x % k != 0) {
        puts("-1");
        continue;
      }
      memset(a, 0, sizeof a);
      binary(x);
      if (k == 1) {
        f = 0;
        find(18, 0, 0);
        if (f)
          printf("%d\n", v);
        else
          puts("-1");
      } else {
        int ans = -1;
        int tmp = -1;
        for (int j = k; j <= s - x; j += k) {
          if (vis[j] && (j ^ x) > ans) {
            ans = j ^ x;
            tmp = j;
          }
        }
        printf("%d\n", tmp);
      }
    }
  }
  return 0;
}
