#include <bits/stdc++.h>
using namespace std;
int T, k, x, s;
int a[140005], c[30 + 1][140005];
int bit(int x) { return x & -x; }
void add(int t, int x) {
  for (; x <= 100000; x += bit(x)) c[t][x]++;
}
int sum(int t, int x) {
  if (x <= 0) return 0;
  int ans = 0;
  for (; x; x -= bit(x)) ans += c[t][x];
  return ans;
}
int getmin(int t, int l, int r) {
  int tmp = sum(t, l - 1);
  if (sum(t, l) - tmp > 0) return l;
  while (r - l > 1) {
    int mid = l + r >> 1;
    if (sum(t, mid) - tmp > 0)
      r = mid;
    else
      l = mid;
  }
  return r;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &k);
    if (k == 1) {
      scanf("%d", &x);
      a[x]++;
      for (int i = 1; i <= 30; i++)
        if (x % i == 0) add(i, x);
      continue;
    }
    scanf("%d%d%d", &x, &k, &s);
    if (x % k != 0) {
      printf("-1\n");
      continue;
    }
    if (k > 30) {
      int res = 0;
      for (int i = k; i + x <= s; i += k)
        if (a[i] && ((i ^ x) > (res ^ x) || res == 0)) res = i;
      if (res == 0) res = -1;
      printf("%d\n", res);
      continue;
    }
    int now = 0;
    for (int i = 16; i >= 0; i--) {
      if (((x >> i) & 1) == 0) {
        if (sum(k, now + (1 << i + 1) - 1) - sum(k, now + (1 << i) - 1) > 0 &&
            getmin(k, now + (1 << i), now + (1 << i + 1) - 1) + x <= s)
          now += 1 << i;
        else if (sum(k, now + (1 << i) - 1) - sum(k, now - 1) > 0 &&
                 getmin(k, now, now + (1 << i - 1)) + x <= s)
          now = now;
        else {
          now = 0;
          break;
        }
      } else {
        if (sum(k, now + (1 << i) - 1) - sum(k, now - 1) > 0 &&
            getmin(k, now, now + (1 << i - 1)) + x <= s)
          now = now;
        else if (sum(k, now + (1 << i + 1) - 1) - sum(k, now + (1 << i) - 1) >
                     0 &&
                 getmin(k, now + (1 << i), now + (1 << i + 1) - 1) + x <= s)
          now += 1 << i;
        else {
          now = 0;
          break;
        }
      }
    }
    if (now == 0 || now + x > s) {
      printf("-1\n");
    } else {
      printf("%d\n", now);
    }
  }
}
