#include<fstream>
using namespace std;
ifstream fin( "iepuri.in" ); ofstream fout( "iepuri.out" );
typedef long long i64;
class Matrice{
public:
static const int nmax = 3;
i64 a[ nmax ][ nmax ];
Matrice() {}
Matrice( i64 x, i64 y, i64 z, i64 aa, i64 b, i64 c, i64 e, i64 f, i64 g ) {
a[ 0 ][ 0 ] = x; a[ 0 ][ 1 ] = y; a[ 0 ][ 2 ] = z;
a[ 1 ][ 0 ] = aa; a[ 1 ][ 1 ] = b; a[ 1 ][ 2 ] = c;
a[ 2 ][ 0 ] = e; a[ 2 ][ 1 ] = f; a[ 2 ][ 2 ] = g;
}
inline Matrice operator * ( const Matrice &x ) const {
Matrice ans;
for( int i = 0; i < nmax; ++ i ) {
for( int j = 0; j < nmax; ++ j ) {
ans.a[ i ][ j ] = 0;
for( int k = 0; k < nmax; ++ k ) {
ans.a[ i ][ j ] += a[ i ][ k ] * x.a[ k ][ j ];
ans.a[ i ][ j ] %= mod;
}
}
}
return ans;
}
inline Matrice operator ^ ( int e ) {
Matrice aux = *this, sol = Matrice( 1, 0, 0, 0, 1, 0, 0, 0, 1 );
while ( e > 0 ) {
if ( e & 1 ) {
sol = sol * aux;
}
aux = aux * aux;
e >>= 1;
}
return sol;
}
private:
static const int mod = 666013;
} f, i;
int main() {
int t;
fin >> t;
while ( t -- ) {
i64 x, y, z, a, b, c, n;
fin >> x >> y >> z >> a >> b >> c >> n;
i = Matrice( x, y, z, 0, 0, 0, 0, 0, 0 );
f = Matrice( 0, 0, c, 1, 0, b, 0, 1, a );
i = i * (f ^ (n - 2));
fout << i.a[ 0 ][ 2 ] << "\n";
}
fin.close();
fout.close();
return 0;
}