1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include "dzilib.h"
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DB
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
  bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }

using LL = long long;
using ULL = unsigned long long;


template<class T, class U, class Op>
T fastop(T a, U b, T r, const Op &op) {
  while (b > 0) {
    if (b % 2 == 1) { r = op(r, a); }
    a = op(a, a);
    b /= 2;
  }
  return r;
}

// MILLER-RABIN

template<class T>
bool witness(T wit, T n) {
  if (wit >= n) {return false; }
  
  auto addmod = [&](const T &a, const T &b) { return (a + b) % n; };
  auto mulmod = [&](const T &a, const T &b) { return fastop(a, b, T(0), addmod); };
  auto powmod = [&](const T &a, const T &b) { return fastop(a, b, T(1), mulmod); };
  int s; T t;
  for(s = 0, t = n - 1; t % 2 == 0; s++, t /= 2);
  wit = powmod(wit, t);
  if(wit == 1 || wit == n - 1)
    return false;
  
  for(int i = 1; i < s; ++i) {
    wit = mulmod(wit, wit);
    
    if(wit == 1)
      return true;
    if(wit == n - 1)
      return false;
  }
  
  return true;
}

// Is n prime?
template<class T>
bool miller(T n) {
  if(n == 2)
    return true;
  if(n % 2 == 0 || n < 2)
    return false;
  
  if(witness(T(2), n) || witness(T(7), n) || witness(T(3), n) || witness(T(5), n) || witness(T(11), n) || witness(T(13), n) || witness(T(17), n))
    return false;
  
  return true;
}

// BABY STEP - GIANT STEP

// Returns dicrete log_a val (mod mod) 
// -1 if no such exponent exists
LL disc_log(LL a, LL val, LL mod) {
  LL d = __gcd(a, mod);
  if(d != 1 && d != mod)
    return -1;
  
  LL sq = sqrt(mod) + 2;
  
  auto mulmod = [&](const LL &a, const LL &b) { return ((__int128_t) a * b) % mod; };
  auto powmod = [&](const LL a, const LL b) { return fastop(a, b, 1ll, mulmod); };
  
  map<LL, int> M;
  
  LL b = 1, c;
  for(int i = 0; i <= sq; ++i) {
    M[b] = i;
    b = mulmod(b, a);
  }
  
  c = powmod(a, mod - 1 - sq);
  b = val;
  for(int i = 0; i <= sq; ++i) {
    if(M.count(b))
      return i * sq + M[b];
    
    b = mulmod(b, c);
  }
  
  return -1;
}

// SOLOVAY-STRASSEN

// Computes jacobi symbol
// m must be odd
// if m is prime, then the result is 0 if m | n, 1 if n is a quadr. resid. mod m, -1 otherwise
template<class T>
int jacobi(T n, T m) {
  int r = 1;
  n %= m;
  while(n > 1) {
    while(n % 2 == 0) {
      n /= 2;
      if(m % 8 == 3 || m % 8 == 5)
        r *= -1;
    }
    
    swap(n, m);
    if(n % 4 == 3 && m % 4 == 3)
      r *= -1;
    
    n %= m;
  }
  
  return r;
}

template<class T>
bool solovay_witness(const T &a, const T &n) {
  T d = __gcd(a, n);
  
  if(d != 1 && d != n)
    return true;
  if(d == n)
    return false;
  
  auto addmod = [&](const T &a, const T &b) { return (a + b) % n; };
  auto mulmod = [&](const T &a, const T &b) { return fastop(a, b, T(0), addmod); };
  auto powmod = [&](const T &a, const T &b) { return fastop(a, b, T(1), mulmod); };
  
  return (T(jacobi(a, n)) - powmod(a, (n - 1)/2)) % n != 0;
}

// Is n prime?
template<class T>
bool solovay(T n) {
  if(n == 2)
    return true;
  if(n % 2 == 0 || n < 2)
    return false;
  
  if(solovay_witness(T(2), n))
    return false;
  
  for(int i = 0; i < 40; ++i)
    if(solovay_witness(T(2 + rand() % 1000), n))
      return false;
    
    return true;
}


// POLARD'S RHO

// a - parameter, shall not be equal to 0 or -2.
// returns a divisor, a proper one when succeeded, equal to n if failed
// in case of failure, change a
template<class T>
T rho(const T &n, const T a) {
  auto addmod = [&](const T &a, const T &b) { return (a + b) % n; };
  auto mulmod = [&](const T &a, const T &b) { return fastop(a, b, T(0), addmod); };
  auto f = [&](const T &x) { return addmod(mulmod(x, x), a); };
  
  T x = 2, y = 2;
  while(true) {
    x = f(x);
    y = f(f(y));
    T d = __gcd(n, abs(x - y));
    if(d != 1) 
      return d;
  }
}

template<class T>
T get_factor(T n) {
  if(n % 2 == 0)
    return 2;
  
  if(n % 3 == 0)
    return 3;
  
  if(n % 5 == 0)
    return 5;
  
  while(true) {
    T d = rho(n, T(rand()%100 + 2));
    if(d != n)
      return d;
  }
}

//const int N = 500e6;
const int M = 1e7;
const int N = M;
//int32_t is_sq1[N];
int32_t sito[N];

