#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=15;
const int mod=20140518;
ll n;
int K;
struct Matrix
{
ll matrix[MAXN][MAXN];
}E;
Matrix matrix_mul(Matrix a,Matrix b)
{
Matrix c;
for(int i=1;i<=K;i++)
for(int j=1;j<=K;j++)
{
c.matrix[i][j]=0;
for(int k=1;k<=K;k++)
{
c.matrix[i][j]+=a.matrix[i][k]*b.matrix[k][j];
if(c.matrix[i][j]>=mod)
c.matrix[i][j]%=mod;
}
}
return c;
}
Matrix matrix_pow(Matrix a,ll k)
{
Matrix c=E;
while(k)
{
if(k&1)
c=matrix_mul(c,a);
a=matrix_mul(a,a);
k>>=1;
}
return c;
}
int main()
{
freopen("text.txt","r",stdin);
int T,kase=0;
scanf("%d",&T);
for(int i=1;i<MAXN;i++)
E.matrix[i][i]=1;
while(T--)
{
kase++;
scanf("%lld%lld",&n,&K);
Matrix A;
memset(A.matrix,0,sizeof(A.matrix));
for(int i=1;i<=K;i++)
{
for(int j=1;j<=i;j++)
A.matrix[i][j]=1;
if(i==K)
continue;
A.matrix[i][i+1]=K+1-i;
}
A=matrix_pow(A,n-1);
Matrix ans;
memset(ans.matrix,0,sizeof(ans.matrix));
for(int i=1;i<=K;i++)
ans.matrix[i][1]=K+1;
ans=matrix_mul(A,ans);
printf("Case #%d: %lld\n",kase,ans.matrix[1][1]);
}
return 0;
}