#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#pragma GCC target("avx,tune=native")
const int MAXBIT = 16;
using namespace std;
int n, k;
template <class T>
ostream& operator<<(ostream& out, vector<T> v) {
  out << v.size() << '\n';
  for (auto e : v) out << e << ' ';
  return out;
}
int f[100100];
struct node {
  int stop = 0;
  node* p[2];
  node(int stop = 0) { p[0] = p[1] = 0; }
};
struct Trie {
 private:
  int limit;
  void ins(node* k, int nr, int bit) {
    if (bit < 0) {
      k->stop = 1;
      return;
    }
    bool b = (nr & (1 << bit)) != 0;
    if (k->p[b] == 0) k->p[b] = new node();
    ins(k->p[b], nr, bit - 1);
  }
  int que(node* k, int x, int bit, int offset) {
    if (bit < 0) {
      if (k->stop) return offset;
      return -1;
    }
    if (offset + (1 << bit) > limit) {
      if (k->p[0]) return que(k->p[0], x, bit - 1, offset);
      return -1;
    }
    bool b = ((x & (1 << bit)) == 0);
    int r = -1;
    if (k->p[b]) r = que(k->p[b], x, bit - 1, offset + b * (1 << bit));
    if (r == -1 && k->p[1 - b])
      r = que(k->p[1 - b], x, bit - 1, offset + (1 - b) * (1 << bit));
    ;
    return r;
  }

 public:
  node* root = new node();
  void insert(int nr) { ins(root, nr, MAXBIT); }
  int query(int lim, int x) {
    limit = lim;
    return que(root, x, MAXBIT, 0);
  }
} T[100100];
int main() {
  ios_base::sync_with_stdio(false);
  int q, t, x;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> t;
    if (t == 1) {
      cin >> x;
      if (f[x]) continue;
      f[x] = 1;
      for (int j = 1; j * j <= x; j++) {
        if (x % j) continue;
        T[j].insert(x);
        T[x / j].insert(x);
      }
    } else {
      int s;
      cin >> x >> k >> s;
      if (s - x <= 0 || x % k != 0) {
        cout << "-1\n";
        continue;
      }
      cout << T[k].query(s - x, x) << '\n';
    }
  }
}
