#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 10;
const int M = 20;
int n;
vector<int> d[N];
bool has[N];
int cnt, rt[N], ch[N * 200][2], mn[N * 200];
int newNode() {
  ++cnt;
  ch[cnt][0] = ch[cnt][1] = 0;
  mn[cnt] = N;
  return cnt;
}
void init() {
  for (int i = 1; i < N; i++)
    for (int j = 0; j < N; j += i) d[j].push_back(i);
  mn[0] = N;
  for (int i = 1; i < N; i++) rt[i] = newNode();
}
void insert(int u, int v, int k) {
  mn[u] = min(mn[u], v);
  if (k == -1) return;
  int t = (v & (1 << k)) ? 1 : 0;
  if (!ch[u][t]) {
    ch[u][t] = newNode();
  }
  insert(ch[u][t], v, k - 1);
}
int find(int u, int v, int m, int k) {
  if (k == -1) return 0;
  int t = (v & (1 << k)) ? 1 : 0;
  if (mn[ch[u][!t]] <= m) {
    return find(ch[u][!t], v, m, k - 1) | ((!t) << k);
  } else if (mn[ch[u][t]] <= m) {
    return find(ch[u][t], v, m, k - 1) | (t << k);
  } else
    return -1;
}
int main() {
  init();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int opt, u, k, s, x;
    scanf("%d", &opt);
    if (opt == 1) {
      scanf("%d", &u);
      if (has[u]) continue;
      has[u] = true;
      for (int j = 0; j < d[u].size(); j++) insert(rt[d[u][j]], u, M);
    } else {
      scanf("%d %d %d", &x, &k, &s);
      if (x % k != 0) {
        printf("-1\n");
        continue;
      }
      printf("%d\n", find(rt[k], x, s - x, M));
    }
  }
  return 0;
}
