#include <bits/stdc++.h>
using namespace std;
inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
template <typename T = int>
inline T nxt() {
  char c = nc();
  T x = 0;
  int f = 1;
  for (; c > '9' || c < '0'; c = nc())
    if (c == '-') f = -1;
  for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
    ;
  x *= f;
  return x;
}
const int MAXN = 1000123;
int d[MAXN];
bool a[MAXN];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  int q = nxt();
  while (q--) {
    int t = nxt();
    if (t == 1) {
      int x = nxt();
      a[x] = true;
      for (int i = 1; i < (350); ++i) {
        if (x % i == 0) d[i] = max(x, d[i]);
      }
    } else {
      int x = nxt(), k = nxt(), s = nxt(), ans = -1;
      if (x % k == 0) {
        if (k >= 350) {
          for (int i = x; i <= s; i += k) {
            if (a[i - x]) ans = max(ans, (i - x) ^ x);
          }
        } else {
          for (int i = min(s, x + d[k]); i >= 0; i--) {
            int v = i ^ x;
            if (v % k == 0 && v + x <= s && a[v]) {
              ans = i;
              break;
            }
          }
        }
      }
      if (ans != -1) ans ^= x;
      cout << ans << '\n';
    }
  }
}
