#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
const int LOG = 18;
const int inf = 1e9 + 10;
struct node {
  int nex[2], val;
  node() {
    nex[0] = nex[1] = -1;
    val = inf;
  }
};
vector<node> v;
vector<int> div2[N];
bool mark[N];
int t[N];
node tmp;
int go(int curr, int t) {
  if (v[curr].nex[t] != -1) return v[curr].nex[t];
  v[curr].nex[t] = v.size();
  v.push_back(tmp);
  return v[curr].nex[t];
}
void add(int curr, int x) {
  v[curr].val = min(v[curr].val, x);
  for (int i = LOG - 1; i >= 0; i--) {
    curr = go(curr, (x >> i) % 2);
    v[curr].val = min(v[curr].val, x);
  }
}
int query(int curr, int x, int maxi, int lev = LOG - 1) {
  if (v[curr].val > maxi) return -1;
  if (lev == -1) return (v[curr].val != inf) ? v[curr].val : -1;
  int R = go(curr, 1);
  int L = go(curr, 0);
  if (v[R].val > maxi) {
    return query(L, x, maxi, lev - 1);
  } else if (v[L].val > maxi) {
    return query(R, x, maxi, lev - 1);
  } else {
    if ((v[L].val ^ x) > (v[R].val ^ x)) {
      return query(L, x, maxi, lev - 1);
    } else {
      return query(R, x, maxi, lev - 1);
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  memset(t, -1, sizeof t);
  int q;
  cin >> q;
  for (int i = 1; i < N; i++) {
    for (int j = i; j < N; j += i) {
      div2[j].push_back(i);
    }
  }
  for (int i = 0, tp, x, k, s, u; i < q; i++) {
    cin >> tp;
    if (tp == 1) {
      cin >> u;
      if (mark[u]) continue;
      mark[u] = true;
      for (auto x : div2[u]) {
        if (t[x] == -1) {
          t[x] = v.size();
          v.push_back(tmp);
        }
        add(t[x], u);
      }
    } else {
      cin >> x >> k >> s;
      if (x % k != 0) {
        cout << "-1\n";
        continue;
      }
      s -= x;
      if (t[k] == -1) {
        t[k] = v.size();
        v.push_back(tmp);
      }
      cout << query(t[k], x, s) << "\n";
    }
  }
  return 0;
}
