#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; 
}