#include <bits/stdc++.h>
#define mod 666013
using namespace std;
ifstream fin ("iepuri.in");
ofstream fout ("iepuri.out");
void usain_bolt()
{
ios::sync_with_stdio(false);
fin.tie(0);
}
long long pwr[4][4], aux[4][4], sol[4][4];
void mult(long long a[4][4], long long b[4][4], long long c[4][4])
{
for(int i = 1; i <= 3; ++i) {
for(int j = 1; j <= 3; ++j) {
for(int k = 1; k <= 3; ++k) {
c[i][j] = (c[i][j] + 1LL * a[i][k] * b[k][j]) % mod;
}
}
}
}
void exp(int n, int x, int y, int z, int A, int B, int C)
{
pwr[1][1] = 0, pwr[1][2] = 0, pwr[1][3] = C;
pwr[2][1] = 1, pwr[2][2] = 0, pwr[2][3] = B;
pwr[3][1] = 0, pwr[3][2] = 1, pwr[3][3] = A;
sol[1][1] = x, sol[1][2] = y, sol[1][3] = z;
sol[2][1] = 0, sol[2][2] = 0, sol[2][3] = 0;
sol[3][1] = 0, sol[3][2] = 0, sol[3][3] = 0;
while(n > 0) {
if(n & 1) {
memset(aux, 0, sizeof(aux));
mult(sol, pwr, aux);
memcpy(sol, aux, sizeof(aux));
}
memset(aux, 0, sizeof(aux));
mult(pwr, pwr, aux);
memcpy(pwr, aux, sizeof(aux));
n >>= 1;
}
}
int main()
{
usain_bolt();
int tt;
fin >> tt;
for (; tt; --tt) {
int x, y, z, a, b, c, n;
fin >> x >> y >> z >> a >> b >> c >> n;
exp(n, x, y, z, a, b, c);
fout << (sol[1][1] + mod) % mod << "\n";
}
return 0;
}