#include <fstream>
using namespace std;
ifstream in("iepuri.in");
ofstream out("iepuri.out");
const int MODULO = 666013;
struct matrice
{
int a[4][4];
matrice operator * (matrice &b)
{
matrice c;
for (int i = 1;i <= 3;++i)
for (int j = 1;j <= 3;++j)
{
c.a[i][j] = 0;
for (int k = 1;k <= 3;++k)
{
c.a[i][j] += a[i][k] * b.a[k][j];
c.a[i][j] %= MODULO;
}
}
return c;
}
};
matrice putere(matrice a, int b)
{
if (b == 1)
return a;
matrice c = putere (a, b / 2);
if (b % 2 == 0)
return c * c;
else return c * c * a;
}
void test()
{
int z0,z1,z2,A,B,C,n;
in >> z0 >> z1 >> z2 >> A >> B >> C >> n;
matrice M;
M.a[1][1] = 0; M.a[1][2] = 0; M.a[1][3] = C;
M.a[2][1] = 1; M.a[2][2] = 0; M.a[2][3] = B;
M.a[3][1] = 0; M.a[3][2] = 1; M.a[3][3] = A;
M = putere(M, n - 2);
int rasp = 0;
rasp += z0 * M.a[1][3];
rasp %= MODULO;
rasp += z1 * M.a[2][3];
rasp %= MODULO;
rasp += z2 * M.a[3][3];
rasp %= MODULO;
out << rasp << '\n';
}
int main()
{
int nrteste;
in >> nrteste;
for (int i = 1;i <= nrteste;++i)
test();
return 0;
}