https://vjudge.net/contest/278754#problem/B
#include<iostream>
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if (b==0)
{
x=1;
y=0;
return a;
}
ll ans=exgcd(b,a%b,y,x);
y=y-(a/b)*x;
return ans;
}
ll r[10],d,i,j,t,k,l,g,x,y,ans;ll f[4]= {23,28,33},f1[4]= {924,759,644};
int main()
{
scanf("%lld",&t);
while (t--)
{
l=0;
while (scanf("%lld%lld%lld%lld",&r[0],&r[1],&r[2],&d)){ ans=0;
if (r[0]==-1&&r[1]==-1&&r[2]==-1) break;
for (i=0; i<3; i++)
{
g=exgcd(f1[i],f[i],x,y);
ans=(ans+x*r[i]*f1[i])%21252;
}
ans=(ans%21252+21252)%21252;
while (ans<=d) ans+=21252;
printf("Case %lld: the next triple peak occurs in %lld days.\n",++l,ans-d);
}
}
}