// Vivek
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
using namespace std;
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
template<class T> void smax(T& a, T val) {if (a < val) a = val;}
const int N = 5*1e5 + 10;
ll n, m, a, o, d, t, b, c, l;
int main(){_
#define int ll
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin >> t;
for (int test = 0; test < t; ++test)
{
cin >> n >> o >> d;
std::vector<ll> x(n+1, 0);
cin >> x[1] >> x[2] >> a >> b >> c >> m >> l;
cout << "Case #" << (test+1) << ": ";
std::vector<ll> s(n+1);
s[1] = x[1] + l;
s[2] = x[2] + l;
for (int i = 3; i <= n; ++i)
{
x[i] = (a*x[i-1] + b*x[i-2] + c) % m;
s[i] = x[i] + l;
}
ll i = 1, ans = -2e18, sum = 0, j = 1, co = 0;
while(i <= n){
while(j <= n){
co += ((s[j] % 2) != 0);
if(co > o){
--co;
break;
}
if(sum + s[j] > d){
co -= ((s[j] % 2) != 0);
break;
}
sum += s[j];
if(sum <= d)smax(ans, sum);
++j;
}
if(i == j){
sum = 0;
++i, ++j;
co = 0;
continue;
}
if(sum <= d)smax(ans, sum);
if((s[i] % 2) != 0 and i != j){
co--;
}
sum -= s[i];
++i;
}
if(ans == -2e18){
cout << "IMPOSSIBLE" << endl;
continue;
}
cout << ans << endl;
}
return 0;
}
Ly8gVml2ZWsKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIF8gaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7Y2luLnRpZShOVUxMKTtjb3V0LnRpZShOVUxMKTsKI2RlZmluZSBsbCBsb25nIGxvbmcgCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojaWZkZWYgT05MSU5FX0pVREdFCiNkZWZpbmUgZW5kbCAnXG4nCiNlbmRpZgp0ZW1wbGF0ZTxjbG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