#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
int n;
int tip;
bool pres[100002];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> tip;
    if (tip == 1) {
      int nr;
      cin >> nr;
      pres[nr] = 1;
    } else {
      int a, b, c;
      cin >> a >> b >> c;
      if (a % b != 0)
        cout << -1 << '\n';
      else {
        int k = (c - a) - (c - a) % b;
        int sol = -1, nr = 0;
        while (k > 0) {
          if (pres[k] && (a ^ k) >= sol) sol = a ^ k, nr = k;
          k -= b;
          if (a + k <= sol) break;
        }
        if (nr == 0)
          cout << -1 << '\n';
        else
          cout << nr << '\n';
      }
    }
  }
  return 0;
}
