#include <cstdio>
#include <cstring>
#define maxn 5
#define mod 666013
using namespace std;
int mat[maxn][maxn], rez[maxn][maxn], Bmat[maxn][maxn];
void mul (int A[][maxn], int B[][maxn])
{
int AUX[maxn][maxn], i, j, k;
memset (AUX, 0, sizeof (AUX));
for (i = 1; i <= 3; i++)
for (j = 1; j <= 3; j++)
for (k = 1; k <= 3; k++)
AUX[i][j] = (1LL * A[i][k] * B[k][j] + AUX[i][j]) % mod;
memcpy (A, AUX, sizeof (AUX));
}
int main ()
{
freopen ("iepuri.in", "r", stdin);
freopen ("iepuri.out", "w", stdout);
int T, x, y, z, a, b, c, n;
scanf ("%d\n", &T);
while (T--)
{
scanf ("%d %d %d %d %d %d %d\n", &x, &y, &z, &a, &b, &c, &n);
memset (mat, 0, sizeof (mat));
memset (rez, 0, sizeof (rez));
mat[1][2] = mat[2][3] = 1;
mat[3][1] = c; mat[3][2] = b; mat[3][3] = a;
rez[1][1] = rez[2][2] = rez[3][3] = 1;
while (n)
{
if (n & 1)
mul (rez, mat);
mul (mat, mat);
n >>= 1;
}
memset (Bmat, 0, sizeof (Bmat));
Bmat[1][1] = x; Bmat[2][1] = y; Bmat[3][1] = z;
mul (rez, Bmat);
printf ("%d\n", rez[1][1]);
//for (int i = 1; i <= 3; i++) printf ("%d ", Bmat[i][1]);
//printf ("***\n");
}
return 0;
}