#include <bits/stdc++.h>
using namespace std;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
typedef unsigned long long int64;
struct Matrix{
int64 a, b, c, d, e, f, g, h, i;
Matrix(){
}
Matrix(int64 A, int64 B, int64 C, int64 D, int64 E, int64 F, int64 G, int64 H, int64 I)
:a{A}, b{B}, c{C}, d{D}, e{E}, f{F}, g{G}, h{H}, i{I} {
}
Matrix operator * (Matrix& X){
Matrix answer;
answer.a = (1LL*a*X.a + 1LL*b*X.d + 1LL*c*X.g)%666013;
answer.b = (1LL*a*X.b + 1LL*b*X.e + 1LL*c*X.h)%666013;
answer.c = (1LL*a*X.c + 1LL*b*X.f + 1LL*c*X.i)%666013;
answer.d = (1LL*d*X.a + 1LL*e*X.d + 1LL*f*X.g)%666013;
answer.e = (1LL*d*X.b + 1LL*e*X.e + 1LL*f*X.h)%666013;
answer.f = (1LL*d*X.c + 1LL*e*X.f + 1LL*f*X.i)%666013;
answer.g = (1LL*g*X.a + 1LL*h*X.d + 1LL*i*X.g)%666013;
answer.h = (1LL*g*X.b + 1LL*h*X.e + 1LL*i*X.h)%666013;
answer.i = (1LL*g*X.c + 1LL*h*X.f + 1LL*i*X.i)%666013;
return answer;
}
};
int64 A, B, C, X, Y, Z, N;
Matrix power(Matrix &K, int64 N){
if(N == 0){
Matrix temp(1, 0, 0, 0, 1, 0, 0, 0, 1);
return temp;
}
if(N == 1){
Matrix temp(A, B, C, 1, 0, 0, 0, 1, 0);
return temp;
}
K = power(K, N / 2);
K = K * K;
if(N & 1){
Matrix temp(A, B, C, 1, 0, 0, 0, 1, 0);
K = K * temp;
}
return K;
}
int main()
{
unsigned short T;
fin >> T;
for(; T; T--){
fin >> X >> Y >> Z >> A >> B >> C >> N;
switch(N){
case 0: fout << X;
break;
case 1: fout << Y;
break;
case 2: fout << Z;
break;
default:
Matrix K;
K = power(K, N - 2);
fout << (1LL*K.a*Z + 1LL*K.b*Y + 1LL*K.c*X)%666013 << "\n";
}
}
}