#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
struct node {
  node* zero = NULL;
  node* one = NULL;
  int minas = 1e9;
};
node* tries[100001];
void add(int x, node* nod) {
  for (int i = 20; i >= 0; i--) {
    if (x & (1 << i)) {
      if (nod->one == NULL) {
        nod->one = new node;
      }
      nod = nod->one;
    } else {
      if (nod->zero == NULL) {
        nod->zero = new node;
      }
      nod = nod->zero;
    }
    nod->minas = min(nod->minas, x);
  }
}
int get(int x, int maxVal, node* nod) {
  bool any = 0;
  int ans = 0;
  for (int i = 20; i >= 0; i--) {
    bool poss0 = 0, poss1 = 0;
    if (nod->zero != NULL && nod->zero->minas <= maxVal) poss0 = 1;
    if (nod->one != NULL && nod->one->minas <= maxVal) poss1 = 1;
    int go = -1;
    if (x & (1 << i)) {
      if (poss0) {
        go = 0;
      } else if (poss1) {
        go = 1;
      }
    } else {
      if (poss1) {
        go = 1;
      } else if (poss0) {
        go = 0;
      }
    }
    if (go == -1) return -1;
    if (go == 1) {
      ans += (1 << i);
      nod = nod->one;
    } else {
      nod = nod->zero;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  for (int i = 1; i <= 100000; i++) {
    tries[i] = new node;
  }
  int q;
  cin >> q;
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int u;
      cin >> u;
      for (int i = 1; i <= sqrt(u); i++) {
        if (u % i == 0) {
          add(u, tries[i]);
          add(u, tries[u / i]);
        }
      }
    } else {
      int x, k, s;
      cin >> x >> k >> s;
      if (x % k != 0 || s - x < 0) {
        cout << "-1\n";
        continue;
      }
      cout << get(x, s - x, tries[k]) << '\n';
    }
  }
}
