#include <iostream>
#include <fstream>
#include <cstring>
#define MOD 666013
using namespace std;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
void inmultire(long long a[][3], long long b[][3])
{
long long c[3][3];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
c[i][j] = 0;
for(int k = 0; k < 3; k++)
c[i][j] += 1LL*a[i][k]*b[k][j];
}
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
a[i][j] = c[i][j]%MOD;
}
void putere(int k, int x, int y, int z, int a, int b, int c)
{
long long A[3][3] ={{a,1,0}, {b,0,1}, {c,0,0}};
long long B[3][3]= {{z,y,x}, {0,0,0}, {0,0,0}};
while(k > 0)
{
if(k%2 == 0)
inmultire(A, A), k /= 2;
else
inmultire(B,A), k--;
}
fout << B[0][0] << "\n";
}
int main()
{
int t;
fin >> t;
while(t--)
{
int x, y, z, a, b, c, n;
fin >> x >> y >> z >> a >> b >> c >> n;
putere(n-2, x, y, z, a, b, c);
}
return 0;
}