#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:536870912")
#pragma warning(disable : 4996)
using namespace std;
const int N = 1e5 + 100, D = 20, INF = 1e9;
vector<int> to[N][3], mn[N];
bool added[N];
void add(int d, int x) {
  int v = 0;
  for (int i = D - 1; i >= 0; i--) {
    int c = (x >> i) & 1;
    if (to[d][c][v] == -1) {
      to[d][c][v] = to[d][0].size();
      to[d][0].push_back(-1);
      to[d][1].push_back(-1);
      mn[d].push_back(INF);
    }
    v = to[d][c][v];
    mn[d][v] = min(mn[d][v], x);
  }
}
int que(int d, int x, int b) {
  int ans = 0, v = 0;
  for (int i = D - 1; i >= 0; i--) {
    int c = (x >> i) & 1;
    if (to[d][1 - c][v] != -1 && mn[d][to[d][1 - c][v]] <= b) {
      v = to[d][1 - c][v];
      ans |= (1 - c) << i;
    } else if (to[d][c][v] != -1 && mn[d][to[d][c][v]] <= b) {
      v = to[d][c][v];
      ans |= c << i;
    } else
      return -1;
  }
  return ans;
}
int main() {
  int q;
  scanf("%d", &q);
  for (int i = 0; i < N; i++) {
    to[i][0].push_back(-1);
    to[i][1].push_back(-1);
    mn[i].push_back(INF);
  }
  while (q--) {
    int t, x, k, s;
    scanf("%d%d", &t, &x);
    if (t == 1) {
      if (!added[x]) {
        added[x] = 1;
        int sq = sqrt(x);
        for (int j = 1; j <= sq; j++) {
          if (x % j == 0) {
            add(j, x);
            add(x / j, x);
          }
        }
      }
    } else {
      scanf("%d%d", &k, &s);
      if (x % k) {
        printf("-1\n");
      } else {
        printf("%d\n", que(k, x, s - x));
      }
    }
  }
  return 0;
}
