//打表#include<iostream>#include<cstdio>#include<algorithm>
using namespace std;constint M =1e6+5;typedeflonglong ll;
ll res[11][M];
ll f(int n,int b){
ll ans =0;while(n){
ans += n % b;
n /= b;}return ans;}intmain(){int t;scanf("%d",&t);for(int i =2; i <=10;++i)
res[i][1]=1;for(int i =2; i <=10;++i)for(int j =2; j <=1000000;++j)
res[i][j]= res[i][j -1]+f(j, i);for(int k =1; k <= t;++k){int x, y;scanf("%d %d",&x,&y);printf("Case #%d: %lld\n", k, res[y][x]);}return0;}