#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long N = 1e5 + 1;
const long long mod = 1e9 + 7;
const long double eps = 1E-7;
struct Snode {
  int val;
  Snode *b[2];
  Snode() {
    val = INF;
    b[0] = b[1] = nullptr;
  }
} * t[N];
char a[N];
int n, x, k, s;
vector<int> g[N];
void ins(int v, int x) {
  Snode *cur = t[v];
  cur->val = min(x, cur->val);
  for (int i = 18; i >= 0; i--) {
    if (cur->b[x >> i & 1] == nullptr) cur->b[x >> i & 1] = new Snode();
    cur = cur->b[x >> i & 1];
    cur->val = min(x, cur->val);
  }
}
int get() {
  int res = 0;
  Snode *cur = t[k];
  if (cur->val > s) return -1;
  for (int i = 18; i >= 0; i--) {
    int bit = x >> i & 1;
    if (cur->b[bit ^ 1] != nullptr && cur->b[bit ^ 1]->val <= s)
      res += (bit ^ 1) << i, cur = cur->b[bit ^ 1];
    else
      res += bit << i, cur = cur->b[bit];
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  for (int i = 1; i < N; i++) {
    for (int j = i; j < N; j += i) g[j].push_back(i);
    t[i] = new Snode();
  }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (x == 1) {
      cin >> x;
      if (!a[x]) {
        a[x] = 1;
        for (auto h : g[x]) ins(h, x);
      }
    } else {
      cin >> x >> k >> s;
      if (x % k != 0) {
        cout << "-1\n";
        continue;
      }
      s -= x;
      cout << get() << endl;
    }
  }
}
