/***********Template Starts Here***********/
#include <bits/stdc++.h>
#define pb push_back
#define nl puts ("")
#define sp printf ( " " )
#define phl printf ( "hello\n" )
#define ff first
#define ss second
#define POPCOUNT __builtin_popcountll
#define RIGHTMOST __builtin_ctzll
#define LEFTMOST(x) (63-__builtin_clzll((x)))
#define MP make_pair
#define FOR(i,x,y) for(int i = (x) ; i <= (y) ; ++i)
#define ROF(i,x,y) for(int i = (y) ; i >= (x) ; --i)
#define CLR(x,y) memset(x,y,sizeof(x))
#define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end())
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define NUMDIGIT(x,y) (((int)(log10((x))/log10((y))))+1)
#define SQ(x) ((x)*(x))
#define ABS(x) ((x)<0?-(x):(x))
#define FABS(x) ((x)+eps<0?-(x):(x))
#define ALL(x) (x).begin(),(x).end()
#define LCM(x,y) (((x)/gcd((x),(y)))*(y))
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long vlong;
typedef unsigned long long uvlong;
typedef pair < int, int > pii;
typedef pair < vlong, vlong > pll;
typedef vector<pii> vii;
typedef vector<int> vi;
/***********Template Ends Here***********/
vlong b, m;
///find (a*b)%m without overflowing
vlong mulMod ( vlong a, vlong b ) {
vlong res = 0;
vlong x = a;
while ( b ) {
if ( b & 1 ) {
res = res + x;
if ( res >= m ) res -= m;
}
x = ( x + x );
if ( x >= m ) x -= m;
b >>= 1;
}
return res;
}
vlong bigmodExtended ( vlong a, vlong p ) {
vlong res = 1 % m, x = a % m;
while ( p ) {
if ( p & 1 ) res = mulMod( res, x );
x = mulMod( x , x ); p >>= 1;
}
return res;
}
vlong divMod ( vlong n ) {
if ( n == 1 ) {
return 1 % m;
}
if ( n & 1 ) {
vlong res = bigmodExtended( b, n-1 ) + divMod( n - 1 );
if ( res >= m ) res -= m;
return res;
}
else {
vlong res = divMod(n/2);
vlong temp = 1 + bigmodExtended(b,n/2);
if ( temp >= m ) temp -= m;
if ( temp == 0 || res == 0 ) return 0;
return mulMod ( res, temp );
}
}
int main () {
int kase, cnt = 0;
scanf ( "%d", &kase );
while ( kase-- ) {
vlong n, d;
scanf ( "%lld %lld %lld %lld", &n, &b, &d, &m );
vlong res = divMod ( n );
res = ( res * d );
res %= m;
printf ( "Case %d: %lld\n", ++cnt, res );
}
return 0;
}
LyoqKioqKioqKioqVGVtcGxhdGUgU3RhcnRzIEhlcmUqKioqKioqKioqKi8KI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG5sIHB1dHMgKCIiKQojZGVmaW5lIHNwIHByaW50ZiAoICIgIiApCiNkZWZpbmUgcGhsIHByaW50ZiAoICJoZWxsb1xuIiApCiNkZWZpbmUgZmYgZmlyc3QKI2RlZmluZSBzcyBzZWNvbmQKI2RlZmluZSBQT1BDT1VOVCBfX2J1aWx0aW5fcG9wY291bnRsbAojZGVmaW5lIFJJR0hUTU9TVCBfX2J1aWx0aW5fY3R6bGwKI2RlZmluZSBMRUZUTU9TVCh4KSAoNjMtX19idWlsdGluX2NsemxsKCh4KSkpCiNkZWZpbmUgTVAgbWFrZV9wYWlyCiNkZWZpbmUgRk9SKGkseCx5KSBmb3IoaW50IGkgPSAoeCkgOyBpIDw9ICh5KSA7ICsraSkKI2RlZmluZSBST0YoaSx4LHkpIGZvcihpbnQgaSA9ICh5KSA7IGkgPj0gKHgpIDsgLS1pKQojZGVmaW5lIENMUih4LHkpIG1lbXNldCh4LHksc2l6ZW9mKHgpKQojZGVmaW5lIFVOSVFVRShWKSAoVikuZXJhc2UodW5pcXVlKChWKS5iZWdpbigpLChWKS5lbmQoKSksKFYpLmVuZCgpKQojZGVmaW5lIE1JTihhLGIpICgoYSk8KGIpPyhhKTooYikpCiNkZWZpbmUgTUFYKGEsYikgKChhKT4oYik/KGEpOihiKSkKI2RlZmluZSBOVU1ESUdJVCh4LHkpICgoKGludCkobG9nMTAoKHgpKS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HZsb25nIGEsIHZsb25nIHAgKSB7CiAgICB2bG9uZyByZXMgPSAxICUgbSwgeCA9IGEgJSBtOwogICAgd2hpbGUgKCBwICkgewogICAgICAgIGlmICggcCAmIDEgKSByZXMgPSBtdWxNb2QoIHJlcywgeCApOwogICAgICAgIHggPSBtdWxNb2QoIHggLCB4ICk7IHAgPj49IDE7CiAgICB9CiAgICByZXR1cm4gcmVzOwp9Cgp2bG9uZyBkaXZNb2QgKCB2bG9uZyBuICkgewogICAgaWYgKCBuID09IDEgKSB7CiAgICAgICAgcmV0dXJuIDEgJSBtOwogICAgfQoKICAgIGlmICggbiAmIDEgKSB7CiAgICAgICAgdmxvbmcgcmVzID0gYmlnbW9kRXh0ZW5kZWQoIGIsIG4tMSApICsgZGl2TW9kKCBuIC0gMSApOwogICAgICAgIGlmICggcmVzID49IG0gKSByZXMgLT0gbTsKCiAgICAgICAgcmV0dXJuIHJlczsKICAgIH0KICAgIGVsc2UgewogICAgICAgIHZsb25nIHJlcyA9IGRpdk1vZChuLzIpOwogICAgICAgIHZsb25nIHRlbXAgPSAxICsgYmlnbW9kRXh0ZW5kZWQoYixuLzIpOwogICAgICAgIGlmICggdGVtcCA+PSBtICkgdGVtcCAtPSBtOwoKICAgICAgICBpZiAoIHRlbXAgPT0gMCB8fCByZXMgPT0gMCApIHJldHVybiAwOwogICAgICAgIHJldHVybiBtdWxNb2QgKCByZXMsIHRlbXAgKTsKICAgIH0KfQoKaW50IG1haW4gKCkgewogICAgaW50IGthc2UsIGNudCA9IDA7CiAgICBzY2FuZiAoICIlZCIsICZrYXNlICk7CgogICAgd2hpbGUgKCBrYXNlLS0gKSB7CiAgICAgICAgdmxvbmcgbiwgZDsKICAgICAgICBzY2FuZiAoICIlbGxkICVsbGQgJWxsZCAlbGxkIiwgJm4sICZiLCAmZCwgJm0gKTsKCiAgICAgICAgdmxvbmcgcmVzID0gZGl2TW9kICggbiApOwoKICAgICAgICByZXMgPSAoIHJlcyAqIGQgKTsKICAgICAgICByZXMgJT0gbTsKCiAgICAgICAgcHJpbnRmICggIkNhc2UgJWQ6ICVsbGRcbiIsICsrY250LCByZXMgKTsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=