#include <fstream>
#include <string.h>
#include <vector>
using namespace std;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
const int MOD = 666013;
typedef vector<vector<int>> mat;
mat p, q, r;
// int p[4][4], q[4][4], r[4][4];
int x, y, z, a, b, c, n, t;
inline void expMat(mat &a, mat &b, mat &c)
{
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
for (int k = 0; k < 3; ++k)
c[i][j] = (c[i][j] + 1LL * a[i][k] * b[k][j]) % MOD;
}
void exp_mat()
{
q = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
p = {{0, 0, c}, {1, 0, b}, {0, 1, a}};
r = mat(3, vector<int>(3, 0));
n -= 2;
while (n)
{
if (n & 1)
{
r = mat(3, vector<int>(3, 0));
expMat(q, p, r);
q = r;
}
r = mat(3, vector<int>(3, 0));
expMat(p, p, r);
p = r;
n >>= 1;
}
int val = (1LL * x * q[0][2] + 1LL * y * q[1][2] + 1LL * z * q[2][2]) % MOD;
fout << val << "\n";
}
int main()
{
fin >> t;
while (t--)
{
fin >> x >> y >> z >> a >> b >> c >> n;
exp_mat();
}
fout.close();
return 0;
}