#include <iostream>
#include <fstream>
using namespace std;
const int mod = 666013;
int sum(int a, int b)
{
return (a + b) % mod;
}
int prod(int a, int b)
{
return (1LL * a * b) % mod;
}
void multiply(int a[3][3], int b[3][3])
{
int c[3][3];
for (int i = 0; i <= 2; i++)
for (int j = 0; j <= 2; j++)
{
c[i][j] = 0;
for (int k = 0; k <= 2; k++)
c[i][j] = sum(c[i][j], prod(a[i][k], b[k][j]));
}
for (int i = 0; i <= 2; i++)
for (int j = 0; j <= 2; j++)
a[i][j] = c[i][j];
}
void putlog(int m[3][3], int n, int a,int b,int c)
{
if (n < 2)
return;
int mu[3][3] = { {0,1,0},{0,0,1},{c,b,a} };
putlog(m, n / 2, a, b, c);
multiply(m, m);
if (n % 2 == 1)
multiply(m, mu);
}
int solve(int n, int a,int b,int c,int x,int y,int z)
{
int m[3][3] = { {0,1,0},{0,0,1},{c,b,a} };
putlog(m, n, a, b, c);
return sum(prod(x, m[0][0]), sum(prod(y, m[0][1]), prod(z, m[0][2])));
}
int main()
{
ifstream cin("iepuri.in");
ofstream cout("iepuri.out");
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
int n, x, y, z, a, b, c;
cin >> x >> y >> z >> a >> b >> c >> n;
cout << solve(n, a, b, c, x, y, z) << "\n";
}
return 0;
}