#include <bits/stdc++.h>
using namespace std;
class Node {
 public:
  Node(int z = -1, int o = -1) : zeroChild(z), oneChild(o) {}
  int zeroChild;
  int oneChild;
};
class Solution {
 public:
  vector<int> process(vector<vector<int>>& tasks) {
    int m = tasks.size();
    preprocess(tasks);
    vector<int> res;
    res.reserve(m);
    for (auto& t : tasks) {
      if (t.size() == 1) {
        add(t[0]);
      } else {
        int x = t[0], k = t[1], s = t[2];
        if (k < 1 || x % k != 0 || s <= x) {
          res.push_back(-1);
          continue;
        }
        res.push_back(find(k, x, s - x));
      }
    }
    return res;
  }

 private:
  int N;
  int d;
  vector<int> treeRoots;
  vector<vector<int>> factors;
  vector<Node> treeNodes;
  vector<set<int>> nums;
  void preprocess(vector<vector<int>>& tasks) {
    int N = 0;
    for (auto& t : tasks) {
      if (t.size() == 1) {
        N = max(N, t[0]);
      }
    }
    vector<int> used(N + 1, false);
    for (auto& t : tasks) {
      if (t.size() == 1) {
        used[t[0]] = true;
      }
    }
    factors.clear();
    factors.resize(N + 1);
    int cnt = 0;
    for (int i = 1; i <= N; ++i) {
      for (int j = i; j <= N; j += i) {
        if (used[j]) {
          factors[j].push_back(i);
          ++cnt;
        }
      }
    }
    treeRoots.clear();
    treeRoots.resize(N + 1);
    fill(treeRoots.begin(), treeRoots.end(), -1);
    d = 1;
    while ((1 << d) <= N) {
      ++d;
    }
    treeNodes.clear();
    treeNodes.reserve((d - 1) * cnt + N);
    nums.clear();
    nums.resize(N + 1);
  }
  int addNode() {
    treeNodes.resize(treeNodes.size() + 1);
    return treeNodes.size() - 1;
  }
  void insert(int num, int i) {
    if (treeRoots[i] == -1) {
      treeRoots[i] = addNode();
    }
    int rt = treeRoots[i];
    for (int f = 1 << (d - 1); f > 0; f >>= 1) {
      if (num & f) {
        if (treeNodes[rt].oneChild < 0) {
          treeNodes[rt].oneChild = addNode();
        }
        rt = treeNodes[rt].oneChild;
      } else {
        if (treeNodes[rt].zeroChild < 0) {
          treeNodes[rt].zeroChild = addNode();
        }
        rt = treeNodes[rt].zeroChild;
      }
    }
  }
  void add(int num) {
    for (auto i : factors[num]) {
      insert(num, i);
      nums[i].insert(num);
    }
  }
  int find(int i, int x, int maxVal) {
    int res = 0;
    int rt = treeRoots[i];
    auto it = nums[i].upper_bound(maxVal);
    if (it == nums[i].begin()) {
      return -1;
    }
    maxVal = *(--it);
    for (int f = 1 << (d - 1); f > 0; f >>= 1) {
      if (rt < 0) {
        return -1;
      }
      int nxt = -1;
      if (x & f) {
        nxt = treeNodes[rt].zeroChild;
        if (nxt < 0) {
          nxt = treeNodes[rt].oneChild;
          res |= f;
        }
        if (res > maxVal) {
          return -1;
        }
      } else {
        nxt = treeNodes[rt].oneChild;
        if (nxt >= 0 && (res | f) <= maxVal) {
          res |= f;
        } else {
          nxt = treeNodes[rt].zeroChild;
        }
      }
      rt = nxt;
    }
    return res;
  }
  void dfs(int u, int v, string& path) {
    if (u == -1) {
      return;
    }
    path.push_back('0' + v);
    if (treeNodes[u].oneChild == -1 && treeNodes[u].zeroChild == -1) {
      cout << path << endl;
    }
    dfs(treeNodes[u].zeroChild, 0, path);
    dfs(treeNodes[u].oneChild, 1, path);
    path.pop_back();
  }
  void print() {
    cout << "Trees " << endl;
    for (auto c : treeRoots) {
      if (c == -1) {
        continue;
      }
      string path;
      cout << "root " << c << endl;
      dfs(treeRoots[c], 0, path);
    }
  }
};
int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  vector<vector<int>> qs;
  qs.reserve(q);
  for (int i = 0, t = 0, v = 0, x = 0, k = 0, s = 0; i < q; ++i) {
    cin >> t;
    if (t == 1) {
      cin >> v;
      qs.emplace_back(vector<int>{v});
    } else {
      cin >> x >> k >> s;
      qs.emplace_back(vector<int>{x, k, s});
    }
  }
  Solution sol;
  auto res = sol.process(qs);
  for (auto r : res) {
    cout << r << endl;
  }
  return 0;
}
