#include <bits/stdc++.h>
using namespace std;
vector<int> huaji[100005];
struct trie {
  struct node {
    int minn;
    node *next[2];
    node() {
      minn = 1e9 + 9;
      next[0] = NULL;
      next[1] = NULL;
    }
  };
  node *root;
  trie() { root = new (node); }
  void insert(int x) {
    node *pointer = root;
    pointer->minn = min(pointer->minn, x);
    for (int i = 20; i >= 0; i--) {
      if (pointer->next[x >> i & 1] == NULL)
        pointer->next[x >> i & 1] = new (node);
      pointer = pointer->next[x >> i & 1];
      pointer->minn = min(pointer->minn, x);
    }
  }
} ygy[100005];
void init() {
  for (int i = 1; i < 1e5 + 5; i++) {
    for (int j = i; j < 1e5 + 5; j += i) {
      huaji[j].push_back(i);
    }
  }
}
int q;
int read() {
  int num = 0, w = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    num = num * 10 + ch - 48;
    ch = getchar();
  }
  return num * w;
}
int find(int x, int k, int s) {
  trie::node *pointer = ygy[k].root;
  if ((x % k) != 0 || pointer->minn + x > s) {
    return -1;
  }
  int ans = 0;
  for (int i = 20; i >= 0; i--) {
    int bit = x >> i & 1;
    if (pointer->next[!(bit)] != NULL && pointer->next[!(bit)]->minn + x <= s) {
      ans += (bit ^ 1) << i;
      pointer = pointer->next[bit ^ 1];
    } else {
      ans += (bit << i);
      pointer = pointer->next[bit];
    }
  }
  return ans;
}
int op;
int main() {
  init();
  q = read();
  while (q--) {
    op = read();
    if (op == 1) {
      int u = read();
      for (int i = 0; i < huaji[u].size(); i++) {
        ygy[huaji[u][i]].insert(u);
      }
    }
    if (op == 2) {
      int x = read(), k = read(), s = read();
      trie::node *pointer = ygy[k].root;
      if ((x % k) != 0 || pointer->minn + x > s) {
        cout << -1 << endl;
        continue;
      }
      int ans = 0;
      for (int i = 20; i >= 0; i--) {
        int bit = x >> i & 1;
        if (pointer->next[!(bit)] != NULL &&
            pointer->next[!(bit)]->minn + x <= s) {
          ans += (bit ^ 1) << i;
          pointer = pointer->next[bit ^ 1];
        } else {
          ans += (bit << i);
          pointer = pointer->next[bit];
        }
      }
      cout << ans << endl;
    }
  }
}
