#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int maxbit = 18;
set<int> arr[maxn];
int q, u, x, k, s, t;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  while (q--) {
    cin >> t;
    if (t == 1) {
      cin >> u;
      for (int i = 1; i <= sqrt(u); i++)
        if (u % i == 0) {
          arr[i].insert(u);
          arr[u / i].insert(u);
        }
    } else {
      cin >> x >> k >> s;
      if (x % k || arr[k].upper_bound(s - x) == arr[k].begin()) {
        cout << -1 << endl;
        continue;
      }
      int ans = 0;
      for (int i = 18; ~i; i--) {
        int f = 1 << i;
        if (f & x) {
          auto it = arr[k].lower_bound(ans);
          if (it != arr[k].end() && *it <= s - x && *it < (ans ^ f)) {
            continue;
          } else
            ans ^= f;
        } else {
          auto it = arr[k].lower_bound(ans ^ f);
          if (it != arr[k].end() && *it <= s - x && *it < (ans + f + f)) {
            ans ^= f;
          } else
            continue;
        }
      }
      cout << ans << endl;
    }
  }
}
