#include using namespace std; long long mod = 1e8+9; //入力が必ず-mod= mod) v -= mod; return *this; } mint operator-=(const mint b){ v -= b.v; if(v < 0) v += mod; return *this; } mint operator*=(const mint b){v = v*b.v%mod; return *this;} mint operator/=(mint b){ if(b == 0) assert(false); int left = mod-2; while(left){if(left&1) *this *= b; b *= b; left >>= 1;} return *this; } mint operator++(){*this += 1; return *this;} mint operator--(){*this -= 1; return *this;} mint operator++(int){*this += 1; return *this;} mint operator--(int){*this -= 1; return *this;} bool operator==(const mint b)const{return v == b.v;} bool operator!=(const mint b)const{return v != b.v;} bool operator>(const mint b)const{return v > b.v;} bool operator>=(const mint b)const{return v >= b.v;} bool operator<(const mint b)const{return v < b.v;} bool operator<=(const mint b)const{return v <= b.v;} mint pow(long long n)const{ mint ret = 1,p = v; if(n < 0) p = p.inv(),n = -n; while(n){ if(n&1) ret *= p; p *= p; n >>= 1; } return ret; } mint inv()const{return mint(1)/v;} }; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int Need = 100; vector>> Pf(Need+1); vector prime(Need+1,true); prime.at(0) = false; prime.at(1) = false; for(int i=2; i<=Need; i++){ if(!prime.at(i)) continue; Pf.at(i) = {{i,1}}; for(int k=i+i; k<=Need; k+=i){ prime.at(k) = false; int f = 0,k2 = k; while(k2%i == 0) f++,k2 /= i; Pf.at(k).push_back({i,f}); } } int Q; cin >> Q; while(Q--){ mint v; cin >> v.v; v.v %= mod; int N,K,B; cin >> N >> K >> B; auto P = Pf.at(B); int n = P.size(); vector> C(n,vector(31)); for(int i=0; i<=N; i++){ if(v == 0) v.v = mod; int idx = 0; for(auto [p,e] : P){ int es = 0,x = v.v; while(x%p == 0) es++,x /= p; C.at(idx).at(es)++; idx++; } v = v*v+v*12345+1; } { int answer = 1001001001,idx = 0; for(auto [p,e] : P){ int left = K,now = 0; for(int i=0; i<=30; i++){ int ope = min(left,C.at(idx).at(i)); now += ope*i; left -= ope; } answer = min(answer,now/e); idx++; } cout << answer << "\n"; } } }