/// *** --- ||| In the name of ALLAH ||| --- *** ///
#include<bits/stdc++.h>
using namespace std;
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define endl '\n'
#define ll long long
#define int long long
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)x.size()
#define mem(a,b) memset(a, b, sizeof(a) )
#define sq(a) ((a) * (a))
const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const int MX = 2e5+123;
const ll infLL = 9000000000000000000;
const int MOD = 1e9+7;
int a, b, m;
pair<int, int> fib (int n) {
if (n == a)
return {a, b};
auto p = fib(n >> 1);
int c = p.first * (2 * p.second - p.first);
int d = p.first * p.first + p.second * p.second;
if (n & 1)
return {d, c + d};
else
return {c, d};
}
void solve (int testCase)
{
cout << "Case " << testCase << ": ";
int n;
cin >> a >> b >> n >> m;
auto ans = fib (n);
string mod = "1";
for (int i = 0; i < m; i++) mod += '0';
m = stoi (mod);
cout << ans.first % m << endl;
}
int32_t main()
{
fastio();
// srand(time(NULL));
int testcases = 1;
cin >> testcases;
for (int tt = 1; tt <= testcases; tt++)
{
solve (tt);
}
return 0;
}
Ly8vICAgKioqICAgLS0tICAgfHx8CQkgSW4gdGhlIG5hbWUgb2YgQUxMQUgJCXx8fCAgIC0tLSAgICoqKiAgIC8vLwoKI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgZmFzdGlvKCkgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTtjaW4udGllKDApO2NvdXQudGllKDApOwojZGVmaW5lIGZyYWN0aW9uKCkgY291dC51bnNldGYoaW9zOjpmbG9hdGZpZWxkKTsgY291dC5wcmVjaXNpb24oMTApOyBjb3V0LnNldGY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