#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
vector<int> d[N];
int root[N], t[22000000][3], ptr = 0;
void insrt(int x, int c) {
  for (int i = 16; i >= 0; i--) {
    if (!t[c][(x >> i) & 1]) {
      t[c][(x >> i) & 1] = ++ptr;
    }
    c = t[c][(x >> i) & 1];
    if (!t[c][2])
      t[c][2] = x;
    else
      t[c][2] = min(t[c][2], x);
  }
}
pair<int, int> query(int x, int c, int mx) {
  int sum = 0, xx = 0;
  for (int i = 16; i >= 0; i--) {
    int k = (x >> i) & 1;
    if (t[c][k ^ 1] && t[t[c][k ^ 1]][2] <= mx) {
      xx += (1 << i);
      sum += (k ^ 1) << i;
      c = t[c][k ^ 1];
      continue;
    }
    if (t[c][k] && t[t[c][k]][2] <= mx) {
      sum += (k << i);
      c = t[c][k];
      continue;
    }
    return make_pair(-1, -1);
  }
  return make_pair(xx, sum);
}
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (int i = 1; i < N; i++) {
    for (int j = i; j < N; j += i) {
      d[j].push_back(i);
    }
  }
  for (int i = 1; i < N; i++) root[i] = ++ptr;
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int typ;
    cin >> typ;
    if (typ == 1) {
      int u;
      cin >> u;
      for (int x : d[u]) insrt(u, root[x]);
    } else {
      int x, k, s;
      cin >> x >> k >> s;
      pair<int, int> ans(-1, -1);
      for (int y : d[x])
        if (y % k == 0) {
          ans = max(ans, query(x, root[y], s - x));
        }
      cout << ans.second << "\n";
    }
  }
}
