#include <iostream>
#include <cstdio>
using namespace std;
int x, y, z, a, b, c, n, modulo = 666013;
void produsMatrici(int mat1[3][3], int mat2[3][3], int mat3[3][3])
{
int a1[3][3], b1[3][3];
for(int i=0; i<3; ++i)
for(int j=0; j<3; ++j)
{
a1[i][j] = mat1[i][j];
b1[i][j] = mat2[i][j];
}
for(int i=0; i<3; ++i)
{
for(int j=0; j<3; ++j)
{
mat3[i][j] = 0;
for(int k=0; k<3; ++k)
{
mat3[i][j] = (mat3[i][j] + (1LL *a1[i][k]*b1[k][j])%modulo)%modulo;
}
}
}
}
void afisare(int matrixAux[][5])
{
for(int i=0; i<3; ++i)
{
for(int j=1; j<=3; ++j)
printf("%d ", matrixAux[i][j]);
printf("\n");
}
}
void solve()
{
int rezultat[3][3] = {{x, y, z}, {0, 0, 0}, {0, 0, 0}};
int matrix[3][3] = {{0, 0, c}, {1, 0, b}, {0, 1, c}};
do
{
if(n%2)
{
produsMatrici(rezultat, matrix, rezultat);
}
produsMatrici(matrix, matrix, matrix);
n/=2;
}
while(n);
printf("%d\n", rezultat[0][2]);
}
int main()
{
int t;
freopen("iepuri.in", "r", stdin);
freopen("iepuri.out", "w", stdout);
scanf("%d", &t);
for(int i=1; i<=t; ++i)
{
scanf("%d%d%d%d%d%d%d", &x, &y, &z, &a, &b, &c, &n);
n = n-2;
solve();
}
return 0;
}