#include <bits/stdc++.h>
using namespace std;
int trie[3][18 * 100005];
int mn[3][18 * 100005];
int cnt[100005];
int node_cnt;
vector<int> dv[100005];
vector<pair<pair<int, int>, pair<int, int> > > query[100005];
int ans[100005];
bool Check(int n, int pos) { return (bool)(n & (1 << pos)); }
void init() {
  for (int i = 1; i <= 100000; i++) {
    for (int j = i; j <= 100000; j += i) dv[j].push_back(i);
  }
}
void add(string second, int num) {
  int n = second.size();
  int cur = 0;
  for (int i = 0; i < n; i++) {
    int ch = second[i] - '0';
    if (!trie[ch][cur]) {
      trie[ch][cur] = ++node_cnt;
      mn[ch][cur] = num;
    }
    mn[ch][cur] = min(mn[ch][cur], num);
    cur = trie[ch][cur];
  }
}
int solve(string second, int sum, int x) {
  int ans = 0;
  int cur = 0;
  for (int i = 17, j = 0; i >= 0; i--, j++) {
    int ch = second[j] - '0';
    if (ch) {
      if (trie[0][cur] && mn[0][cur] + x <= sum) {
        cur = trie[0][cur];
      } else if (trie[1][cur] && mn[1][cur] + x <= sum) {
        cur = trie[1][cur];
        ans += (1 << i);
      } else
        return -1;
    } else {
      if (trie[1][cur] && mn[1][cur] + x <= sum) {
        cur = trie[1][cur];
        ans += (1 << i);
      } else if (trie[0][cur] && mn[0][cur] + x <= sum) {
        cur = trie[0][cur];
      } else
        return -1;
    }
  }
  return ans;
}
int main() {
  init();
  int q;
  scanf("%d", &q);
  for (int p = 0; p < q; p++) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int u;
      scanf("%d", &u);
      if (cnt[u]) continue;
      cnt[u] = 1;
      for (int i : dv[u]) query[i].push_back({{1, p}, {u, -1}});
    } else {
      int x, k, sum;
      scanf("%d %d %d", &x, &k, &sum);
      query[k].push_back({{2, p}, {x, sum}});
    }
  }
  for (int i = 0; i <= 100000; i++) {
    for (int j = 0; j <= node_cnt; j++) {
      trie[0][j] = trie[1][j] = mn[0][j] = mn[1][j] = 0;
    }
    node_cnt = 0;
    for (auto j : query[i]) {
      if (j.first.first == 1) {
        int u = j.second.first;
        string second;
        for (int i = 17; i >= 0; i--) {
          second += (Check(u, i) + '0');
        }
        add(second, u);
      } else {
        int idx = j.first.second;
        int u = j.second.first, sum = j.second.second;
        string second;
        for (int i = 17; i >= 0; i--) {
          second += (Check(u, i) + '0');
        }
        if (u % i != 0)
          ans[idx] = -1;
        else
          ans[idx] = solve(second, sum, u);
      }
    }
  }
  for (int i = 0; i < q; i++) {
    if (ans[i]) printf("%d\n", ans[i]);
  }
}
