#include <fstream>
#include <iostream>
#include <array>
using namespace std;
constexpr int mod = 666013;
template <unsigned int a, unsigned int b>
using mat = array<array<uint64_t, b>, a>;
template <unsigned int a, unsigned int b, unsigned int c>
mat<a, c> operator*(const mat<a, b>& x, const mat<b, c>& y){
mat<a, c> rez;
for(int i = 0; i < a; ++i){
for(int j = 0; j < c; ++j){
rez[i][j] = 0;
for(int k = 0; k < b; ++k){
rez[i][j] += (x[i][k] * y[k][j])%mod;
rez[i][j] %= mod; } } }
return rez; }
template <unsigned int a, unsigned int b>
ostream& operator<<(ostream& lhs, const mat<a, b>& x){
for(int i = 0; i < a; ++i){
for(int j = 0; j < b; ++j){
lhs << x[i][j] << ' '; }
lhs << '\n'; }
return lhs; }
template <typename T>
T exp(const T& baza, const int e, const T rez){
return e ? exp(baza*baza, e/2, (e&1) ? baza*rez : rez) : rez; }
uint64_t rezolva(const uint64_t x, const uint64_t y, const uint64_t z,
const uint64_t a, const uint64_t b, const uint64_t c,
const uint64_t n){
const mat<3u, 3u> mult = {a, 1, 0, b, 0, 1, c, 0, 0};
const mat<1u, 3u> baza = {z, y, x};
const auto tmp = exp(mult, n, mat<3u, 3u>{ {1, 0, 0, 0, 1, 0, 0, 0, 1} });
return (baza * tmp)[0][2]; }
int main(){
ifstream f("iepuri.in");
ofstream g("iepuri.out");
uint64_t t = 0;
f >> t;
for(uint64_t i = 0, x, y, z, a, b, c, n; i < t; ++i){
f >> x >> y >> z >> a >> b >> c >> n;
g << rezolva(x, y, z, a, b, c, n) << '\n'; }
return 0; }