/*#include <iostream>
#include <fstream>
#define MOD 666013
using namespace std;
ifstream f("iepuri.in");
ofstream g("iepuri.out");
int cc[5][5],aa[5][5],bb[5][5],dd[5][5];
int x,y,z,A,B,C,n,t,i,j,k;
void Inm_mat(int a[][5], int b[][5], int c[][5])
{
int i,j,k;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
c[i][j]=0;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
for(k=1;k<=3;k++)
c[i][j]=(c[i][j]+(a[i][k]*b[k][j])%MOD)%MOD;
}
void copycat(int b[][5],int c[][5])
{
int i,j;
for (i=1;i<=3;i++)
for(j=1;j<=3;j++)
b[i][j]=c[i][j];
}
void reinit()
{
for (i=1;i<=3;i++)
for(j=1;j<=3;j++)
aa[i][j]=bb[i][j]=cc[i][j]=dd[i][j]=0;
}
int main()
{
f>>t;
for(;t;t--)
{
f>>x>>y>>z>>A>>B>>C>>n;
aa[1][2]=1;
aa[2][3]=1;
aa[3][1]=C;
aa[3][2]=B;
aa[3][3]=A;
bb[1][1]=1;
bb[2][2]=1;
bb[3][3]=1;
for (i=0;(1<<i)<=n;++i)
{
if (((1<<i) & n) > 0)
{
Inm_mat(bb,aa,cc);
copycat(bb,cc);
}
Inm_mat(aa,aa,dd);
copycat(aa,dd);
}
g<<((1LL*bb[3][1]*x)%MOD + (1LL*bb[3][2]*y)%MOD + (1LL*bb[3][3]*z)%MOD)%MOD<<"\n";
reinit();
}
return 0;
}*/
#include <fstream>
#include <cstring>
#define MOD 666013
using namespace std;
ifstream f("iepuri.in");
ofstream g("iepuri.out");
int t,x,y,z,a,b,c,n;
int m[4][4],t[4][4],sol[4][4];
void mul1()
{
int i,j,k,r[4][4];
memset(r,0,sizeof(r));
for (i=1;i<=3;i++)
for (j=1;j<=3;j++)
{
for (k=1;k<=3;k++)
r[i][j]+=(1LL*sol[i][k]*m[k][j])%MOD;
r[i][j]%=MOD;
}
memcpy(sol,r,sizeof(r));
}
void mul2()
{
int i,j,k,r[4][4];
memset(r,0,sizeof(r));
for (i=1;i<=3;i++)
for (j=1;j<=3;j++)
{
for (k=1;k<=3;k++)
r[i][j]+=(1LL*m[i][k]*m[k][j])%MOD;
r[i][j]%=MOD;
}
memcpy(m,r,sizeof(r));
}
void rid_log(long long p)
{
long long i,j;
for (i = 0;1LL*((1LL*1)<<i)<=p;i++)
{
if (((1<<i)&p)>0)
mul1();
mul2();
}
}
int main()
{
int i, j, temp, t1, t2, t3;
for (f>>tests;tests;tests--)
{
f>>x>>y>>z>>a>>b>>c>>n;
m[1][2]=1;
m[2][3]=1;
m[3][1]=c;
m[3][2]=b;
m[3][3]=a;
memcpy(sol,m,sizeof(m));
expo(n-3);
c=sol[3][1];
b=sol[3][2];
a=sol[3][3];
t1=(1LL*c*x)%MOD;
t2 = (1LL*b*y)%MOD;
t3 = (1LL*a*z)%MOD;
temp=(t1+t2+t3)%MOD;
g<<temp<<'\n';
}
}