/* -*- coding: utf-8 -*- * * 435.cc: No.435 占い(Extra) - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int MOD = 9; /* typedef */ typedef long long ll; /* global variables */ int dvs[MAX_N + 1]; /* subroutines */ void gen_dvs(int maxp) { for (int p = 2; p * p <= maxp; p++) if (! dvs[p]) for (int q = p * p; q <= maxp; q += p) dvs[q] = p; } ll powmod(ll a, int b) { ll pm = 1; while (b > 0) { if (b & 1) pm = pm * a % MOD; a = a * a % MOD; b >>= 1; } return pm; } ll pfs2ll(int pfs[]) { if (pfs[0] > 0) return 0; ll r = 1; for (int a = 1; a < MOD; a++) r = r * powmod((ll)a, pfs[a]) % MOD; return r; } /* main */ // nCr = n(n-1)..(n-r+1)/r! // nC(r+1) = n(n-1)..(n-r)/(r+1)! // -> nC(r+1) = nCr*(n-r)/(r+1) int main() { gen_dvs(MAX_N); int tn; scanf("%d", &tn); while (tn--) { int ni, ai, bi, mi; ll xi; scanf("%d%lld%d%d%d", &ni, &xi, &ai, &bi, &mi); int n = ni - 1; ll ans = 0; bool nz = false; int pfs[MOD] = {0}; for (int i = 0; i <= n; i++) { int d = xi % 10; if (d > 0) nz = true; ans = (ans + pfs2ll(pfs) * d) % MOD; if (i < n) { int j = n - i; while (dvs[j]) pfs[dvs[j] % MOD]++, j /= dvs[j]; pfs[j % MOD]++; int k = i + 1; while (dvs[k]) pfs[dvs[k] % MOD]--, k /= dvs[k]; pfs[k % MOD]--; } xi = ((xi ^ ai) + bi) % mi; } if (nz && ans == 0) ans = MOD; printf("%lld\n", ans); } return 0; }