#include <iostream>
#include <fstream>
#include <vector>
std::ifstream in("iepuri.in");
std::ofstream out("iepuri.out");
int M = 666013;
class Matrix{
private:
std::vector<std::vector<int>> data;
int n;
int m;
public:
Matrix(int n, int m){
this->n = n;
this->m = m;
this->data = std::vector(n, std::vector<int>(m, 0));
}
Matrix(int n, int m, std::vector<std::vector<int>>& x){
this->n = n;
this->m = m;
this->data = std::vector(x.size(), std::vector<int>(x[0].size(), 0));
for(int i = 0; i<x.size(); i++){
for(int j = 0; j<x[0].size(); j++){
this->data[i][j] = x[i][j];
}
}
}
Matrix operator*(const Matrix& obj){
Matrix result(this->n, obj.m);
for(int k = 0; k < this->n; k++){
for(int i = 0; i < obj.m; i++){
for(int j = 0; j < this->m; j++){
result.data[k][i] += this->data[k][j] * obj.data[j][i];
}
}
}
return result;
}
Matrix& operator=(const Matrix& obj){
this->n = obj.n;
this->m = obj.m;
this->data = obj.data;
return *this;
}
Matrix& operator%(int value){
for(int i = 0; i<this->n; i++){
for(int j = 0; j<this->m; j++)
this->data[i][j] %= value;
}
return *this;
}
int operator()(int i, int j){
if(i < 0 || i >=n || j < 0 || j>=m) throw std::out_of_range("Matrix index out of range");
return this->data[i][j];
}
void showData(){
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
std::cout << this->data[i][j] << " ";
}
std::cout << std::endl;
}
}
};
Matrix fpow(Matrix& T, int N){
std::vector<std::vector<int>> temp {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
Matrix result = Matrix(3, 3, temp);
Matrix aux = T;
while(N > 0){
if(N % 2 == 1){
result = (result % M) * (aux % M);
N--;
continue;
}
N/=2;
aux = (aux % M) * (aux % M);
}
return result;
}
int func(int X, int Y, int Z, int A, int B, int C, int N){
std::vector<std::vector<int>> transition {{0, 0, C}, {1, 0 , B}, {0, 1, A}};
Matrix T = Matrix(3, 3, transition);
Matrix result = fpow(T, N-2);
std::vector<std::vector<int>> tmp{{X, Y, Z}};
Matrix fin = Matrix(1, 3, tmp);
fin = (fin * result) % M;
return fin(0, 2);
}
int main(){
int T;
int N, X, Y, Z, A, B, C;
in >> T;
while(T--){
in >> X >> Y >> Z >> A >> B >> C >> N;
out << func(X, Y, Z, A, B, C, N) << std::endl;
}
}