#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int SQR = 401;
bitset<MAXN> elements;
class trie {
  vector<int> div[MAXN + 10];
  struct node {
    bitset<SQR + 10> present;
    node *nxt[2];
    void add(int val) { present[val] = 1; }
    node() { nxt[0] = nxt[1] = nullptr; }
  };
  void pre_process() {
    for (int i = 1; i < SQR; ++i) {
      for (int j = i; j < MAXN; j += i) {
        div[j].push_back(i);
      }
    }
  }
  node *root;
  void add(node *cur, int super_val, int to_ins, int bt) {
    if (bt < 0) {
      cur->present[to_ins] = 1;
      return;
    }
    assert(cur != nullptr);
    cur->present[to_ins] = 1;
    int ele = (super_val & (1 << bt)) ? 1 : 0;
    if (cur->nxt[ele] == nullptr) {
      node *aux = new node();
      cur->nxt[ele] = aux;
    }
    add(cur->nxt[ele], super_val, to_ins, bt - 1);
  }
  bool has(node *to_see, int val) {
    if (to_see == nullptr) return false;
    return to_see->present[val];
  }
  int search(node *cur, int k, int sum, int x, int carry, int bt) {
    if (bt < 0) return carry;
    assert(cur != nullptr);
    if (cur->present[k] == 0) return -1;
    if ((carry | (1 << bt)) > sum) {
      if (has(cur->nxt[0], k))
        return search(cur->nxt[0], k, sum, x, carry, bt - 1);
      else
        return -1;
    }
    int first_try = ((1 << bt) & x) ? 0 : 1;
    int ans = -1;
    if ((carry | (first_try * (1 << bt))) <= sum &&
        has(cur->nxt[first_try], k)) {
      ans = search(cur->nxt[first_try], k, sum, x,
                   (carry | (first_try * (1 << bt))), bt - 1);
    }
    first_try ^= 1;
    if (ans == -1 && (carry | (first_try * (1 << bt))) <= sum &&
        has(cur->nxt[first_try], k)) {
      ans = search(cur->nxt[first_try], k, sum, x,
                   (carry | (first_try * (1 << bt))), bt - 1);
    }
    return ans;
  }

 public:
  trie() {
    root = new node();
    pre_process();
  }
  void add(int val) {
    for (auto &d : div[val]) {
      add(root, val, d, 17);
    }
  }
  int search(int k, int sum, int x) { return search(root, k, sum, x, 0, 17); }
};
int q;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> q;
  trie base;
  for (int que = 0; que < q; ++que) {
    int type;
    cin >> type;
    if (type == 1) {
      int u;
      cin >> u;
      base.add(u);
      elements[u] = 1;
    } else {
      int x, k, s;
      cin >> x >> k >> s;
      if (x % k != 0) {
        cout << -1 << '\n';
        continue;
      }
      if (k >= SQR) {
        int ans = -1;
        int xor_ans = -1;
        for (int i = 1; i * k <= s - x; ++i) {
          if (elements[i * k] && xor_ans < (x ^ (i * k))) {
            xor_ans = x ^ (i * k);
            ans = i * k;
          }
        }
        cout << ans << '\n';
        continue;
      }
      cout << base.search(k, s - x, x) << '\n';
    }
  }
}
