#include <bits/stdc++.h>
using namespace std;
FILE* fin = fopen("iepuri.in", "r");
FILE* fout = fopen("iepuri.out", "w");
const int MOD = 666.013;
struct matrice
{
long long v[3][3];
};
matrice multiplication(matrice a, matrice b)
{
matrice rez;
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 3; column++)
{
rez.v[row][column] = 0;
for (int i = 0; i < 3; i++)
rez.v[row][column] += a.v[row][i] * b.v[i][column];
rez.v[row][column] %= MOD;
}
}
return rez;
}
matrice putere(matrice a, int n)
{
matrice rez = {{{1, 0, 0},
{0, 1, 0},
{0, 0, 1}}};
while (n != 0)
{
if (n % 2 == 1)
rez = multiplication(rez, a);
a = multiplication(a, a);
n /= 2;
}
return rez;
}
void solve()
{
int x, y, z, a, b, c, n;
fscanf(fin, "%d %d %d %d %d %d %d", &x, &y, &z, &a, &b, &c, &n);
matrice m = {{{a, b, c},
{1, 0, 0},
{0, 1, 0}}};
matrice p = putere(m, n - 2);
int rez = (p.v[0][0] * z + p.v[0][1] * y + p.v[0][2] * x) % MOD;
fprintf(fout, "%d\n", rez);
}
int main()
{
int t;
fscanf(fin, "%d", &t);
while(t--)
solve();
return 0;
}