#include <bits/stdc++.h>
using namespace std;
int n, op, tot;
int son[20000010][2];
int cnt[20000010];
vector<int> vec[100010];
void insert(int k, int x) {
  int now = k;
  cnt[now] = min(cnt[now], x);
  for (register int i = 18; i >= 0; i--) {
    int p = (1 & (x >> i));
    if (son[now][p]) {
      now = son[now][p];
    } else {
      son[now][p] = ++tot;
      now = son[now][p];
    }
    cnt[now] = min(cnt[now], x);
  }
  cnt[now] = x;
}
int query(int x, int k, int s) {
  if (x % k != 0) return -1;
  if (cnt[k] > s - x) return -1;
  int now = k, res = 0;
  for (register int i = 18; i >= 0; i--) {
    int p = (1 & (x >> i));
    p ^= 1;
    if (cnt[son[now][p]] <= s - x) {
      now = son[now][p];
    } else {
      now = son[now][p ^ 1];
    }
  }
  return cnt[now];
}
int main() {
  scanf("%d", &n);
  for (register int i = 1; i <= 100000; i++) {
    for (register int j = i; j <= 100000; j += i) {
      vec[j].push_back(i);
    }
  }
  memset(cnt, 127, sizeof(cnt));
  tot = 100000;
  for (register int i = 1; i <= n; i++) {
    int x, k, s;
    scanf("%d%d", &op, &x);
    if (op == 1) {
      for (register int j = 0; j < vec[x].size(); j++) {
        insert(vec[x][j], x);
      }
    } else {
      scanf("%d%d", &k, &s);
      printf("%d\n", query(x, k, s));
    }
  }
  return 0;
}
