#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
set<int> boi[maxn], s;
vector<int> p[maxn];
int q;
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  int MAX = 1e5;
  for (int i = 1; i <= MAX; i++)
    for (int j = i; j <= MAX; j += i) p[j].push_back(i);
  while (q--) {
    int type;
    cin >> type;
    if (type == 1) {
      int u;
      cin >> u;
      if (s.find(u) == s.end()) {
        for (int i = 0; i < p[u].size(); i++) boi[p[u][i]].insert(u);
        s.insert(u);
      }
    } else {
      int x, k, s, res = -1, kq = 0;
      cin >> x >> k >> s;
      set<int>::iterator it = boi[k].upper_bound(s - x);
      if (it == boi[k].begin() || x % k) {
        cout << -1 << endl;
        continue;
      }
      while (it != boi[k].begin()) {
        it--;
        int v = *it;
        if (res < (v ^ x)) {
          res = v ^ x;
          kq = v;
        }
        if (v + x < res) break;
      }
      cout << kq << endl;
    }
  }
  return 0;
}
