#include <bits/stdc++.h>
using namespace std;
const long double EPS = (long double)1e-7;
const long double PI = acos(0) * 2;
bool isZero(const long double& x) { return abs(x) <= EPS; }
int sign(const long double& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); }
long long gcd(long long a, long long b) {
  for (; b; a %= b, swap(a, b)) {
  }
  return abs(a);
}
pair<long long, long long> operator+(const pair<long long, long long>& a,
                                     const pair<long long, long long>& b) {
  return pair<long long, long long>(a.first + b.first, a.second + b.second);
}
pair<long long, long long> operator-(const pair<long long, long long>& a,
                                     const pair<long long, long long>& b) {
  return pair<long long, long long>(a.first - b.first, a.second - b.second);
}
pair<long long, long long> operator*(const pair<long long, long long>& a,
                                     const long long& b) {
  return pair<long long, long long>(a.first * b, a.second * b);
}
long long operator*(const pair<long long, long long>& a,
                    const pair<long long, long long>& b) {
  return a.first * b.second - b.first * a.second;
}
long long ccw(const pair<long long, long long>& a,
              const pair<long long, long long>& b,
              const pair<long long, long long>& c) {
  return a * b + b * c + c * a;
}
void fg(vector<int> G[], int a, int b) {
  G[a].push_back(b);
  G[b].push_back(a);
}
void fg(vector<pair<int, int> > G[], int a, int b, int c) {
  G[a].push_back({b, c});
  G[b].push_back({a, c});
}
const int MAXX = 100005;
struct SEG {
  SEG() : l(NULL), r(NULL), dt(0) {}
  SEG *l, *r;
  int dt;
  void upd(int s, int e, int x) {
    dt++;
    if (s == e) return;
    int m = (s + e) >> 1;
    if (x <= m) {
      if (NULL == l) l = new SEG();
      l->upd(s, m, x);
    } else {
      if (NULL == r) r = new SEG();
      r->upd(m + 1, e, x);
    }
  }
  int getcnt(int s, int e, int p, int q) {
    if (q < p || e < p || q < s) return 0;
    if (p <= s && e <= q) return dt;
    int m = (s + e) >> 1;
    return (l ? l->getcnt(s, m, p, q) : 0) +
           (r ? r->getcnt(m + 1, e, p, q) : 0);
  }
  int get(int s, int e, int x, int u) {
    if (!dt) return -1;
    if (s == e) return s <= u ? s : -1;
    int m = (s + e) >> 1;
    int L = (l ? l->getcnt(s, m, s, u) : 0);
    int R = (r ? r->getcnt(m + 1, e, m + 1, u) : 0);
    if ((m ^ x) >= ((m + 1) ^ x)) {
      if (NULL != l && L) return l->get(s, m, x, u);
      if (NULL != r && R) return r->get(m + 1, e, x, u);
    } else {
      if (NULL != r && R) return r->get(m + 1, e, x, u);
      if (NULL != l && L) return l->get(s, m, x, u);
    }
    return -1;
  }
};
bitset<MAXX> chk;
SEG seg[200];
int Q;
int solve(int x, int k, int s) {
  if (x % k || s - x <= 0) return -1;
  if (200 <= k) {
    int r = -1, ri = -1;
    for (int v = k; v < MAXX && x + v <= s; v += k) {
      if (chk[v]) {
        int t = x ^ v;
        if (r < t) {
          r = t;
          ri = v;
        }
      }
    }
    return ri;
  }
  return seg[k].get(0, 131071, x, s - x);
}
int main() {
  scanf("%d", &Q);
  for (int t, a, b, c; Q--;) {
    scanf("%d%d", &t, &a);
    if (1 == t) {
      if (chk[a]) continue;
      chk[a] = true;
      for (int i = 1; i < 200; i++) {
        if (a % i) continue;
        seg[i].upd(0, 131071, a);
      }
    } else {
      scanf("%d%d", &b, &c);
      printf("%d\n", solve(a, b, c));
    }
  }
  return 0;
}
