#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void Cin(T& x) {
  char c;
  T sign = 1;
  x = 0;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    if (c == '-') sign = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
  x *= sign;
}
const int N = (int)1e5 + 7;
const int M = (int)1e5;
const int Inf = (int)1e9;
struct TTrie {
  TTrie* chil[2];
  int val;
};
TTrie* r[N];
bool InTrie[N];
int q, cmd, x, k, s, res;
bool Get(int x, int i) { return (x >> i) & 1; }
TTrie* Add() {
  TTrie* p = new TTrie;
  p->chil[1] = p->chil[0] = NULL;
  p->val = Inf;
  return p;
}
void Insert(TTrie* root, int x) {
  TTrie* p = root;
  for (int i = 20; i >= 0; --i) {
    bool b = Get(x, i);
    if (p->chil[b] == NULL) p->chil[b] = Add();
    p = p->chil[b];
    p->val = min(p->val, x);
  }
}
int Query(TTrie* root, int x, int s) {
  TTrie* p = root;
  for (int i = 20; i >= 0; --i) {
    bool b = Get(x, i);
    if (p->chil[b ^ 1] && p->chil[b ^ 1]->val + x <= s)
      p = p->chil[b ^ 1];
    else if (p->chil[b] && p->chil[b]->val + x <= s)
      p = p->chil[b];
    else
      return -1;
  }
  return p->val;
}
void Enter() {
  Cin(q);
  for (int i = 1; i <= M; ++i) r[i] = Add();
}
void Solve() {
  while (q--) {
    Cin(cmd);
    if (cmd == 1) {
      Cin(x);
      if (InTrie[x]) continue;
      for (int i = 1; i * i <= x; ++i) {
        if (x % i) continue;
        Insert(r[i], x);
        if (i * i != x) Insert(r[x / i], x);
      }
      InTrie[x] = 1;
    } else {
      Cin(x), Cin(k), Cin(s);
      if (x % k == 0 && x <= s)
        cout << Query(r[k], x, s) << '\n';
      else
        cout << -1 << '\n';
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  Enter();
  Solve();
  return 0;
}
