#include <bits/stdc++.h>
using namespace std;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
const long long MOD = 666013;
int t, n;
long long x, y, z, a, b, c;
void inmultire(long long A[3][3], long long B[3][3], long long C[3][3]) {
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++) {
C[i][j] = 0;
for(int k = 0; k < 3; k++)
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
}
}
}
void copiere(long long A[3][3], long long C[3][3]){
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
A[i][j] = C[i][j];
}
int main() {
fin>>t;
while(t--){
fin>>x>>y>>z>>a>>b>>c>>n;
long long mat[3][3] = {
{ a, b, c },
{ 1, 0, 0 },
{ 0, 1, 0 }
};
long long p[3][3] = {
{ 1, 0, 0 },
{ 0, 1, 0 },
{ 0, 0, 1 }
};
long long aux[3][3];
n-=2;
while(n){
if(n&1){
inmultire(p, mat, aux);
copiere(p, aux);
}
inmultire(mat, mat, aux);
copiere(mat, aux);
n>>=1;
}
fout<<(p[0][0] * z + p[0][1] * y + p[0][2] * x) % MOD<<"\n";
}
return 0;
}