#include <bits/stdc++.h>
using namespace std;
const int MOD = 666013;
const int MAXK = 3;
struct Matrix {
int n;
long long mat[MAXK][MAXK];
Matrix(int _n = 0, bool identity = false) {
n = _n;
memset(mat, 0, sizeof(mat)); // Initializam toate elementele cu 0
if (identity) {
for (int i = 0; i < n; ++i) {
mat[i][i] = 1;
}
}
}
Matrix operator*(const Matrix& other) const {
Matrix res(n);
// Ordinea i, k, j optimizeaza accesul la memoria cache
for (int i = 0; i < n; ++i) {
for (int k = 0; k < n; ++k) {
if (mat[i][k] == 0) continue; // Sare peste inmultirile cu 0
for (int j = 0; j < n; ++j) {
res.mat[i][j] = (res.mat[i][j] + mat[i][k] * other.mat[k][j]) % MOD;
}
}
}
return res;
}
};
Matrix power(Matrix base, long long exp) {
Matrix res(base.n, true); // Initializam cu matricea unitate
while (exp > 0) {
if (exp & 1) res = res * base; // Daca bitul curent e 1
base = base * base; // Ridicam baza la patrat
exp >>= 1; // Trecem la urmatorul bit
}
return res;
}
int main()
{
long long i,j,n=3,x,y,z,a,b,c,k,t;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
fin>>t;
Matrix A(n);
for(i=1;i<=t;i++){
fin>>x>>y>>z>>a>>b>>c>>k;
if(k==0)
fout<<x%MOD<<endl;
else if(k==1)
fout<<y%MOD<<endl;
else if(k==2)
fout<<z%MOD<<endl;
else{
A.mat[0][0] = a; A.mat[0][1] = b; A.mat[0][2] = c;
A.mat[1][0] = 1; A.mat[1][1] = 0; A.mat[1][2] = 0;
A.mat[2][0] = 0; A.mat[2][1] = 1; A.mat[2][2] = 0;
Matrix ans=power(A,k-2);
fout<<(ans.mat[0][0]*z + ans.mat[0][1]*y+ans.mat[0][2]*x)%MOD<<endl;
}
}
return 0;
}