#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<int> factor[N];
struct Node {
  int mn;
  Node* nxt[2];
  Node() {
    mn = N;
    nxt[0] = nxt[1] = NULL;
  }
} * root[N];
void insert(int f, int x) {
  Node* now = root[f];
  for (int i = 17; i >= 0; i--) {
    int c = (x >> i) & 1;
    if (now->nxt[c] == NULL) {
      now->nxt[c] = new Node();
    }
    now = now->nxt[c];
    now->mn = min(now->mn, x);
  }
}
int query(int f, int x, int y) {
  Node* now = root[f];
  for (int i = 17; i >= 0; i--) {
    int c = (x >> i) & 1;
    if (now->nxt[!c] != NULL && (now->nxt[!c])->mn <= y) {
      now = now->nxt[!c];
    } else {
      if (now->nxt[c] != NULL && (now->nxt[c])->mn <= y)
        now = now->nxt[c];
      else
        return -1;
    }
  }
  return now->mn;
}
int main() {
  for (int i = 1; i < N; i++) {
    for (int j = i; j < N; j += i) factor[j].push_back(i);
    root[i] = new Node();
  }
  int n;
  cin >> n;
  while (n--) {
    int t;
    cin >> t;
    if (t == 1) {
      int x;
      cin >> x;
      for (int f : factor[x]) {
        insert(f, x);
      }
    } else {
      int x, k, s, ans = -1;
      cin >> x >> k >> s;
      if (x % k == 0) {
        ans = query(k, x, s - x);
      }
      cout << ans << endl;
    }
  }
  return 0;
}
