#include <bits/stdc++.h>
#define scanf (void)!scanf
const int MOD = 666013;
using namespace std;
inline void Open(const string Name) {
#ifndef ONLINE_JUDGE
(void)!freopen((Name + ".in").c_str(), "r", stdin);
(void)!freopen((Name + ".out").c_str(), "w", stdout);
#endif
}
void MatProduct(int M[][3], int F[][3]) {
int ans[3][3];
memset(ans, 0, sizeof(ans));
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 3; ++j)
for(int k = 0; k < 3; ++k)
ans[i][j] = (1LL * ans[i][j] + (1LL * M[i][k] * F[k][j]) % MOD) % MOD;
memcpy(M, ans, sizeof(ans));
}
void binpow(int Mat[][3], int N) {
int Res[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
while(N) {
if(N & 1) MatProduct(Res, Mat);
MatProduct(Mat, Mat);
N >>= 1;
}
memcpy(Mat, Res, sizeof(Res));
}
void solve() {
int X, Y, Z, A, B, C, N;
scanf("%d%d%d%d%d%d%d", &X, &Y, &Z, &A, &B, &C, &N);
int M[3][3] = {{A, B, C}, {1, 0, 0}, {0, 1, 0}};
int F[3][1] = {{Z}, {Y}, {X}};
binpow(M, N - 2);
int ans[3][1];
memset(ans, 0, sizeof(ans));
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 1; ++j)
for(int k = 0; k < 3; ++k)
ans[i][j] = (1LL * ans[i][j] + (1LL * M[i][k] * F[k][j]) % MOD) % MOD;
printf("%d\n", ans[0][0]);
}
int main() {
Open("iepuri");
int T;
scanf("%d", &T);
while(T--) {
solve();
}
return 0;
}