#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const int BIT = 17;
const int D = 60;
bool has[N];
vector<int> fac[N];
int maxnum = 0;
struct Trie {
  struct Node {
    int data;
    Node* nex[2];
    Node() {
      data = 1e9 + 10;
      nex[0] = nex[1] = NULL;
    }
  };
  Node* root = new Node();
  void add(int x) {
    Node* now = root;
    for (int i = BIT - 1; i >= 0; --i) {
      now->data = min(x, now->data);
      int bit = (x >> i) & 1;
      if (now->nex[bit] == NULL) now->nex[bit] = new Node();
      now = now->nex[bit];
    }
    now->data = min(x, now->data);
  }
  int query_max(int x, int lim) {
    Node* now = root;
    int res = 0;
    for (int i = BIT - 1; i >= 0; --i) {
      int bit = !((x >> i) & 1);
      if (now->nex[bit] == NULL || now->nex[bit]->data > lim) bit ^= 1;
      now = now->nex[bit];
      res = (res << 1) | bit;
    }
    return res;
  }
} t[D + 5];
void init() {
  for (int i = 1; i <= D; ++i)
    for (int j = i; j <= 100000; j += i) fac[j].push_back(i);
}
int query(int x, int k, int s) {
  if (x % k != 0) return -1;
  if (k > D) {
    int lim = min(maxnum, s - x);
    int res = -1;
    for (int i = k; i <= lim; i += k)
      if (has[i]) res = max(res, x ^ i);
    if (res == -1) return -1;
    return res ^ x;
  }
  if (t[k].root->data > s - x) return -1;
  return t[k].query_max(x, s - x);
}
int main() {
  int Q, op, x, k, s;
  init();
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d%d", &op, &x);
    if (op == 1) {
      if (has[x]) continue;
      maxnum = max(maxnum, x);
      has[x] = 1;
      for (auto i : fac[x]) t[i].add(x);
    } else {
      scanf("%d%d", &k, &s);
      printf("%d\n", query(x, k, s));
    }
  }
  return 0;
}
