#include <bits/stdc++.h>
using namespace std;
const bool print = false;
const int N = 1e5 + 7;
const int D = 20;
int q;
int test(int v, int w) { return ((v & (1 << w)) > 0); }
struct tr {
  tr *son[2];
  int val = 1e9;
  tr() { son[0] = son[1] = NULL; }
  void ins(int v, int g) {
    val = min(val, v);
    if (g == -1) return;
    if (son[test(v, g)] == NULL) son[test(v, g)] = new tr();
    son[test(v, g)]->ins(v, g - 1);
  }
  void srch(int s, int x, int g) {
    if (g == -1) {
      printf("%d\n", val);
      return;
    }
    int res = -1;
    for (int i = (0); i <= (1); i++)
      if (son[i] != NULL && (son[i]->val) <= s)
        res = max(res, x ^ (son[i]->val));
    for (int i = (0); i <= (1); i++)
      if (son[i] != NULL && (son[i]->val) <= s && (x ^ (son[i]->val)) == res)
        son[i]->srch(s, x, g - 1);
  }
};
tr drz[N];
vector<int> dz[N];
bitset<N> already;
void new_val() {
  int v;
  scanf("%d", &v);
  if (already[v]) return;
  already[v] = 1;
  for (int i : dz[v]) drz[i].ins(v, D);
}
void query() {
  int x, k, s;
  scanf("%d%d%d", &x, &k, &s);
  if (x % k) {
    printf("-1\n");
    return;
  }
  s -= x;
  if (drz[k].val > s) {
    printf("-1\n");
    return;
  }
  drz[k].srch(s, x, D);
}
int main() {
  scanf("%d", &q);
  for (int i = (1); i <= (N - 1); i++)
    for (int j = i; j < N; j += i) dz[j].push_back(i);
  while (q--) {
    int t;
    scanf("%d", &t);
    if (t == 1)
      new_val();
    else
      query();
  }
  return 0;
}
