#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
using namespace std;
mt19937_64 gen(time(NULL));
long long const MAXN = 1e5 + 5;
struct hasher {
  vector<long long> pw, h;
  long long MOD, p;
  string s;
  void precalc() {
    long long n = (long long)s.size();
    pw.resize(n + 1);
    h.resize(n + 1);
    pw[0] = 1;
    h[0] = s[0];
    for (long long i = 1; i < n; i++) {
      pw[i] = (pw[i - 1] * p) % MOD;
      h[i] = (h[i - 1] * p + s[i]) % MOD;
    }
  }
  long long get(long long l, long long r) {
    long long ret = h[r] - (l ? h[l - 1] * pw[r - l + 1] : 0);
    ret %= MOD;
    if (ret < 0) ret += MOD;
    return ret;
  }
  hasher(string _s, long long _p, long long _MOD) {
    s = _s;
    MOD = _MOD;
    p = _p;
    precalc();
  }
};
struct Node {
  bool term;
  long long mn;
  Node* go[2];
  Node() {
    mn = MAXN;
    go[0] = go[1] = nullptr;
  }
};
Node* trie[MAXN];
void add(long long num, long long x) {
  Node* cur = trie[num];
  cur->mn = min(cur->mn, x);
  for (long long i = 17; i >= 0; i--) {
    long long f = x & (1LL << i);
    f = f > 0;
    if (cur->go[f] == nullptr) {
      cur->go[f] = new Node();
    }
    cur = cur->go[f];
    cur->mn = min(cur->mn, x);
  }
}
vector<long long> divs[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (long long i = 1; i < MAXN; i++) {
    trie[i] = new Node();
    for (long long j = i; j < MAXN; j += i) divs[j].push_back(i);
  }
  long long t;
  cin >> t;
  while (t--) {
    long long q;
    cin >> q;
    if (q == 1) {
      long long u;
      cin >> u;
      for (long long v : divs[u]) {
        add(v, u);
      }
    } else {
      long long s, x, k;
      cin >> x >> k >> s;
      long long mx = s - x;
      Node* cur = trie[k];
      if (x % k || cur->mn > mx) {
        cout << -1 << '\n';
        continue;
      }
      long long ans = 0;
      for (long long i = 17; i >= 0; i--) {
        long long prior = (x & (1LL << i));
        prior = (prior == 0);
        if (cur->go[prior] == nullptr) {
          prior ^= 1;
          cur = cur->go[prior];
        } else if (cur->go[prior]->mn > mx) {
          prior ^= 1;
          cur = cur->go[prior];
        } else {
          cur = cur->go[prior];
          ans |= (long long)(prior) << i;
        }
        ans |= ((long long)(prior)) << i;
      }
      cout << ans << '\n';
    }
  }
  return 0;
}
