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
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include "dzilib.h"
#include<bits/stdc++.h>
using namespace std;

#define pb push_back

typedef long long ll;
typedef long double ld;
int t, q;
long long c, n;
mt19937_64 rnd(32313131);
int TOT_W[60];
ll gen(ll C, ll mod, ll res) {
    res %= mod;
    if (res < 0) res += mod;
    assert(res < mod && res <= C);
    ll F = rnd() % ((C - res) / mod + 1);
    ll U = F * mod + res;
    assert(U <= C);
    return U;
}
ll TOT_Q = 0;
ll TOT_QQ = 0;
const int maxM = 500'005;
ll F[maxM];

const ll LIM = (1LL << 50) + (ll)1e14 + 1000;
void solve() {
    //init by mod 8
    //I will add my_shift + c / 10
    vector<int> S(8);
    iota(S.begin(), S.end(), 0);
    shuffle(S.begin(), S.end(), rnd);
    c = min(c, (1LL << 50) + 3);
    assert(c + n <= LIM);
    while (true) {
        vector<int> nS;
        shuffle(S.begin(), S.end(), rnd);
        for (auto& it : S) {
            if (nS.empty() && it == S.back()) {
                nS.emplace_back(it);
                break;
            }
            ll T = gen(c, 8, it);
            TOT_QQ += 1;
            int di = Ask(T);
            TOT_Q += 1;
            if (di % 3 != 0) {
            }
            else {
                nS.emplace_back(it);
            }
        }
        swap(S, nS);
        if (S.size() == 1) break;
    }
    ll PW = 8;
    ll cur_res = S[0];
    int ex = 3;
    /*S.resize(4);
    cur_res += 4;
    iota(S.begin(), S.end(), 0);
    shuffle(S.begin(), S.end(), rnd);
    while (true) {
        vector<int> nS;
        for (auto& it : S) {
            ll T = gen(c, 32, it * 8 + cur_res);
            int di = Ask(T);
            TOT_Q += 1;
            if (di % 5 != 0) {
            }
            else {
                nS.emplace_back(it);
            }
        }
        swap(S, nS);
        if (S.size() == 1) break;
    }
    cur_res += S[0] * 8;
    ex += 2;
    PW = 32;*/


    bool FLAG = true;
#ifndef DEBUG
    FLAG = (q <= 820);
#endif
    //q <= 820

    while (PW < n) {
        cur_res += PW / 2;
        cur_res %= PW;
        PW *= 2;
        ex += 1;
        if (PW != 16 && PW != 256 && PW != 65536) {
            ll WW = 0;
            while (true) {
                int z = rnd() % 2;
                bool fnd = false;
                for (int u : {z, 1 - z}) {
                    ll T = gen(c, PW, (cur_res + u * PW / 2));
                    WW += 1;
                    int d = Ask(T);
                    TOT_W[ex] += 1;
                    TOT_Q += 1;
                    bool has_any = false;
                    for (int z = ex + 1; z <= 60; z++) {
                        if (d % z == 0) {
                            if (d / z < maxM && F[d / z] != -1 && F[d / z] <= (LIM / (1LL << (z - 1)))) {
                                //2^{z - 1}
                                has_any = true;
                            }
                        }
                    }
                    if (d % ex != 0) {
                        int any_val = -1;
                        for (int z = ex + 1; z <= 60; z++) {
                            if (d % z == 0 && d / z < maxM && F[d / z] != -1 && F[d / z] <= (LIM / ((1LL << (z - 1))))) {
                                if (any_val == -1) any_val = z;
                                else if (any_val > 0) any_val = -2;
                            }
                        }
                        assert(any_val != -1);
                        if (any_val != -2) {
                            while (ex < any_val) {
                                ex += 1;
                                PW *= 2;
                            }
                            cur_res = T % PW;
                            cur_res %= PW;
                        }
                        else {
                            cur_res += (1 - u) * PW / 2;
                            cur_res %= PW;
                        }
                        fnd = true;
                        break;
                    }
                    else if (!has_any) {
                        cur_res += u * PW / 2;
                        cur_res %= PW;
                        fnd = true;
                        break;
                    }
                }
                if (fnd) break;
            }
        }
        else {
            S.resize(4);
            iota(S.begin(), S.end(), 0);
            shuffle(S.begin(), S.end(), rnd);
            while (true) {
                shuffle(S.begin(), S.end(), rnd);
                vector<int> nS;
                for (auto &it: S) {
                    if (nS.empty() && it == S.back()) {
                        nS.emplace_back(it);
                        break;
                    }
                    ll T = gen(c, PW * 2, it * (PW / 2) + cur_res);
                    int di = Ask(T);
                    TOT_Q += 1;
                    if (di % (ex + 1) != 0) {
                    } else {
                        nS.emplace_back(it);
                    }
                }
                swap(S, nS);
                if (S.size() == 1) break;
            }
            ex += 1;
            cur_res += (PW / 2) * S[0];
            PW *= 2;
        }
    }
    //x + cur_res == PW / 2 mod PW
    ll my_val = (PW / 2 - cur_res) % PW;
    if (my_val <= 0) my_val += PW;
    assert(my_val <= n);
    Answer(my_val);
}
//map<ll,int> F;
vector<ll> primes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 53};
int MX = 0;
void prec(ll num, int pos, ll dv) {
    assert(pos < (int)primes.size());
    assert(dv < maxM);
    if (F[dv] == -1) F[dv] = num;
    else F[dv] = min((ll)F[dv], num);
    MX = max(MX, (int)dv);
    for (ll d = 1; ; d++) {
        if ((ld)num * primes[pos] > LIM + 10) break;
        num *= primes[pos];
        prec(num, pos + 1, dv * (d + 1));
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    memset(F, -1, sizeof F);
    prec(1, 0, 1);
//    cout << F.size() << " " << MX << endl;
//    exit(0);
    t = GetT();
    q = GetQ();
    c = GetC();
    n = GetN();
    while (t--) {
        solve();
    }
//    cout << TOT_Q << endl;
    return 0;
}