#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7, M = 24;
int tr[N * M][2], idx, dwtag[N * M];
void insert(int v) {
  int p = 0;
  for (int i = M; i >= 0; --i) {
    int k = v >> i & 1;
    if (!tr[p][k]) tr[p][k] = ++idx;
    p = tr[p][k];
    dwtag[p] = min(v, (dwtag[p] == 0 ? v : dwtag[p]));
  }
}
int query(int x, int limit) {
  int p = 0;
  for (int i = M; i >= 0; --i) {
    int o = tr[p][x >> i & 1], r = tr[p][!(x >> i & 1)];
    if (dwtag[o] + x > limit && dwtag[r] + x > limit) return -1;
    if (dwtag[r] + x <= limit)
      p = r;
    else
      p = o;
  }
  return dwtag[p];
}
int query(int x) {
  int p = 0;
  for (int i = M; i >= 0; --i) {
    p = tr[p][x >> i & 1];
    if (!p || dwtag[p] > x) return 0;
  }
  return p;
}
int main() {
  dwtag[0] = 1e9;
  int q;
  scanf("%d", &q);
  while (q--) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int x;
      scanf("%d", &x);
      insert(x);
    } else {
      int x, k, s;
      scanf("%d%d%d", &x, &k, &s);
      if (x % k)
        puts("-1");
      else {
        if (k == 1)
          printf("%d\n", query(x, s));
        else {
          int resx = -1e9, resv = -1;
          for (int z = k; x + z <= s; z += k)
            if (query(z)) {
              if ((z ^ x) > resx) {
                resx = z ^ x;
                resv = z;
              }
            }
          printf("%d\n", resv);
        }
      }
    }
  }
  return 0;
}
