Program cel;
type matrice=array[1..3,1..3] of longint;
var f,g:text;
n,a1,b,c,x1,y,z,t,k,i,j:longint;
a:matrice;
procedure inmultire(var p,a,b:matrice);
var i,j,k:longint;
x:matrice;
Begin
For i:=1 to 3 do
For j:=1 to 3 do
Begin
x[i,j]:=0;
For k:=1 to 3 do x[i,j]:=(x[i,j]+a[i,k]*b[k,j]) mod 666013;
end;
p:=x;
end;
procedure putere(n:longint;var a:matrice);
var i,j:longint;
d:matrice;
Begin
For i:=1 to 3 do
For j:=1 to 3 do
If i=j then d[i,j]:=1
else d[i,j]:=0;
While n<>0 do
Begin
If n mod 2=1 then inmultire(d,d,a);
inmultire(a,a,a);
n:=n div 2;
end;
a:=d;
end;
Begin
Assign(f,'iepuri.in');Reset(f);
Assign(g,'iepuri.out');Rewrite(g);
Readln(f,t);
For i:=1 to t do
Begin
Readln(f,x1,y,z,a1,b,c,n);
a[1,1]:=0;
a[1,2]:=1;
a[1,3]:=0;
a[2,1]:=0;
a[2,2]:=0;
a[2,3]:=1;
a[3,1]:=c mod 666013;
a[3,2]:=b mod 666013;
a[3,3]:=a1 mod 666013;
putere(n,a);
Writeln(g,(x1*a[1,1]+y*a[1,2]+z*a[1,3]) mod 666013);
end;
Close(f);
Close(g);
end.