#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#ifdef LOCAL
ifstream fin("input.txt");
#define fout cout
#else
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
#define endl '\n'
#endif
const int MOD = 666013;
typedef vector<vector<int>> mat;
mat operator*(const mat &a, const mat &b) {
mat ret = b;
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b[0].size(); j++) {
ret[i][j] = 0;
for (int k = 0; k < a[0].size(); k++) {
ret[i][j] += (1ll * a[i][k] * b[k][j]) % MOD;
}
ret[i][j] %= MOD;
}
}
return ret;
}
int t, x, y, z, a, b, c, n;
mat pow(const mat &a, int n) {
if (n == 1) return a;
if (n % 2 == 0) {
const mat p = pow(a, n / 2);
return p * p;
}
return a * pow(a, n - 1);
}
int solve() {
mat A = {
{0, 1, 0},
{0, 0, 1},
{c, b, a}
};
mat B = {
{x},
{y},
{z}
};
mat ans = pow(A, n) * B;
return ans[0][0];
}
int main() {
fin >> t;
while (t--) {
fin >> x >> y >> z >> a >> b >> c >> n;
fout << solve() << endl;
}
return 0;
}