#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
struct node {
  int n;
  node *zero = NULL;
  node *one = NULL;
  node(int num) { n = num; }
};
node *trie[111111];
vector<int> d[111111];
void add(int k, int u) {
  if (trie[k] == NULL) trie[k] = new node(u);
  node *curr = trie[k];
  for (int i = 18; i >= 0; i--) {
    curr->n = min(curr->n, u);
    if (u & (1 << i)) {
      if (curr->one == NULL) curr->one = new node(u);
      curr = curr->one;
    } else {
      if (curr->zero == NULL) curr->zero = new node(u);
      curr = curr->zero;
    }
  }
}
int query(int x, int k, int s) {
  if (x % k || trie[k] == NULL || trie[k]->n + x > s) return -1;
  node *curr = trie[k];
  int res = 0;
  for (int i = 18; i >= 0; i--) {
    if (x & (1 << i)) {
      if (curr->zero != NULL && curr->zero->n + x <= s) {
        curr = curr->zero;
      } else {
        res |= (1 << i);
        curr = curr->one;
      }
    } else {
      if (curr->one != NULL && curr->one->n + x <= s) {
        curr = curr->one;
        res |= (1 << i);
      } else
        curr = curr->zero;
    }
  }
  return res;
}
int main() {
  for (int i = 1; i <= 111111; i++) {
    for (int j = i; j <= 111111; j += i) d[j].push_back(i);
  }
  int q, u, k, x, s, t;
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &t);
    if (t == 1) {
      scanf("%d", &u);
      for (int k : d[u]) add(k, u);
    } else {
      scanf("%d %d %d", &x, &k, &s);
      printf("%d\n", query(x, k, s));
    }
  }
  return 0;
}
