#include <stdio.h>
#include <string.h>
#define MOD 666013
typedef int mat3[3][3];
void multiply(mat3 &A, mat3 &B) {
mat3 tmp;
memset( tmp, 0, sizeof(tmp) );
for ( int i = 0; i < 3; i++ ) {
for ( int j = 0; j < 3; j++ ) {
for ( int k = 0; k < 3; k++ ) {
tmp[i][j] = (tmp[i][j] + A[i][k] * B[k][j]) % MOD;
}
}
}
memcpy( A, tmp, sizeof(tmp) );
}
int main(void) {
FILE *fin, *fout;
int T, n, x, y, z, a, b, c;
fin = fopen( "iepuri.in", "r" );
fout = fopen("iepuri.out", "w");
fscanf( fin, "%d", &T );
while ( T-- ) {
fscanf( fin, "%d%d%d%d%d%d%d", &x, &y, &z, &a, &b, &c, &n );
mat3 curr = { { x, y, z } };
mat3 mult = { { 0, 0, c }, { 1, 0, b }, { 0, 1, a } };
n -= 2;
while ( n ) {
if ( n & 1 ) {
multiply( curr, mult );
}
multiply( mult, mult );
n >>= 1;
}
fprintf( fout, "%d\n", curr[0][2] );
}
fclose(fin);
fclose(fout);
return 0;
}