#include <fstream>
#include <vector>
using namespace std;
const int MOD = 666013;
const int NR = 3;
vector <vector <int>> produs(vector <vector <int>> &p, vector <vector <int>> &a) {
// p = p * a
vector <vector <int>> aux(NR, vector <int>(NR, 0));
for (int i = 0; i < NR; i++) {
for (int j = 0; j < NR; j++) {
aux[i][j] = 0;
for (int k = 0; k < NR; k++) {
aux[i][j] = (aux[i][j] + (long long)p[i][k] * a[k][j]) % MOD;
}
}
}
return aux;
}
int main() {
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
vector <vector <int>> f, p;
int t;
fin >> t;
for (int i = 0; i < t; i++) {
int x, y, z, a, b, c, n;
fin >> x >> y >> z >> a >> b >> c >> n;
f = {{a, b, c}, {1, 0, 0}, {0, 1, 0}};
p = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
n -= 2;
while (n != 0) {
int cif_b = (n & 1);
if (cif_b != 0) {
p = produs(p, f);
}
f = produs(f, f);
n >>= 1;
}
fout << ((long long)p[0][0] * z + (long long)p[0][1] * y + (long long)p[0][2] * x) % MOD << "\n";
}
fin.close();
fout.close();
return 0;
}