#include <bits/stdc++.h>
using namespace std;
set<int> ss[100005];
int main() {
  std::ios::sync_with_stdio(false);
  int t, m, k, v, s, x;
  cin >> t;
  while (t--) {
    cin >> m;
    if (m == 1) {
      cin >> x;
      for (int i = 1; i <= sqrt((double)x); i++) {
        if (x % i == 0) {
          ss[i].insert(x);
          ss[x / i].insert(x);
        }
      }
    } else {
      cin >> x >> k >> s;
      if (x % k != 0) {
        cout << -1 << endl;
        continue;
      }
      set<int>::iterator it = ss[k].upper_bound(s - x);
      if (ss[k].empty() || it == ss[k].begin()) {
        cout << -1 << endl;
        continue;
      }
      int ans = -1;
      int num;
      --it;
      for (; it != ss[k].begin(); --it) {
        int v = *it;
        if (ans >= v + x) {
          break;
        }
        if (ans < (v ^ x)) {
          ans = v ^ x;
          num = *it;
        }
      }
      if (ans < (*it ^ x)) {
        num = *it;
        ans = (*it ^ x);
      }
      cout << num << endl;
    }
  }
  return 0;
}
