#include <bits/stdc++.h>
using namespace std;
const int upper = 1e5 + 3;
vector<int> seive;
void doSeive() {
  seive.resize(upper + 1);
  seive[0] = seive[1] = 1;
  for (int i = 2; i <= upper; ++i) seive[i] = i;
  int root = ceil(sqrt(upper));
  for (int i = 2; i <= root; ++i) {
    if (seive[i] == i) {
      for (int j = 2 * i; j <= upper; j += i) seive[j] = min(seive[j], i);
    }
  }
}
void getFactors(vector<pair<int, int> > &primes, vector<int> &powers,
                vector<int> &factors, int pos) {
  if (pos == primes.size()) {
    int factor = 1;
    for (int i = 0; i < primes.size(); ++i) {
      for (int j = 0; j < powers[i]; ++j) {
        factor *= primes[i].first;
      }
    }
    factors.push_back(factor);
    return;
  }
  for (int power = 0; power <= primes[pos].second; ++power) {
    powers.push_back(power);
    getFactors(primes, powers, factors, pos + 1);
    powers.pop_back();
  }
}
const int Log = 18;
struct binaryTrie {
  struct node {
    int minElem;
    node *left, *right;
    node() {
      minElem = 1e8;
      left = right = nullptr;
    }
  };
  node *root;
  binaryTrie() { root = new node(); }
  void insert(int num) {
    node *curr = root;
    for (int bit = Log - 1; bit >= 0; --bit) {
      curr->minElem = min(curr->minElem, num);
      if (num & (1 << bit)) {
        if (curr->right == nullptr) {
          curr->right = new node();
        }
        curr = curr->right;
      } else {
        if (curr->left == nullptr) curr->left = new node();
        curr = curr->left;
      }
      curr->minElem = min(curr->minElem, num);
    }
  }
  int query(int constant, int x) {
    int sum = 0;
    node *curr = root;
    if (curr->minElem > constant) return -1;
    for (int bit = Log - 1; bit >= 0; --bit) {
      if (x & (1 << bit)) {
        if (curr->left != nullptr) {
          curr = curr->left;
        } else if (curr->right != nullptr && curr->right->minElem <= constant) {
          curr = curr->right;
          sum += (1 << bit);
        } else {
          return -1;
        }
      } else {
        if (curr->right != nullptr && curr->right->minElem <= constant) {
          curr = curr->right;
          sum += (1 << bit);
        } else if (curr->left != nullptr) {
          curr = curr->left;
        } else {
          return -1;
        }
      }
    }
    return sum;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  doSeive();
  int q;
  cin >> q;
  int type, elem, x, k, s;
  vector<bool> inserted((int)1e5 + 2, false);
  vector<binaryTrie> arr((int)1e5 + 2);
  while (q--) {
    cin >> type;
    if (type == 1) {
      cin >> elem;
      int temp = elem;
      if (inserted[elem]) continue;
      inserted[elem] = true;
      vector<pair<int, int> > factors;
      while (elem > 1) {
        int minFactor = seive[elem];
        while (seive[elem] == minFactor) {
          elem /= minFactor;
          if (factors.size() && factors.back().first == minFactor)
            factors.back().second++;
          else
            factors.push_back({minFactor, 1});
        }
      }
      vector<int> allDivisors, dummy;
      getFactors(factors, dummy, allDivisors, 0);
      for (int f : allDivisors) {
        arr[f].insert(temp);
      }
    } else if (type == 2) {
      cin >> x >> k >> s;
      if (x % k != 0) {
        cout << "-1\n";
        continue;
      }
      int constant = s - x;
      cout << arr[k].query(constant, x) << '\n';
    }
  }
  return 0;
}
