#include <bits/stdc++.h>
using namespace std;
template <class X>
bool mini(X& a, const X& b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class X>
bool maxi(X& a, const X& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef const int& ci;
struct trie {
  trie* child[2];
  int Min;
  trie() {
    child[0] = child[1] = nullptr;
    Min = 1 << 30;
  }
  void insert(int x) {
    auto cur = this;
    mini(this->Min, x);
    for (int i = 17; i >= (0); --i) {
      auto& nxt = cur->child[(x >> i & 1)];
      if (nxt == nullptr) nxt = new trie;
      cur = nxt;
      mini(cur->Min, x);
    }
  }
  int max_xor(int x, int high) {
    auto cur = this;
    if (cur->Min > high) return -1;
    int res = 0;
    for (int i = 17; i >= (0); --i) {
      bool k = (x >> i & 1) ^ 1;
      if (cur->child[k] && cur->child[k]->Min <= high)
        res = res << 1 | k;
      else
        res = res << 1 | (k ^= 1);
      cur = cur->child[k];
    }
    return res;
  }
};
const int N = 1e5 + 1;
int n;
vector<int> q[N];
trie* head[N];
void enter() { cin >> n; }
void solve() {
  for (int i = 1, _b = N - 1; i <= _b; ++i) head[i] = new trie;
  for (int i = 1, type, x, k, s; i <= n; ++i) {
    cin >> type >> x;
    if (type == 1) {
      for (int j = 1; j * j <= x; ++j)
        if (x % j == 0) {
          q[j].emplace_back(x);
          if (j * j != x) q[x / j].emplace_back(x);
        }
    } else {
      cin >> k >> s;
      if (x % k || k > s - x)
        cout << -1 << '\n';
      else {
        for (int& y : q[k]) head[k]->insert(y);
        q[k].clear();
        cout << head[k]->max_xor(x, s - x) << '\n';
      }
    }
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  enter();
  solve();
}
