#include <bits/stdc++.h>
using namespace std;
int x, k, s, n, t, fr[100005];
vector<int> D;
struct trie {
  trie *L, *R;
  int div[101];
  trie() {
    memset(div, 0x3f, sizeof(div));
    L = R = 0;
  }
} * root;
void add(int x) {
  D.clear();
  for (int i = 1; i <= min(x, 100); i++) {
    if (x % i == 0) D.push_back(i);
  }
  trie *nod = root;
  for (int j = 0; j < D.size(); j++) nod->div[D[j]] = min(nod->div[D[j]], x);
  for (int i = 17; i >= 0; i--) {
    if (x & (1 << i)) {
      if (!nod->R) nod->R = new trie();
      nod = nod->R;
    } else {
      if (!nod->L) nod->L = new trie();
      nod = nod->L;
    }
    for (int j = 0; j < D.size(); j++) nod->div[D[j]] = min(nod->div[D[j]], x);
  }
  fr[x]++;
}
void query_easy() {
  int sav = -1;
  s -= x;
  int mx = min(100000, s);
  int ans = 0;
  for (int i = k; i <= mx; i += k) {
    if (fr[i]) {
      if ((i ^ x) >= ans) {
        ans = i ^ x;
        sav = i;
      }
    }
  }
  cout << sav << '\n';
}
void query_hard() {
  int ans = 0;
  s -= x;
  trie *nod = root;
  for (int i = 17; i >= 0; i--) {
    if (!(x & (1 << i))) {
      if (nod->R && nod->R->div[k] <= s)
        nod = nod->R, ans |= (1 << i);
      else if (nod->L && nod->L->div[k] <= s)
        nod = nod->L;
      else {
        cout << "-1\n";
        return;
      }
    } else {
      if (nod->L && nod->L->div[k] <= s)
        nod = nod->L;
      else if (nod->R && nod->R->div[k] <= s)
        nod = nod->R, ans |= (1 << i);
      else {
        cout << "-1\n";
        return;
      }
    }
  }
  if (fr[ans])
    cout << ans << '\n';
  else
    cout << "-1\n";
}
int main() {
  root = new trie();
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> t;
    if (t == 1) {
      cin >> x;
      add(x);
    } else {
      cin >> x >> k >> s;
      if (x % k != 0 || s <= x) {
        cout << -1 << '\n';
        continue;
      }
      if (k >= 100)
        query_easy();
      else
        query_hard();
    }
  }
  return 0;
}
