#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn=100+5;
const int mod=1000007;
const int N=3;
struct Mat
{
LL mat[N][N];
};
//矩阵相乘
Mat mul(Mat a,Mat b)
{
Mat ans;
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
ans.mat[i][j]=0;
for(int k=0; k<N; k++)
{
ans.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
ans.mat[i][j]%=mod;
}
}
}
return ans;
}
//矩阵的n次方
Mat quickPow(Mat a,int n)
{
Mat ans=//单位矩阵
{
1,0,0,
0,1,0,
0,0,1
};
while(n)
{
if(n&1)
ans=mul(ans,a);
n>>=1;
a=mul(a,a);
}
return ans;
}
int main()
{
int t,a,b,c,n;
scanf("%d",&t);
Mat tem;
Mat e=
{
0,0,0,
1,0,0,
0,0,1
};
int f[2];
tem.mat[2][0]=1;
while(t--){
scanf("%d%d%d%d%d%d",&f[0],&f[1],&a,&b,&c,&n);
if(n==1||n==2){
printf("%d\n",(f[n-1]+mod)%mod);
}
else{
e.mat[0][0]=b;
e.mat[0][1]=a;
e.mat[0][2]=c;
tem.mat[0][0]=f[1];
tem.mat[1][0]=f[0];
Mat ans = mul(quickPow(e,n-2),tem);
printf("%lld\n",(ans.mat[0][0]+mod)%mod);
}
}
return 0;
}
02-15
600