#include<fstream>
#include<iostream>
#include<climits>
#include<algorithm>
#include<cstring>
#include<cmath>
#include <vector>
#include <queue>
#include <iomanip>
#include <bitset>
#include <deque>
#define MOD 666013
using namespace std;
ifstream f("iepuri.in");
ofstream g("iepuri.out");
//ifstream f("in.in");
//ofstream g("out.out");
long long t,a,b,c,x,y,z,n;
void produs(long long prod[3][3],long long a[3][3],long long b[3][3]){
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
prod[i][j] = 0;
for(int k=0;k<=2;k++){
prod[i][j] = (prod[i][j] + a[i][k] * b[k][j])%MOD;
}
}
}
}
void egal(long long x[3][3],long long a[3][3]){
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
x[i][j] = a[i][j];
}
}
}
int main(){
f>>t;
while(t--){
f>>x>>y>>z>>a>>b>>c>>n;
if(n==0){
g<<x;
continue;
}
if(n==1){
g<<y;
continue;
}
if(n==2){
g<<z;
continue;
}
n-=2;
long long tmp[3][3] = {
{0,0,0},
{0,0,0},
{0,0,0}
};
long long cnt[3][3] = {
{a,b,c},
{1,0,0},
{0,1,0}
};
long long p[3][3] = {
{1,0,0},
{0,1,0},
{0,0,1}
};
while(n!=0){
if(n%2==1){
produs(tmp,p,cnt);
egal(p,tmp);
}
produs(tmp,cnt,cnt);
egal(cnt,tmp);
n/=2;
}
long long start[3][3] = {
{z,0,0},
{y,0,0},
{x,0,0}
};
produs(tmp,p,start);
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
cout<<tmp[i][j]<<" ";
}
cout<<"\n";
}cout<<"\n";
g<<tmp[0][0]<<'\n';
}
f.close();
g.close();
return 0;
}