#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
vector<int> G[maxn];
struct node {
  int Min, val;
  node *ch[2];
  node() {
    Min = maxn;
    ch[0] = ch[1] = NULL;
  }
} * rt[maxn];
int vis[maxn];
void insert(int x) {
  int Len = G[x].size();
  for (int i = 0; i < Len; i++) {
    node *cur = rt[G[x][i]];
    cur->Min = min(cur->Min, x);
    for (int j = 17; j >= 0; j--) {
      if (cur->ch[x >> j & 1] == NULL) cur->ch[x >> j & 1] = new node;
      cur = cur->ch[x >> j & 1];
      cur->Min = min(cur->Min, x);
    }
    cur->val = x;
  }
}
int query(int x, int k, int s) {
  if (x % k != 0) return -1;
  node *cur = rt[k];
  if (cur->Min > s - x) return -1;
  for (int i = 17; i >= 0; i--) {
    int tb = x >> i & 1;
    if (cur->ch[tb ^ 1] != NULL && cur->ch[tb ^ 1]->Min <= s - x)
      cur = cur->ch[tb ^ 1];
    else
      cur = cur->ch[tb];
  }
  return cur->val;
}
int main() {
  int N, i, j, opt, x, k, s;
  for (i = 1; i < maxn; i++)
    for (j = i; j < maxn; j += i) G[j].push_back(i);
  for (i = 1; i < maxn; i++) rt[i] = new node;
  scanf("%d", &N);
  while (N--) {
    scanf("%d", &opt);
    if (opt == 1) {
      scanf("%d", &x);
      if (!vis[x]) vis[x] = 1, insert(x);
    } else {
      scanf("%d%d%d", &x, &k, &s);
      printf("%d\n", query(x, k, s));
    }
  }
  return 0;
}
