#pragma warning(disable:4786) #pragma warning(disable:4996) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define MEM(a, b) memset(a, (b), sizeof(a)) #define CLR(a) memset(a, 0, sizeof(a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) ) #define S(X) ( (X) * (X) ) #define SZ(V) (int )V.size() #define FORN(i, n) for(i = 0; i < n; i++) #define FORAB(i, a, b) for(i = a; i <= b; i++) #define ALL(V) V.begin(), V.end() #define IN(A, B, C) ((B) <= (A) && (A) <= (C)) typedef pair PII; typedef pair PDD; typedef vector VI; typedef vector VP; #define AIN(A, B, C) assert(IN(A, B, C)) //typedef int LL; typedef long long int LL; //typedef __int64 LL; LL a, b, c, d, s; int cnt[1000006]; int main() { int T, ks, i, n; LL sum; scanf("%d", &T); FORAB(ks, 1, T) { scanf("%d %lld %lld %lld %lld", &n, &a, &b, &c, &d); CLR(cnt); s = d; cnt[s]++; FORAB(i, 1, n - 1) cnt[s = (a * s * s + b * s + c) % 1000000]++; sum = 0; int now = 0; FORN(i, 1000000) { if(now % 2 == 0) sum += (cnt[i] % 2) * i; else sum -= (cnt[i] % 2) * i; now += cnt[i]; } printf("%lld\n", ABS(sum)); } return 0; }