#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7, maxm = 2e7 + 7, W = 1e5, U = 16, INF = 0x3f3f3f3f;
int n, root[maxn], tot = W;
int son[maxm][2], minnum[maxm];
bool vis[maxn];
char cc;
long long ff;
template <typename T>
void read(T& aa) {
  aa = 0;
  cc = getchar();
  ff = 1;
  while ((cc < '0' || cc > '9') && cc != '-') cc = getchar();
  if (cc == '-') ff = -1, cc = getchar();
  while (cc >= '0' && cc <= '9') aa = aa * 10 + cc - '0', cc = getchar();
  aa *= ff;
}
int prime[maxn], totp, num[maxn];
bool ok[maxn];
void get_p() {
  for (int i = (2); i <= (W); ++i) {
    if (!ok[i]) prime[++totp] = i, num[i] = i;
    for (int j = (1); j <= (totp); ++j) {
      if (prime[j] > W / i) break;
      ok[i * prime[j]] = 1;
      num[i * prime[j]] = prime[j];
      if (i % prime[j] == 0) break;
    }
  }
}
void add(int pos, int x) {
  minnum[pos] = min(minnum[pos], x);
  int r;
  for (int i = (U); i >= (0); --i) {
    r = (x >> i) & 1;
    if (!son[pos][r]) minnum[son[pos][r] = ++tot] = x;
    pos = son[pos][r];
    minnum[pos] = min(minnum[pos], x);
  }
}
int zz[maxn];
void get_add(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  int s = 1, t = 1, p, now, y, o = x;
  zz[1] = 1;
  while (x != 1) {
    p = num[x];
    now = 0;
    y = 1;
    while (x % p == 0) x /= p, now++;
    for (int i = (1); i <= (now); ++i) {
      y *= p;
      for (int j = (1); j <= (s); ++j) zz[++t] = zz[j] * y;
    }
    s = t;
  }
  for (int i = (1); i <= (t); ++i) add(zz[i], o);
}
int get_ans(int x, int pos, int v) {
  if (x % pos || minnum[pos] > v) return -1;
  int r;
  for (int i = (U); i >= (0); --i) {
    r = (x >> i) & 1;
    if (minnum[son[pos][r ^ 1]] <= v)
      pos = son[pos][r ^ 1];
    else
      pos = son[pos][r];
  }
  return minnum[pos];
}
int main() {
  read(n);
  int op, k, x, v;
  get_p();
  for (int i = (0); i <= (W); ++i) minnum[i] = INF;
  for (int i = (1); i <= (n); ++i) {
    read(op);
    read(x);
    if (op == 1)
      get_add(x);
    else {
      read(k);
      read(v);
      printf("%d\n", get_ans(x, k, v - x));
    }
  }
  return 0;
}
