#include <iostream>
#include <fstream>
using namespace std;
ifstream in("iepuri.in");
ofstream out("iepuri.out");
const int MOD = 666013;
class matrix
{
private:
struct matrice
{
long long a, b, c;
long long d, e, f;
long long g, h, i;
} mat;
public:
matrix();
matrix(long long A, long long B, long long C, long long D, long long E, long long F, long long G, long long H, long long I);
matrix(long long a, long long b, long long c);
long long get(long long c1, long long c2, long long c3);
matrix &operator *(const matrix &i);
matrix &operator =(const matrix &i);
matrix &operator ^(long long x);
matrix &operator %(const int x);
friend ostream &operator<< (ostream &cout, const matrix &i);
};
long long matrix ::get(long long c1, long long c2, long long c3) {
return (((this -> mat.a * c1) % MOD + (this -> mat.b * c2) % MOD ) % MOD + (this -> mat.c * c3) % MOD) % MOD;
}
matrix :: matrix()
{
mat.a = mat.b = mat.c = mat.d = mat.e = mat.f = mat.g = mat.h = mat.i = 0;
}
matrix :: matrix(long long A, long long B, long long C, long long D, long long E, long long F, long long G, long long H, long long I)
{
mat.a = A;
mat.b = B;
mat.c = C;
mat.d = D;
mat.e = E;
mat.f = F;
mat.g = G;
mat.h = H;
mat.i = I;
}
matrix :: matrix(long long A, long long B, long long C)
{
mat.a = A;
mat.b = B;
mat.c = C;
mat.d = 1;
mat.e = 0;
mat.f = 0;
mat.g = 0;
mat.h = 1;
mat.i = 0;
}
matrix& matrix ::operator*(const matrix &i)
{
long long A, B, C, D, E, F, G, H, I;
A = (((this -> mat.a * i.mat.a) % MOD + (this -> mat.b * i.mat.d) % MOD) % MOD + (this -> mat.c * i.mat.g) % MOD ) % MOD;
B = (((this -> mat.a * i.mat.b) % MOD + (this -> mat.b * i.mat.e) % MOD) % MOD + (this -> mat.c * i.mat.h) % MOD ) % MOD;
C = (((this -> mat.a * i.mat.c) % MOD + (this -> mat.b * i.mat.f) % MOD) % MOD + (this -> mat.c * i.mat.i) % MOD ) % MOD;
D = (((this -> mat.d * i.mat.a) % MOD + (this -> mat.e * i.mat.d) % MOD) % MOD + (this -> mat.f * i.mat.g) % MOD ) % MOD;
E = (((this -> mat.d * i.mat.b) % MOD + (this -> mat.e * i.mat.e) % MOD) % MOD + (this -> mat.f * i.mat.h) % MOD ) % MOD;
F = (((this -> mat.d * i.mat.c) % MOD + (this -> mat.e * i.mat.f) % MOD) % MOD + (this -> mat.f * i.mat.i) % MOD ) % MOD;
G = (((this -> mat.g * i.mat.a) % MOD + (this -> mat.h * i.mat.d) % MOD) % MOD + (this -> mat.i * i.mat.g) % MOD ) % MOD;
H = (((this -> mat.g * i.mat.b) % MOD + (this -> mat.h * i.mat.e) % MOD) % MOD + (this -> mat.i * i.mat.h) % MOD ) % MOD;
I = (((this -> mat.g * i.mat.c) % MOD + (this -> mat.h * i.mat.f) % MOD) % MOD + (this -> mat.i * i.mat.i) % MOD ) % MOD;
mat.a = A;
mat.b = B;
mat.c = C;
mat.d = D;
mat.e = E;
mat.f = F;
mat.g = G;
mat.h = H;
mat.i = I;
return *this;
}
matrix& matrix::operator=(const matrix &i)
{
this -> mat.a = i.mat.a;
this -> mat.b = i.mat.b;
this -> mat.c = i.mat.c;
this -> mat.d = i.mat.d;
this -> mat.e = i.mat.e;
this -> mat.f = i.mat.f;
this -> mat.g = i.mat.g;
this -> mat.h = i.mat.h;
this -> mat.i = i.mat.i;
return *this;
}
matrix& matrix::operator^(long long x)
{
matrix P(1LL * 1, 1LL * 0, 1LL * 0, 1LL * 0, 1LL * 1, 1LL * 0, 1LL * 0, 1LL * 0, 1LL * 1);
while(x != 1)
{
if (x % 2 == 1)
{
x--;
P = (P * *this);
P = P % MOD;
}
x /= 2;
*this = (*this * *this);
*this = *this % MOD;
}
*this = *this * P;
*this = *this % MOD;
return *this;
}
matrix& matrix ::operator%(const int x)
{
this -> mat.a = this -> mat.a % x;
this -> mat.b = this -> mat.b % x;
this -> mat.c = this -> mat.c % x;
this -> mat.d = this -> mat.d % x;
this -> mat.e = this -> mat.e % x;
this -> mat.f = this -> mat.f % x;
this -> mat.g = this -> mat.g % x;
this -> mat.h = this -> mat.h % x;
this -> mat.i = this -> mat.i % x;
return *this;
}
ostream &operator<<(ostream &cout, const matrix &i)
{
cout << i.mat.a << " " << i.mat.b << " " << i.mat.c << "\n";
cout << i.mat.d << " " << i.mat.e << " " << i.mat.f << "\n";
cout << i.mat.g << " " << i.mat.h << " " << i.mat.i << "\n";
return cout;
}
int t, _1, _2, _3, coef1, coef2, coef3, n;
int main()
{
in >> t;
for(int i = 0; i < t; i++){
in >> _1 >> _2 >> _3 >> coef1 >> coef2 >> coef3 >> n;
matrix C(coef1, coef2, coef3);
matrix D;
D = C ^ (n - 2);
out << D.get(_3, _2, _1) << "\n";
}
return 0;
}