#include <iostream>
#include <fstream>
#define modulo 666013
using namespace std;
typedef unsigned long long int mare;
struct matrix3x3 {
mare x[3][3];
matrix3x3(mare x00, mare x01, mare x02, mare x10, mare x11, mare x12, mare x20,mare x21, mare x22) {
x[0][0] = x00 % modulo;
x[0][1] = x01 % modulo;
x[0][2] = x02 % modulo;
x[1][0] = x10 % modulo;
x[1][1] = x11 % modulo;
x[1][2] = x12 % modulo;
x[2][0] = x20 % modulo;
x[2][1] = x21 % modulo;
x[2][2] = x22 % modulo;
}
};
inline matrix3x3 multiply (matrix3x3 a, matrix3x3 b) {
return matrix3x3(
a.x[0][0]*b.x[0][0] + a.x[0][1]*b.x[1][0] + a.x[0][2]*b.x[2][0],
a.x[0][0]*b.x[0][1] + a.x[0][1]*b.x[1][1] + a.x[0][2]*b.x[2][1],
a.x[0][0]*b.x[0][2] + a.x[0][1]*b.x[1][2] + a.x[0][2]*b.x[2][2],
a.x[1][0]*b.x[0][0] + a.x[1][1]*b.x[1][0] + a.x[1][2]*b.x[2][0],
a.x[1][0]*b.x[0][1] + a.x[1][1]*b.x[1][1] + a.x[1][2]*b.x[2][1],
a.x[1][0]*b.x[0][2] + a.x[1][1]*b.x[1][2] + a.x[1][2]*b.x[2][2],
a.x[2][0]*b.x[0][0] + a.x[2][1]*b.x[1][0] + a.x[2][2]*b.x[2][0],
a.x[2][0]*b.x[0][1] + a.x[2][1]*b.x[1][1] + a.x[2][2]*b.x[2][1],
a.x[2][0]*b.x[0][2] + a.x[2][1]*b.x[1][2] + a.x[2][2]*b.x[2][2]
);
}
matrix3x3 scale(matrix3x3 X, mare n) {
if (n == 0)
return matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1);
matrix3x3 X1 = scale(X, n / 2);
if (n & 1)
return multiply(multiply(X1, X1), X);
else
return multiply(X1, X1);
}
int main() {
int T, X, Y, Z, A, B, C;
mare N;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
fin >> T;
while (T) {
T--;
fin >> A >> B >> C >> X >> Y >> Z >> N;
matrix3x3 A1(A, 1, 0, B, 0, 1, C, 0, 0);
matrix3x3 A = scale(A1, N - 2);
// for (int i = 0; i <= 2; i++) {
// for (int j = 0; j <= 2; j++)
// cout << A.x[i][j] << '\t';
// cout << '\n';
// }
fout << A.x[0][0] * Z + A.x[1][0] * Y + A.x[2][0] * X << '\n';
}
fin.close();
fout.close();
return 0;
}