template<class T>
void __factorize(const T &n, map<int, int> &x) {
  int tmp = n;
  if (n < N) {
    while (tmp > 1) {
      x[sito[tmp]]++;
      tmp /= sito[tmp];
    }
    return;
  }
  if(n == 1)
    return;
  else if(miller(n))
    x[n]++;
  else {
    T d = get_factor(n);
    __factorize(d, x);
    __factorize(n / d, x);
  }
}

template<class T>
int CountDivs(const T &n) {
  map<int, int> x;
  __factorize(n, x);
  //debug(x);
  int res = 1;
  for (auto& p : x) {
    res = res * (p.nd + 1);
  }
  //debug(n, res);
  return res;
}

int jajo = 55066939520818;

int qs;
int all_qs;
int remq;
int Askk(int x) {
  remq--;
  qs++;
#ifdef LOCAL
  all_qs++;
  return CountDivs(x + jajo);
#else
  return Ask(x);
#endif
}

void Answerr(int x) {
  //debug(x);
#ifdef LOCAL
  assert(x == jajo);
#else
  Answer(x);
#endif
}



int32_t main() {

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  cin.tie(0);
  //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  
  
  int glupie = 0;
#ifdef LOCAL
//   int t = 10;
//   int q = 100;
//   int c = 1e17;
//   int n = 1e14;
//   int cl = clock();
//   cl = 20889;
//   debug(cl);
//   srand(cl);
//   jajo = rand() % 1000000;
  int t = GetT();
  int q = GetQ();
  long long c = GetC();
  long long n = GetN();
  FOR (i, 2, N - 1) {
    if (sito[i] == 0) {
      for (int j = 1; i * j < N; j++) {
        sito[i * j] = i;
      }
    }
  }
  //srand(446);
#else
  int t = GetT();
  int q = GetQ();
  long long c = GetC();
  long long n = GetN();
#endif
  remq = q;
  REP (ind, t) {
    int rem_tests = t - ind;
    //LD rem_avg_q = 1. * remq / rem_tests;
#ifdef DB
    jajo = GetJajo();
#endif
    int heh = c / 10 + 2137;
    int y;
    for (int j = rand() % 8; ; j++) {
      int r = heh + 8 * (rand() % 20000) + j;;
      int fail = 0;
      REP (i, 6) {
        if (Askk(r + i * 8) % 3) {
          fail = 1;
          break;
        }
      }
      if (fail == 0) {
        y = r;
        break;
      }
    }
    //debug(qs);
    //debug(jajo + y, (jajo + y) % 8);
    //cerr<<(jajo + y<<endl;
    glupie += qs;
    int pot = 2;
    set<int> niefajne{5, 7, 9, 15, 21};
    int mask = 0;
    if (remq >= 73 * rem_tests + 4) {
      niefajne.insert(27);
      mask++;
    }
    if (remq >= 75 * rem_tests + 4) {
      niefajne.insert(11);
      mask += 2;
    }
    if (remq >= 78 * rem_tests + 4) {
      niefajne.insert(13);
      mask += 4;
    }
    if (remq >= 81 * rem_tests + 4) {
      niefajne.insert(35);
      mask += 8;
    }
    if (remq >= 84 * rem_tests + 4) {
      niefajne.insert(25);
      mask += 16;
    }
    if (remq >= 87 * rem_tests + 4) {
      niefajne.insert(45);
      mask += 32;
    }
    //debug(mask);
    while ((1ll << pot) < n) {
      VI cands;
      y += (rand() % 100) * (1ll << (pot + 1));
      for (int j = 1; j <= 7; j += 2) {
        cands.PB(y + (j << pot));
      }
      //random_shuffle(ALL(cands));
      VI anses;
      if (niefajne.count(pot + 3)) {
        for (auto cand : cands) {
          anses.PB(Askk(cand));
        }
        int cnt_good = 0;
        for (auto a : anses) {
          if (a % (pot + 3) == 0) {
            cnt_good++;
          }
        }
        //debug(pot);
        assert(cnt_good >= 1);
        if (cnt_good > 1) {
          //debug(pot, "powtorka");
          continue;
        }
        REP (i, 4) {
          if (anses[i] % (pot + 3) == 0) {
            y = cands[i];
          }
        }
      } else {
        int div1 = Askk(cands[0]);
        if (div1 % (pot + 3) == 0) {
          y = cands[0];
        } else {
          if (div1 % (pot + 4) == 0 || div1 % (pot + 5) == 0 || div1 % (pot + 6) == 0) {
            swap(cands[1], cands[2]);
          }
          int found = 0;
          RE (j, 2) {
            int d = Askk(cands[j]);
            if (d % (pot + 3) == 0) {
              y = cands[j];
              found = 1;
              break;
            }
          }
          if (!found) {
            y = cands[3];
          }
        }
//         for (auto cand : cands) {
//           if (Askk(cand) % (pot + 3) == 0) {
//             y = cand;
//             break;
//           }
//         }
      }
      pot += 2;
      //debug(pot, (jajo + y) % (1ll << pot));
#ifdef DB
      assert((jajo + y) % (1ll << (pot + 1)) == (1ll << (pot)));
#endif
    }
    int po = (1ll << pot);
    int ce = (y + po - 1) / po;
    debug(mask, qs);
    Answerr(ce * po - y);
    qs = 0;
  }
  debug(all_qs, glupie);
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  return 0;
}