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
#include "dzilib.h"
#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) (int((x).size()))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

// https://github.com/tonowak/acmlib/blob/master/code/math/miller-rabin/main.cpp
ll llmul(ll a, ll b, ll m){
	return ll(__int128_t(a)*b%m);
}
ll llpowi(ll a, ll n, ll m){
	for(ll ret = 1; ; n /= 2ll){
		if(n == 0ll) return ret;
		if(n&1ll) ret = llmul(ret, a, m);
		a = llmul(a, a, m);
	}
}
bool miller_rabin(ll n) {
	if(n < 2) return false;
	int r = 0;
	ll d = n-1ll;
	while(~d&1ll) d>>=1, ++r;
	for(int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}){
		if(!(a%n)) continue;
		ll x = llpowi(a, d, n);
		if(x==1 || x==n-1) continue;
		bool composite = true;
		REP(i, r-1){
			x = llmul(x, x, n);
			if(x == n-1ll){composite = false; break;}
		}
		if(composite) return false;
	}
	return true;
}

ll dzielniki(ll n){
	ll ret = 1ll;
	for(ll i = 2ll; i*i*i <= n; ++i){
		ll ile = 0ll;
		while(!(n%i)) n /= i, ++ile;
		ret *= ile+1ll;
	}
	if(n > 1){
		if(miller_rabin(n)) return ret<<1;
		ll pier = roundl(sqrtl(n));
		if(pier*pier == n) ret *= 3ll;
		else ret *= 4ll;
	}
	return ret;
}

// https://github.com/tonowak/acmlib/blob/master/code/math/extended-gcd/main.cpp
tuple<ll, ll, ll> extended_gcd(ll a, ll b) {
	if(!a) return {b, 0, 1};
	auto [gcd, x, y] = extended_gcd(b%a, a);
	return {gcd, y-x*(b/a), x};
}

// https://github.com/tonowak/acmlib/blob/master/code/math/crt/main.cpp
ll crt(ll a, ll m, ll b, ll n) {
	if(n > m) swap(a, b), swap(m, n);
	auto [d, x, y] = extended_gcd(m, n);
	ll ret = (b-a)%n*x%n/d*m + a;
	return ret < 0 ? ret + m*n/d : ret;
}

mt19937 mt(2137);
uniform_int_distribution<ll> dist(0, 1e18);

void solve(){
	int q = GetQ();
	ll c = GetC();
	ll n = GetN();
	
	ll los = n+dist(mt)%n;
	
	ll wiem_resz = 0ll;
	ll wiem_mod = 1ll;
	auto informacja = [&](ll resz, ll mod){
		resz = (((resz-los)%mod)+mod)%mod;
		wiem_resz = crt(wiem_resz, wiem_mod, resz, mod);
		wiem_mod *= mod;
	};
	
	unordered_map<ll, ll> mapa;
	auto zapytaj = [&](ll x){
		if(!mapa[x]) mapa[x] = Ask(los+x);
		return mapa[x];
	};
	
	auto a_moze_wiem = [&](){
		if(wiem_mod >= n){
			ll ret = wiem_resz;
			if(!ret) ret += wiem_mod;
			return Answer(ret), 1;
		}
		if(wiem_mod > 148176000ll){
			vector<ll> vec_kand;
			for(ll ret = wiem_resz; ret <= n; ret += wiem_mod) if(ret){
				bool git = 1;
				for(auto [off, dziel] : mapa) if(dzielniki(ret+off+los) != dziel){git = 0; break;}
				if(git) vec_kand.emplace_back(ret);
				if(ssize(vec_kand) > 1) break;
			}
			//~ printf("murzyn: %d\n", ssize(vec_kand));
			if(ssize(vec_kand) == 1) return Answer(vec_kand[0]), 1;
		}
		return 0;
	};
	
	vector<pii> kwadraty;
	
	{
		set<int> secik;
		REP(x, 128) secik.emplace(x);
		for(int off = 0; ssize(secik) > 1; off += 128){
			set<int> nowy;
			for(int x : secik){
				ll t = zapytaj(off+x);
				if(!(t%7)) nowy.emplace(x);
			}
			swap(secik, nowy);
		}
		int t = *secik.begin();
		kwadraty.emplace_back((128-t)%8, 2);
		informacja((128+64-t)%128, 128);
	}
	
	for(int p : {3, 5, 7, 11, 13}){
		int p2 = p*p;
		int p3 = p2*p;
		vector<int> czy(p3, 1);
		vector<int> chce_ujebac(p3);
		auto czy_szukaj_dalej = [&](){
			vector<int> vec_kand;
			REP(kand, p3){
				bool git = 1;
				FOR(ile, 1, p-1) if(!czy[(kand+ile*p2)%p3]) git = 0;
				if(git) vec_kand.emplace_back(kand);
			}
			if(ssize(vec_kand) == 1){
				int t = vec_kand[0];
				kwadraty.emplace_back((p3-t)%p3, p);
				informacja((p3-t)%p3, p3);
				return 0;
			}
			REP(i, p3) chce_ujebac[i] = 0;
			for(int i : vec_kand) FOR(ile, 1, p-1) chce_ujebac[(i+ile*p2)%p3] = 1;
			return 1;
		};
		for(int x = 0; czy_szukaj_dalej(); ++x) if(chce_ujebac[x%p3]){
			ll t = zapytaj(x);
			int trojki = 0;
			while(!(t%3ll)) t /= 3ll, ++trojki;
			for(auto [fi, se] : kwadraty) if((ll(fi)+x)%(se*se*se) && !((x+ll(fi))%(se*se))) --trojki;
			if(!trojki) czy[x%p3] = 0;
		}
		if(a_moze_wiem()) return;
	}
}

int main(){
	int t = GetT();
	while(t--) solve();
	return 0;
}