#include <fstream>
#define MOD 666013
using namespace std;
void initmat(int x, int y, int z, long long mat[]){
for(int i=0;i<8;i++){
mat[i]=0;
}
mat[3] = 1;
mat[7] = 1;
mat[2] = x;
mat[5] = y;
mat[8] = z;
}
void mult(long long mat1[], long long mat2[], long long mat3[]){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
mat3[3*i+j] = 0;
for(int a=0;a<3;a++){
mat3[3*i+j] += ((mat1[3*i+a]%MOD)*(mat2[j+3*a]%MOD))%MOD;
}
}
}
}
void copy_m(long long mat1[], long long mat2[]){
for(int i=0;i<9;i++)mat2[i]=mat1[i];
}
long long solve(int x, int y, int z, int a, int b, int c, int n){
long long mat[9], mat0[9], rezmat[9]={0}, temp[9];
initmat(a, b, c, mat);
initmat(a, b, c, mat0);
rezmat[0] = 1;
rezmat[4] = 1;
rezmat[8] = 1;
//initmat(x, y, z, rezmat);
//n -= 2;
while(n>0){
if(n%2) {
//n = (n - 1)/2;
mult(rezmat, mat, temp);
copy_m(temp, rezmat);
}
n = n / 2;
mult(mat, mat, temp);
copy_m(temp, mat);
}
long long r1, r2, r3;
r1 = ((rezmat[0]*x)%MOD)+((rezmat[3]*y)%MOD)+((rezmat[6]*z)%MOD);
return r1%MOD;
}
int main()
{
ifstream in("iepuri.in");
ofstream out("iepuri.out");
int t;
in>>t;
for(int i=0;i<t;i++){
int x, y, z, a, b, c, n;
in>>x>>y>>z>>a>>b>>c>>n;
out<<solve(x, y, z, c, b, a, n)<<"\n";
}
return 0;
}