#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << to_string(H) << endl;
  debug_out(T...);
}
const int infi = 0x3f3f3f3f;
struct Snode {
  int mn = infi;
  Snode *child[2];
  Snode() {
    child[0] = nullptr;
    child[1] = nullptr;
  }
  Snode(int _mn) {
    mn = _mn;
    child[0] = child[1] = nullptr;
  }
};
const int SQN = 318;
Snode *trie[SQN];
void insert(int id, int val) {
  if (trie[id] == nullptr)
    trie[id] = new Snode(val);
  else
    trie[id]->mn = min(trie[id]->mn, val);
  Snode *cur = trie[id];
  for (int i = 18; i >= 0; i--) {
    if (cur->child[val >> i & 1] == nullptr) {
      cur->child[val >> i & 1] = new Snode(val);
    }
    cur->mn = min(cur->mn, val);
    cur = cur->child[val >> i & 1];
  }
}
int query(int id, int x, int s) {
  int allowed = s - x;
  Snode *cur = trie[id];
  if (cur == nullptr || (cur->mn > allowed)) return -1;
  int v = 0;
  for (int i = 18; i >= 0; i--) {
    if (x >> i & 1) {
      if (cur->child[0] != nullptr && cur->child[0]->mn <= allowed) {
        cur = cur->child[0];
      } else if (cur->child[1] != nullptr && cur->child[1]->mn <= allowed) {
        cur = cur->child[1];
        v = v ^ (1 << i);
      } else
        return -1;
    } else {
      if (cur->child[1] != nullptr && cur->child[1]->mn <= allowed) {
        cur = cur->child[1];
        v = v ^ (1 << i);
      } else if (cur->child[0] != nullptr && cur->child[0]->mn <= allowed) {
        cur = cur->child[0];
      } else
        return -1;
    }
  }
  return v;
}
const int N = 1e5 + 10;
vector<int> vec[N];
int present[N];
signed main() {
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int tp;
    scanf("%d", &tp);
    if (tp == 1) {
      int val;
      scanf("%d", &val);
      if (present[val]) continue;
      present[val] = true;
      for (int j = 1; j * j <= val; j++) {
        if (val % j) continue;
        int x = val / j;
        int y = j;
        if (x < SQN)
          insert(x, val);
        else
          vec[x].push_back(val);
        if (x == y) continue;
        if (y < SQN)
          insert(y, val);
        else
          assert(false);
      }
    } else {
      int x, k, s;
      scanf("%d %d %d", &x, &k, &s);
      if (x % k) {
        puts("-1");
        continue;
      }
      if (k < SQN) {
        printf("%d\n", query(k, x, s));
      } else {
        int ans = -1;
        int cg = 0;
        for (int v : vec[k]) {
          if (v + x <= s && (v ^ x) >= cg) {
            ans = v;
            cg = ans ^ x;
          }
        }
        printf("%d\n", ans);
      }
    }
  }
}
