#include <bits/stdc++.h>
using namespace std;
typedef struct trie {
  int mi;
  int fin;
  trie* nxt[2];
  trie() {
    mi = 1000000007LL;
    fin = 0;
    memset(nxt, 0, sizeof(nxt));
  };
  void add(int x) {
    trie* ptr = this;
    ptr->mi = min(ptr->mi, x);
    for (int i = 18; i >= 0; i--) {
      bool bit = x >> i & 1;
      if (!ptr->nxt[bit]) ptr->nxt[bit] = new trie();
      ptr = ptr->nxt[bit];
      ptr->mi = min(ptr->mi, x);
    }
    ptr->fin++;
  }
  int getans(int k, int x, int s) {
    if (x % k || mi + x > s) return -1;
    int ans = 0;
    trie* ptr = this;
    for (int i = 18; i >= 0; i--) {
      bool bit = x >> i & 1;
      bool nex =
          ptr->nxt[bit ^ 1] && ptr->nxt[bit ^ 1]->mi + x <= s ? bit ^ 1 : bit;
      if (!ptr->nxt[nex]) break;
      ans += nex << i;
      ptr = ptr->nxt[nex];
    }
    return ans;
  }
} * ptrie;
int n, m;
ptrie tree[100007];
vector<int> uoc[100007];
void prefun() {
  for (int i = 1; i < 100007; i++) tree[i] = new trie();
  for (int i = 1; i < 100007; i++)
    for (int j = i; j < 100007; j += i) uoc[j].push_back(i);
}
void enter() {
  for (int i = 1; i < 100007; i++) tree[i] = new trie();
  cin >> n;
  int type, k, x, s;
  for (int ques = 1; ques <= n; ques++) {
    cin >> type;
    if (type == 1) {
      cin >> x;
      for (int u : uoc[x]) tree[u]->add(x);
    } else {
      cin >> x >> k >> s;
      cout << tree[k]->getans(k, x, s) << "\n";
    }
  }
}
int main() {
  {
    if (fopen("spoj"
              ".inp",
              "r"))
      freopen(
          "spoj"
          ".inp",
          "r", stdin),
          freopen(
              "spoj"
              ".out",
              "w", stdout);
  };
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  prefun();
  enter();
  return 0;
}
