#include <bits/stdc++.h>
using namespace std;
using LL = long long ;
template < typename T> using V = vector< T> ;
template < typename T, typename S> using P = pair< T, S> ;
template < class T> using min_heap = priority_queue< T, std:: vector < T> , std:: greater < T>> ;
using LD = long double ;
#define ALL(v) v.begin(), v.end()
#define endl '\n'
#define SYNC ios_base::sync_with_stdio(false); cin.tie(NULL); cerr.tie(NULL);
#define REP(i, n) for(int i = 0; i < (int)n ;i++)
#define REPN(i, n) for(int i = 1; i <= (int)n ; i++)
#define her cerr << "here\n"
#define pp push_back
#define fi first
#define se second
#define un(x) x.erase(unique(ALL(x)), x.end())
template < typename T, typename S> void check( T & a, S & b) { if ( a >= b) { a % = b; } }
template < typename T> T gcd( T u, T v) { if ( u == v) return u; if ( u == 0 ) return v; if ( v == 0 ) return u; if ( ~u & 1 ) { if ( v & 1 ) return gcd( u >> 1 , v) ; else return gcd( u >> 1 , v >> 1 ) << 1 ; } if ( ~v & 1 ) return gcd( u, v >> 1 ) ; if ( u > v) return gcd( ( u - v) >> 1 , v) ; return gcd( ( v - u) >> 1 , u) ; }
const int N = ( int ) 1e5 + 10 ;
#define int long long
int solve( auto & a, auto & b, int k) {
map< P< int , P< int ,int >> , int > vis;
int n = ( int ) a.size ( ) ;
reverse( ALL( a) ) ;
reverse( ALL( b) ) ;
priority_queue< P< int ,P< int ,int >> > q;
q.push ( { a[ 0 ] * b[ 0 ] , { 0 , 0 } } ) ;
q.push ( { a[ n - 1 ] * b[ n - 1 ] , { n - 1 , n - 1 } } ) ;
int last = 0 ;
REPN( i, k) {
auto u = q.top ( ) ;
q.pop ( ) ;
if ( vis[ u] ) {
i-- ;
continue ;
}
vis[ u] = true ;
last = u.fi ;
int y = u.se .fi , z = u.se .se ;
if ( y + 1 < n) {
q.push ( { a[ y + 1 ] * b[ z] , { y + 1 , z} } ) ;
}
if ( z + 1 < n) {
q.push ( { a[ y] * 1LL * b[ z + 1 ] , { y, z + 1 } } ) ;
}
if ( y - 1 >= 0 ) {
q.push ( { a[ y - 1 ] * b[ z] , { y - 1 , z} } ) ;
}
if ( z - 1 >= 0 ) {
q.push ( { a[ y] * b[ z - 1 ] , { y, z - 1 } } ) ;
}
}
return last;
}
int a[ N] , b[ N] ;
int32_t main( ) {
SYNC;
cout << fixed << setprecision( 10 ) ;
int T; int caseno = 1 ;
cin >> T;
while ( T-- ) {
cout << "Case #" << caseno++ << ": " ;
//m.clear();
int n, k, l1, l2, c, d, e1, e2, f;
cin >> n >> k >> l1 >> l2 >> c >> d >> e1 >> e2 >> f;
V< int > x( n) , y( n) , r( n, 0 ) , s( n, 0 ) ;
x[ 0 ] = a[ 0 ] = l1; y[ 0 ] = b[ 0 ] = l2;
REPN( i, n - 1 ) {
x[ i] = ( x[ i - 1 ] * c + d * y[ i - 1 ] + e1) % f;
y[ i] = ( y[ i - 1 ] * c + d * x[ i - 1 ] + e2) % f;
r[ i] = ( r[ i - 1 ] * c + d * s[ i - 1 ] + e1) % 2 ;
s[ i] = ( r[ i - 1 ] * d + c * s[ i - 1 ] + e2) % 2 ;
a[ i] = ( r[ i] & 1 ? - x[ i] : x[ i] ) ;
b[ i] = ( s[ i] & 1 ? - y[ i] : y[ i] ) ;
}
V< int > f1, f2;
REP( i, n) {
int sum = 0 ;
for ( int j = i; j < n; j++ ) {
sum + = a[ j] ;
f1.pp ( sum) ;
}
}
REP( i, n) {
int sum = 0 ;
for ( int j = i; j < n; j++ ) {
sum + = b[ j] ;
f2.pp ( sum) ;
}
}
sort( ALL( f1) ) ; sort( ALL( f2) ) ;
cout << solve( f1, f2, k) << endl;
cerr << "Caseno: " << caseno - 1 << " done\n " ;
}
cerr << "Time elapsed :" << clock ( ) * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n ' ;
return 0 ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdXNpbmcgTEwgPSBsb25nIGxvbmc7CnRlbXBsYXRlPHR5cGVuYW1lIFQ+IHVzaW5nIFYgPS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