#include <bits/stdc++.h>
#define MOD 666013
using namespace std;
ifstream f("iepuri.in");
ofstream g("iepuri.out");
typedef vector <vector <long long>> VI;
int task;
long long x, y, z, A, B, C, n;
void product(VI a, VI b, VI &sol)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
sol[i][j] = 0;
for (int k = 0; k < 3; k++)
sol[i][j] = (sol[i][j] + (a[i][k] * b[k][j]) % MOD) % MOD;
}
}
int main()
{
f >> task;
while (task--)
{
f >> x >> y >> z >> A >> B >> C >> n;
VI sol = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, a = {{A, 1, 0}, {B, 0, 1}, {C, 0, 0}}, b = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
n -= 2;
while (n > 0)
{
if (n % 2 == 1)
{
product(a, sol, b);
sol = b;
n--;
}
product(a, a, b);
a = b;
n /= 2;
}
g << (z * sol[0][0] + y * sol[1][0] + x * sol[2][0]) % MOD << "\n";
}
return 0;
}