#include <bits/stdc++.h>
using namespace std;
struct Node {
  int ch[2];
  Node() { ch[0] = ch[1] = 0; }
};
Node* trees[100005];
int* sz[100005];
int tot[100005];
bool vis[100005];
void Insert(int o, int x) {
  int U = 1;
  ++sz[o][1];
  for (int i = 18 - 1; i >= 0; --i) {
    if (!trees[o][U].ch[(x >> i) & 1]) trees[o][U].ch[(x >> i) & 1] = ++tot[o];
    U = trees[o][U].ch[(x >> i) & 1];
    ++sz[o][U];
  }
}
void Insert(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  for (int i = 1; i * i <= x; ++i)
    if (x % i == 0) {
      if (i != x / i) {
        Insert(i, x);
        Insert(x / i, x);
      } else
        Insert(i, x);
    }
}
bool jiechu;
bool check(int o, int Bit, int lim, int i, int U) {
  if (jiechu || Bit < (lim >> (i - 1) & 1)) return sz[o][U];
  if (Bit > (lim >> (i - 1) & 1)) return 0;
  int sum = 0;
  for (--i; i >= 1; --i) {
    int limBit = (lim >> (i - 1) & 1);
    if (limBit == 1) sum += sz[o][trees[o][U].ch[0]];
    U = trees[o][U].ch[limBit];
  }
  sum += sz[o][U];
  return sum > 0;
}
int query(int o, int lim, int W) {
  jiechu = 0;
  int res = 0, U = 1;
  for (int i = 18; i >= 1; --i) {
    int Bit = ((W >> (i - 1) & 1) ^ 1);
    if (!check(o, Bit, lim, i, trees[o][U].ch[Bit])) {
      Bit ^= 1;
      if (!check(o, Bit, lim, i, trees[o][U].ch[Bit])) return -1;
    }
    if (Bit < (lim >> (i - 1) & 1)) jiechu = 1;
    res += (1 << (i - 1)) * Bit;
    U = trees[o][U].ch[Bit];
  }
  return res;
}
int q;
int main() {
  int op, x, K, s;
  for (int i = 1; i <= 100000; ++i) {
    tot[i] = 1;
    trees[i] = new Node[20 * 100000 / i];
    sz[i] = new int[20 * 100000 / i];
    for (int j = 0; j < 20 * 100000 / i; ++j) sz[i][j] = 0;
  }
  scanf("%d", &q);
  for (; q; --q) {
    scanf("%d%d", &op, &x);
    if (op == 1)
      Insert(x);
    else {
      scanf("%d%d", &K, &s);
      if (x % K != 0 || s <= x) {
        puts("-1");
        continue;
      }
      printf("%d\n", query(K, s - x, x));
    }
  }
  return 0;
}
