#include <bits/stdc++.h>
using namespace std;
const int inf = 0x7fffffff;
const int maxn = 101010;
struct TNode {
  TNode *son[2];
  long long minn;
  TNode() {
    minn = inf;
    son[0] = son[1] = NULL;
  }
};
struct pTrie {
  TNode *root;
  void insert(long long x) {
    if (root == NULL) root = new TNode();
    TNode *p = root;
    for (long long i = 19; i >= 0; i--) {
      long long nowbit = (x & (1 << i)) > 0;
      if (p->son[nowbit] == NULL) {
        p->son[nowbit] = new TNode();
      }
      p = p->son[nowbit];
      p->minn = min(p->minn, x);
    }
  }
  long long query(long long x, long long lim) {
    TNode *p = root;
    long long xbit;
    long long ret = 0;
    for (long long i = 19; i >= 0; i--) {
      if (x & (1 << i)) {
        xbit = i;
        break;
      }
    }
    for (long long i = 19; i >= 0; i--) {
      if (ret > lim) return -1;
      long long nowbit = (x & (1 << i)) > 0;
      long long fan = 1 - nowbit;
      if (!(nowbit <= 1 && nowbit >= 0)) {
        printf("fuckfuck!!\n");
        exit(0);
      }
      if (p == NULL) return -1;
      if (p->son[fan] != NULL && p->son[fan]->minn <= lim) {
        p = p->son[fan];
        ret |= (fan << i);
        continue;
      } else if (p->son[nowbit] != NULL && p->son[nowbit]->minn <= lim) {
        p = p->son[nowbit];
        ret |= (nowbit << i);
        continue;
      } else
        return -1;
    }
    return ret;
  }
  void deleteTrie(TNode *cur) {
    if (cur == NULL) return;
    deleteTrie(cur->son[0]);
    deleteTrie(cur->son[1]);
    delete cur;
  }
};
pTrie tries[maxn];
vector<long long> facs[maxn];
void initFactor() {
  for (long long i = 1; i < maxn; i++) {
    for (long long j = i; j < maxn; j += i) {
      facs[j].push_back(i);
    }
  }
}
long long Q;
void doInsert(long long uu) {
  if (uu >= maxn) return;
  long long sz = facs[uu].size();
  for (long long i = 0; i < sz; i++) {
    if (facs[uu][i] >= maxn) {
      printf("fuckyou!!\n");
      exit(0);
    }
    tries[facs[uu][i]].insert(uu);
  }
}
bool mem[maxn];
void doQuery(long long x, long long k, long long s) {
  long long mminlim = s - x;
  if (mminlim <= 0) {
    printf("-1\n");
    return;
  }
  if (x % k != 0) {
    printf("-1\n");
    return;
  }
  if (k >= maxn) {
    printf("-1\n");
    return;
  }
  printf("%d\n", tries[k].query(x, mminlim));
}
signed main() {
  initFactor();
  memset(mem, 0, sizeof mem);
  scanf("%lld", &Q);
  for (long long i = 1; i <= Q; i++) {
    long long act;
    scanf("%lld", &act);
    if (act == 1) {
      long long u;
      scanf("%lld", &u);
      if (mem[u]) continue;
      doInsert(u);
      mem[u] = true;
    } else if (act == 2) {
      long long x, k, s;
      scanf("%lld%lld%lld", &x, &k, &s);
      doQuery(x, k, s);
    }
  }
  for (long long i = 0; i < maxn; i++) tries[i].deleteTrie(tries[i].root);
  return 0;
}
