#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005, INF = 0x3f3f3f3f, BOUND = 100000;
struct Node {
  int son[2], mn;
};
vector<Node> tr[MAXN];
int vis[MAXN], Q;
void ins(int r, int x) {
  int k = 0;
  tr[r][k].mn = min(tr[r][k].mn, x);
  for (int i = 16; i >= 0; i--) {
    int t = x >> i & 1, s = tr[r][k].son[t];
    if (!s) {
      s = tr[r][k].son[t] = (int)tr[r].size();
      tr[r].push_back((Node){0, 0, INF});
    }
    k = s, tr[r][k].mn = min(tr[r][k].mn, x);
  }
}
int ask(int r, int x, int s) {
  int k = 0, res = 0;
  if (tr[r][k].mn > s) return -1;
  for (int i = 16; i >= 0; i--) {
    int t = !(x >> i & 1), ss = tr[r][k].son[t];
    if (ss && tr[r][ss].mn <= s)
      res |= t << i, k = ss;
    else
      k = tr[r][k].son[!t], res |= !t << i;
  }
  return res;
}
vector<int> dv[MAXN];
int main() {
  for (int i = 1; i <= BOUND; i++) tr[i].push_back((Node){0, 0, INF});
  for (int i = 1; i <= BOUND; i++)
    for (int j = i; j <= BOUND; j += i) dv[j].push_back(i);
  for (scanf("%d", &Q); Q--;) {
    int t, a, b, c;
    scanf("%d%d", &t, &a);
    if (t == 1) {
      if (vis[a]) continue;
      for (int i : dv[a]) ins(i, a);
    } else {
      scanf("%d%d", &b, &c);
      printf("%d\n", a % b ? -1 : ask(b, a, c - a));
    }
  }
  return 0;
}
