#include <bits/stdc++.h>
#define MOD 666013
using namespace std;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
int mod_multiply(int a, int b, int mod) {
return 1ll * (1ll * (1ll * (a % mod) * 1ll * (b % mod))) % (1ll * mod);
}
void mult_mat(int A[3][3], int B[3][3], int C[3][3], int mod) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
C[i][j] = 0;
for (int k = 0; k < 3; ++k) {
C[i][j] += mod_multiply(A[i][k], B[k][j], mod);
}
C[i][j] %= mod;
}
}
}
void copy_mat(int dest[3][3], int src[3][3]) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
dest[i][j] = src[i][j];
}
}
}
void fast_pow(int base[3][3], int exponent, int mod, int result[3][3]) {
if (exponent <= 0) {
result[0][0] = result[1][1] = result[2][2] = 1;
return;
}
fast_pow(base, exponent / 2, mod, result);
int res_aux[3][3] = {0};
if (exponent % 2 == 0) {
mult_mat(result, result, res_aux, mod);
} else {
mult_mat(result, result, res_aux, mod);
copy_mat(result, res_aux);
mult_mat(base, result, res_aux, mod);
}
copy_mat(result, res_aux);
}
void solve() {
int x, y, z, a, b, c, n;
fin >> x >> y >> z >> a >> b >> c >> n;
int mat[3][3] = {{a, b, c}, {1, 0, 0}, {0, 1, 0}};
int res[3][3] = {0};
fast_pow(mat, n - 2, MOD, res);
fout << (mod_multiply(res[0][0], z, MOD) +
mod_multiply(res[0][1], y, MOD) +
mod_multiply(res[0][2], x, MOD)) % MOD << "\n";
}
int main(void) {
int t;
fin >> t;
for (int i = 0; i < t; ++i) {
solve();
}
return 0;
}