#include <bits/stdc++.h>
using namespace std;
void Debug() { cout << '\n'; }
template <class FIRST, class... REST>
void Debug(FIRST arg, REST... rest) {
  cout << arg << " ";
  Debug(rest...);
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  if (!v.empty()) {
    for (int i = int(0); i < int((int)(v).size() - 1); i++) out << v[i] << ", ";
    out << v.back();
  }
  out << "]";
  return out;
}
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& v) {
  out << "(" << v.first << ", " << v.second << ")";
  return out;
}
const int MAX_N = 200010;
const int MAX_V = 100010;
const double eps = 1e-6;
const long long mod = 1000000007;
const int inf = 1 << 29;
const long long linf = 1LL << 60;
const double PI = 3.14159265358979323846;
int Q;
set<int> S[100010];
void loop(const vector<pair<int, int> >& vec, int at, int x, int cur) {
  if (at == (int)(vec).size()) {
    S[cur].insert(x);
    return;
  }
  for (int i = int(0); i < int(vec[at].second + 1); i++) {
    loop(vec, at + 1, x, cur);
    cur *= vec[at].first;
  }
}
void add(int x) {
  vector<pair<int, int> > vec;
  int tx = x;
  for (int i = 2; i * i <= tx; i++) {
    if (tx % i == 0) {
      int cnt = 0;
      while (tx % i == 0) {
        tx /= i;
        cnt++;
      }
      vec.push_back(pair<int, int>(i, cnt));
    }
  }
  if (tx != 1) vec.push_back(pair<int, int>(tx, 1));
  loop(vec, 0, x, 1);
}
int get(int k, int y, int x) {
  set<int>& s = S[k];
  int a = 0, b = y + 1;
  int l = 0, r = (1 << 17);
  for (int i = int(17) - 1; i >= int(0); i--) {
    int m = (r + l) / 2;
    if ((1 << i) & x) {
      int tlv = max(a, l), trv = min(b, m);
      if (trv - tlv > 0 && s.lower_bound(trv) != s.lower_bound(tlv)) {
        r = m;
      } else {
        tlv = max(a, m), trv = min(b, r);
        if (trv - tlv > 0 && s.lower_bound(trv) != s.lower_bound(tlv)) {
          l = m;
        } else
          return -1;
      }
    } else {
      int tlv = max(a, m), trv = min(b, r);
      if (trv - tlv > 0 && s.lower_bound(trv) != s.lower_bound(tlv)) {
        l = m;
      } else {
        tlv = max(a, l), trv = min(b, r);
        if (trv - tlv > 0 && s.lower_bound(trv) != s.lower_bound(tlv)) {
          r = m;
        } else
          return -1;
      }
    }
  }
  return l;
}
void solve() {
  cin >> Q;
  while (Q--) {
    int type;
    cin >> type;
    if (type == 1) {
      int x;
      cin >> x;
      add(x);
    } else {
      int x, k, s;
      cin >> x >> k >> s;
      if (x % k != 0)
        cout << "-1\n";
      else
        cout << get(k, s - x, x) << "\n";
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(20);
  srand((unsigned int)time(NULL));
  solve();
  return 0;
}
