#include <bits/stdc++.h>
std::mt19937_64 rng(
    std::chrono::steady_clock::now().time_since_epoch().count());
int myRand(int R) {
  int val = rng() % R;
  assert(val >= 0);
  return val;
}
const long double PI = atan(1.0) * 4;
const int32_t INF32 = 2e9 + 7;
const int64_t INF64 = 3e18;
const int32_t LOG = 21;
int32_t MOD = 1e9 + 7;
using namespace std;
void my_debugger(string second, int LINE_NUM) { cerr << '\n'; }
template <typename start, typename... end>
void my_debugger(string second, int LINE_NUM, start x, end... y) {
  if (second.back() != ',') {
    second += ',';
    cerr << "LINE(" << LINE_NUM << "): ";
  }
  int i = second.find(',');
  cerr << second.substr(0, i) << " = " << x;
  second = second.substr(i + 1);
  if (!second.empty()) cerr << ", ";
  my_debugger(second, LINE_NUM, y...);
}
struct Trie {
  struct node {
    int nxt[2];
    int min1;
    bool leaf;
    node(int x) {
      nxt[0] = nxt[1] = -1;
      min1 = x;
      leaf = false;
    }
  };
  vector<node> nodes;
  Trie() { nodes = vector<node>(1, node(INF32)); }
  void add(int x) {
    int cur = 0;
    for (int i = (int)20; i >= 0; i--) {
      nodes[cur].min1 = min(nodes[cur].min1, x);
      int bit = 0;
      if ((1 << i) & x) bit = 1;
      if (nodes[cur].nxt[bit] == -1) {
        nodes[cur].nxt[bit] = nodes.size();
        nodes.push_back(node(x));
      }
      cur = nodes[cur].nxt[bit];
    }
    nodes[cur].leaf = true;
  }
  int max_xor(int x, int lim) {
    int cur = 0;
    if (nodes[0].min1 > lim) return -1;
    for (int i = (int)20; i >= 0; i--) {
      int bit = 1;
      if ((1 << i) & x) bit = 0;
      int temp = nodes[cur].nxt[bit];
      if (temp == -1 || nodes[temp].min1 > lim) bit ^= 1;
      cur = nodes[cur].nxt[bit];
    }
    return nodes[cur].min1;
  }
};
vector<Trie> trie(100003, Trie());
bool check[100003];
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  int tests = 1;
  for (int ii = 0; ii < (int)tests; ii++) {
    int q;
    cin >> q;
    for (int i = 0; i < (int)q; i++) {
      int type;
      cin >> type;
      if (type == 1) {
        int x;
        cin >> x;
        for (int i = 1; i * i <= x; i++) {
          if (x % i == 0) {
            trie[i].add(x);
            if (i != x / i) trie[x / i].add(x);
          }
        }
        check[x] = true;
      } else {
        int x, k, second;
        cin >> x >> k >> second;
        second -= x;
        if (second <= 0 || x % k != 0) {
          cout << -1 << '\n';
          continue;
        }
        cout << trie[k].max_xor(x, second) << '\n';
      }
    }
  }
  return 0;
}
