#include <bits/stdc++.h>
using namespace std;
struct X {
  X* u[2];
  int mi;
  X() {
    u[0] = u[1] = nullptr;
    mi = 1 << 30;
  }
};
const int N = 123456, B = 17;
X* r[N];
int m[N];
vector<int> e[N];
int i, j, k;
int main() {
  for (i = 1; i < N; i++)
    for (j = i; j < N; j += i) e[j].push_back(i);
  for (i = 0; i < N; i++) r[i] = new X();
  int q;
  scanf("%d", &q);
  while (q--) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int u;
      scanf("%d", &u);
      if (!m[u]) {
        m[u] = 1;
        for (int v : e[u]) {
          X* n = r[v];
          for (int i = B; i >= 0; i--) {
            int b = (u >> i) & 1;
            if (n->u[b] == nullptr) n->u[b] = new X();
            n = n->u[b];
            if (n->mi > u) n->mi = u;
          }
        }
      }
    } else {
      int x, k, s, l;
      scanf("%d %d %d", &x, &k, &s);
      l = s - x + 1;
      if (l <= 1 || x % k)
        printf("-1\n");
      else {
        int r = -1;
        int p = 0;
        X* u = ::r[k];
        for (i = B; i >= 0; i--) {
          int b = (x >> i) & 1;
          for (j = 0; j < 2; j++) {
            b ^= 1;
            if (u->u[b] != nullptr && u->u[b]->mi < l) {
              u = u->u[b];
              p |= b << i;
              break;
            }
          }
          if (j == 2) break;
        }
        if (i == -1) {
          if (r == -1 || (r ^ x) < (p ^ x)) r = p;
        }
        printf("%d\n", r);
      }
    }
  }
  return 0;
}
