#include <bits/stdc++.h>
using namespace std;
int q, tipe, temp, x, k, s;
vector<int> v;
set<int> cek;
int val[100000 + 5];
map<pair<int, pair<int, int> >, int> temp2;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &tipe);
    if (tipe == 1) {
      scanf("%d", &temp);
      if (!val[temp]) {
        val[temp] = true;
        cek.insert(temp);
        temp2.clear();
      }
    } else {
      scanf("%d %d %d", &x, &k, &s);
      if (x % k != 0 or x >= s)
        printf("-1\n");
      else {
        if (temp2[make_pair(x, make_pair(k, s))]) {
          printf("%d\n", temp2[make_pair(x, make_pair(k, s))]);
          continue;
        }
        int xam = -1;
        int res = -1;
        auto its = cek.upper_bound(s - x);
        int it = *its;
        if (it <= 0 or it < (s - x) / k) {
          for (auto b = cek.begin(); b != its and (*b) + x <= s; ++b) {
            if (xam < ((*b) ^ x) and gcd(*b, x) % k == 0) {
              xam = ((*b) ^ x);
              res = *b;
            }
          }
        } else {
          for (int i = k; i <= 100000 and i + x <= s; i += k) {
            if (val[i] and xam < (i ^ x)) {
              xam = (i ^ x);
              res = i;
            }
          }
        }
        temp2[make_pair(x, make_pair(k, s))] = res;
        printf("%d\n", res);
      }
    }
  }
}
