#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx2,avx,fma")
const int maxm = 1e5 + 2;
struct node {
  node* p[2];
  long long int mini;
  node() {
    p[0] = NULL;
    p[1] = NULL;
    mini = 1e5;
  }
};
node* root[maxm];
vector<vector<int> > di(1e5 + 2);
void func() {
  int i, j;
  for (i = 1; i < maxm; i++) {
    for (j = i; j < maxm; j += i) di[j].push_back(i);
    root[i] = new node();
  }
}
void add(int k, int n) {
  node* b = root[k];
  b->mini = min((int)b->mini, n);
  int i;
  for (i = 18; i >= 0; i--) {
    int t = (n >> i) & 1;
    if (b->p[t] == NULL) b->p[t] = new node();
    b = b->p[t];
    b->mini = min((int)b->mini, n);
  }
}
int query(int x, int k, int s) {
  node* b = root[k];
  if (x % k || (b->mini + x) > s) return -1;
  int i, ans = 0;
  for (i = 18; i >= 0; i--) {
    int t = (x >> i) & 1;
    if (b->p[t ^ 1] != NULL && (b->p[t ^ 1]->mini) + x <= s) {
      ans += ((t ^ 1) << i);
      b = b->p[t ^ 1];
    } else {
      ans += (t << i);
      b = b->p[t];
    }
  }
  return ans;
}
bool ch[maxm];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  func();
  int i, j, q, t;
  cin >> q;
  for (j = 0; j < q; j++) {
    cin >> t;
    if (t == 1) {
      int u;
      cin >> u;
      if (!ch[u]) {
        for (i = 0; i < di[u].size(); i++) add(di[u][i], u);
      }
    } else {
      int x, k, s;
      cin >> x >> k >> s;
      cout << query(x, k, s) << "\n";
    }
  }
  return 0;
}
