#include <fstream>
using namespace std;
ifstream cin("iepuri.in");
ofstream cout("iepuri.out");
struct matrix {
int v[3][3];
matrix () {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (i != j) {
v[i][j] = 0;
}
else {
v[i][j] = 1;
}
}
}
}
void operator = (const matrix& alpha) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
v[i][j] = alpha.v[i][j];
}
}
}
void operator += (const matrix& other) {
matrix a;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
a.v[i][j] = v[i][j] + other.v[i][j];
}
}
*this = a;
}
void operator *= (const matrix& other) {
matrix a;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
a.v[i][j] = 0;
for (int k = 0; k < 3; ++k) {
a.v[i][j] += v[i][k] * other.v[k][j];
}
}
}
*this = a;
}
void operator ^= (int exp) {
matrix ans, cur;
cur = *this;
while (exp) {
if (exp&1) {
ans *= cur;
}
cur *= cur;
exp>>=1;
}
*this = ans;
}
};
int main()
{
int t, x, y, z, a, b, c, n;
matrix alpha;
cin >> t;
for (int i = 0; i < t; ++i) {
cin >> x >> y >> z >> a >> b >> c >> n;
alpha.v[0][0] = 0;
alpha.v[0][1] = 1;
alpha.v[0][2] = 0;
alpha.v[1][0] = 0;
alpha.v[1][1] = 0;
alpha.v[1][2] = 1;
alpha.v[2][0] = c;
alpha.v[2][1] = b;
alpha.v[2][2] = a;
alpha ^= n - 2;
cout << alpha.v[2][0] * x + alpha.v[2][1] * y + alpha.v[2][2] * z << "\n";
}
return 0;
}