#include <bits/stdc++.h>
using namespace std;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
template <typename first, typename second>
ostream& operator<<(ostream& os, const pair<first, second>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ",";
    os << *it;
  }
  return os << "]";
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename first, typename second>
ostream& operator<<(ostream& os, const map<first, second>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}
void faltu() { cerr << '\n'; }
template <typename T>
void faltu(T a[], int n) {
  for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
  cerr << '\n';
}
template <typename T, typename... hello>
void faltu(T arg, const hello&... rest) {
  cerr << arg << ' ';
  faltu(rest...);
}
inline bool checkBit(long long n, int i) { return n & (1LL << i); }
inline long long setBit(long long n, int i) {
  return n | (1LL << i);
  ;
}
inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }
const int mx = 1e5 + 5;
vector<int> divv[mx];
int lim = 27;
bool used[mx];
struct trie {
  int choto_le, choto_ri;
  int le, ri;
  trie() : le(-1), ri(-1), choto_le(inf), choto_ri(inf) {}
};
vector<trie> T[mx];
void add(int konta, int num) {
  int node = 0;
  for (int i = lim; i >= 0; --i) {
    if (!checkBit(num, i)) {
      if (T[konta][node].le == -1) {
        T[konta][node].le = (int)T[konta].size();
        T[konta].push_back(trie());
      }
      T[konta][node].choto_le = min(T[konta][node].choto_le, num);
      node = T[konta][node].le;
    } else {
      if (T[konta][node].ri == -1) {
        T[konta][node].ri = (int)T[konta].size();
        T[konta].push_back(trie());
      }
      T[konta][node].choto_ri = min(T[konta][node].choto_ri, num);
      node = T[konta][node].ri;
    }
  }
}
int func(int konta, int x, int boro) {
  int node = 0;
  int num = -1;
  for (int i = lim; i >= 0; --i) {
    if (!checkBit(x, i)) {
      if (T[konta][node].ri != -1 && T[konta][node].choto_ri <= boro) {
        num = T[konta][node].choto_ri;
        node = T[konta][node].ri;
      } else if (T[konta][node].le != -1 && T[konta][node].choto_le <= boro) {
        num = T[konta][node].choto_le;
        node = T[konta][node].le;
      } else
        break;
    } else {
      if (T[konta][node].le != -1 && T[konta][node].choto_le <= boro) {
        num = T[konta][node].choto_le;
        node = T[konta][node].le;
      } else if (T[konta][node].ri != -1 && T[konta][node].choto_ri <= boro) {
        num = T[konta][node].choto_ri;
        node = T[konta][node].ri;
      } else
        break;
    }
  }
  return num;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  for (int i = 1; i < mx; ++i) {
    for (int j = i; j < mx; j += i) {
      divv[j].push_back(i);
    }
  }
  for (int i = 1; i < mx; ++i) T[i].push_back(trie());
  int q;
  cin >> q;
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int u;
      cin >> u;
      if (used[u]) continue;
      used[u] = 1;
      for (auto x : divv[u]) add(x, u);
    } else {
      int x, k, s;
      cin >> x >> k >> s;
      if (x % k != 0) {
        cout << -1 << '\n';
        continue;
      }
      if (s - x <= 0) {
        cout << -1 << '\n';
        continue;
      }
      int ans = func(k, x, s - x);
      cout << ans << '\n';
    }
  }
}
