#include <bits/stdc++.h>
using namespace std;
struct Trie {
  Trie *son[2];
  int count, num, Min;
  Trie() {
    count = num = 0, Min = 1e9;
    memset(son, 0, sizeof son);
  }
} * rt[100010];
int cnt = 1;
void Insert(int s, int f) {
  int deep = 16;
  Trie *now = rt[f];
  now->count++, now->Min = min(now->Min, s);
  while (deep >= 0) {
    int g = (s >> deep) & 1;
    if (now->son[g] == NULL) now->son[g] = new Trie();
    now = now->son[g];
    now->count++, now->Min = min(now->Min, s);
    deep--;
  }
  now->num++;
}
int Find(int s, int f, int h) {
  int deep = 16, ans = 0;
  Trie *now = rt[f];
  while (deep >= 0) {
    int g = (s >> deep) & 1, g2 = g ^ 1;
    if (now->son[g2] != NULL && now->son[g2]->Min <= h - s)
      now = now->son[g2], ans += g2 * (1 << deep);
    else
      now = now->son[g], ans += g * (1 << deep);
    deep--;
  }
  return ans;
}
int main() {
  for (int i = 1; i < 100010; i++) rt[i] = new Trie();
  int Q, cas, x, k, s;
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d", &cas);
    if (cas == 1) {
      scanf("%d", &x);
      int h = sqrt(x);
      for (int i = 1; i <= h; i++) {
        if (x % i == 0) {
          Insert(x, i);
          if (x != i * i) Insert(x, x / i);
        }
      }
    } else {
      scanf("%d%d%d", &x, &k, &s);
      if (x % k != 0 || rt[k]->Min > s - x) {
        printf("-1\n");
        continue;
      }
      printf("%d\n", Find(x, k, s));
    }
  }
  return 0;
}
