#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using PII = pair<int, int>;
using TI3 = tuple<int, int, int>;
using LL = long long;
using ULL = unsigned long long;
using MAT = array<array<LL, 2>, 2>;
template <class T>
constexpr inline T begin(const T &) {
  return 0;
}
template <class T>
constexpr inline T end(const T &x) {
  return x;
}
template <class T>
void _read(T &x) {
  cin >> x;
}
void _read(size_t &x) { scanf("%zu", &x); }
void _read(int &x) { scanf("%d", &x); }
void _read(LL &x) { scanf("%lld", &x); }
void _read(ULL &x) { scanf("%llu", &x); }
void _read(double &x) { scanf("%lf", &x); }
void _read(char &x) { scanf(" %c", &x); }
void _read(char *x) { scanf("%s", x); }
void read() {}
template <class T, class... U>
void read(T &head, U &...tail) {
  _read(head);
  read(tail...);
}
template <class T>
void _print(const T &x) {
  cout << x;
}
void _print(const size_t &x) { printf("%zu", x); }
void _print(const int &x) { printf("%d", x); }
void _print(const LL &x) { printf("%lld", x); }
void _print(const ULL &x) { printf("%llu", x); }
void _print(const double &x) { printf("%.16lf", x); }
void _print(const char &x) { putchar(x); }
void _print(const char *x) { printf("%s", x); }
template <class T>
void _print(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _print(*i++)) {
    if (i != x.cbegin()) putchar(' ');
  }
}
void print() {}
template <class T, class... U>
void print(const T &head, const U &...tail) {
  _print(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  print(tail...);
}
const int MOD = 1000000007;
struct TrieNode {
  TrieNode *next[2];
  int m;
  TrieNode() {
    memset(next, 0, sizeof(next));
    m = 0x3f3f3f3f;
  }
};
struct Trie {
  TrieNode *root;
  Trie() { root = new TrieNode(); }
  void insert(const int num) {
    TrieNode *p = root;
    static_assert(std::is_same<decltype(17), decltype(-1)>::value,
                  "REP diff types");
    for (auto k = (17); ((-1) > 0 and k < (-1)) or ((-1) < 0 and k > (-1));
         k += (-1)) {
      bool b = (num >> k) & 1;
      if (p->next[b] == NULL) {
        p->next[b] = new TrieNode();
      }
      p = p->next[b];
      p->m = min(p->m, num);
    }
  }
  int search(const int num, int s) {
    int cur = 0;
    TrieNode *p = root;
    static_assert(std::is_same<decltype(17), decltype(-1)>::value,
                  "REP diff types");
    for (auto k = (17); ((-1) > 0 and k < (-1)) or ((-1) < 0 and k > (-1));
         k += (-1)) {
      bool b = (num >> k) & 1;
      if (p->next[!b] and p->next[!b]->m <= s) {
        p = p->next[!b];
        cur |= (int(!b) << k);
      } else if (p->next[b] and p->next[b]->m <= s) {
        p = p->next[b];
        cur |= (int(b) << k);
      } else {
        return -1;
      }
    }
    return cur;
  }
};
unordered_set<int> S;
int main() {
  const int MAGIC = 100;
  Trie trie[MAGIC + 1];
  int q;
  read(q);
  for (auto i = begin(q); i != end(q); ++i) {
    int t;
    read(t);
    if (t == 1) {
      int u;
      read(u);
      S.insert(u);
      static_assert(std::is_same<decltype(1), decltype(MAGIC + 1)>::value,
                    "REP diff types");
      for (auto j = (1); j < (MAGIC + 1); ++j) {
        if (u % j == 0) {
          trie[j].insert(u);
        }
      }
    } else {
      int x, k, s;
      read(x, k, s);
      if (x % k != 0) {
        print(-1);
        continue;
      }
      if (k <= MAGIC) {
        int ans = trie[k].search(x, s - x);
        print(ans);
      } else {
        int M = -1, vM = -1;
        static_assert(std::is_same<decltype(k), decltype(100001)>::value,
                      "REP diff types");
        for (auto v = (k);
             ((k) > 0 and v < (100001)) or ((k) < 0 and v > (100001));
             v += (k)) {
          if (x + v <= s and S.find(v) != S.end()) {
            if ((v ^ x) > M) {
              vM = v;
              M = v ^ x;
            }
          }
        }
        print(vM);
      }
    }
  }
  return 0;
}
