#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double PI = acos((long double)-1.0);
const int INF = 1 << 30;
const long long LINF = 1ll << 61;
const int BUFFER_SZ = 1 << 20;
const int MOD = 1000 * 1000 * 1000 + 9;
char BUFFER[BUFFER_SZ];
int gi() {
  int x;
  scanf("%d", &x);
  return x;
}
double gd() {
  double x;
  scanf("%lf", &x);
  return x;
}
long long gll() {
  long long x;
  cin >> x;
  return x;
}
vector<int> gvi(int n) {
  vector<int> a;
  while (n-- > 0) a.push_back(gi());
  return a;
}
string gs() {
  scanf("%s", BUFFER);
  return string(BUFFER);
}
template <class T>
void print(vector<T> &x, string format) {
  format += "%c";
  for (int i = 0; i < x.size(); ++i)
    printf(format.c_str(), x[i], i + 1 == x.size() ? '\n' : ' ');
}
const int LEN = 18;
set<int> B[111111];
void add(int x) {
  B[1].insert(x);
  B[x].insert(x);
  for (int d = 2; d * d <= x; ++d) {
    if (x % d == 0) {
      B[d].insert(x);
      B[x / d].insert(x);
    }
  }
}
inline int test(int k, int v) {
  set<int>::iterator it = B[k].lower_bound(v);
  if (it == B[k].end()) return -1;
  return *it;
}
int req(int x, int k, int s) {
  if (x % k) return -1;
  int res = 0;
  for (int p = LEN - 1; p >= 0; --p) {
    if (x >> p & 1) {
      int o1 = test(k, res);
      if (o1 != -1 && (res >> p) == (o1 >> p) && x + o1 <= s) {
      } else {
        res |= 1 << p;
        int o2 = test(k, res);
        if (o2 != -1 && (res >> p) == (o2 >> p) && x + o2 <= s) {
        } else {
          return -1;
        }
      }
    } else {
      res |= 1 << p;
      int o1 = test(k, res);
      if (o1 != -1 && (res >> p) == (o1 >> p) && x + o1 <= s) {
      } else {
        res ^= 1 << p;
        int o2 = test(k, res);
        if (o2 != -1 && (res >> p) == (o2 >> p) && x + o2 <= s) {
        } else {
          return -1;
        }
      }
    }
  }
  return res;
}
void solve() {
  int q = gi();
  for (int t = 0; t < q; ++t) {
    int o = gi();
    if (o == 1) {
      int x = gi();
      add(x);
    } else {
      int x = gi();
      int k = gi();
      int s = gi();
      printf("%d\n", req(x, k, s));
    }
  }
}
int main() {
  solve();
  return 0;
}
