#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, mod = 1e9 + 7;
struct trie {
  int mm;
  int go[2];
  trie() {
    go[1] = go[0] = 0;
    mm = 1e9;
  }
};
vector<trie> t[N];
vector<int> D[N];
void get_divisors() {
  for (int i = 1; i < 1e5 + 1; i++)
    for (int j = i; j < 1e5 + 1; j += i) D[j].push_back(i);
  for (int i = 0; i < N; i++) t[i].emplace_back();
}
void Insert(int x, int which) {
  vector<trie> &tr = t[which];
  int cur = 0;
  for (int i = 16; i >= 0; i--) {
    bool bit = (x & (1 << i));
    if (!tr[cur].go[bit]) tr[cur].go[bit] = tr.size(), tr.emplace_back();
    cur = tr[cur].go[bit];
    tr[cur].mm = min(tr[cur].mm, x);
  }
}
int get(int x, int k, int s, int which) {
  s -= x;
  int cur = 0;
  int ret = 0;
  vector<trie> &tr = t[which];
  for (int i = 16; i >= 0; i--) {
    bool bit = (x & (1 << i));
    int cur1 = tr[cur].go[!bit];
    int cur2 = tr[cur].go[bit];
    if (cur1 && tr[cur1].mm <= s)
      cur = cur1, ret |= (bit ? 0 : (1 << i));
    else if (cur2 && tr[cur2].mm <= s)
      cur = cur2, ret |= (bit ? (1 << i) : 0);
    else
      return 1e9;
  }
  return ret;
}
int main() {
  get_divisors();
  int q;
  scanf("%d", &q);
  while (q--) {
    int type;
    scanf("%d", &type);
    if (type == 1) {
      int x;
      scanf("%d", &x);
      for (int it : D[x]) Insert(x, it);
    } else {
      int x, k, s;
      scanf("%d%d%d", &x, &k, &s);
      if (x % k)
        printf("-1\n");
      else {
        int ans = get(x, k, s, k);
        printf("%d\n", ((ans == 1e9) ? -1 : ans));
      }
    }
  }
  return 0;
}
