//http://acm.hdu.edu.cn/showproblem.php?pid=4734
#include <bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(), (a).end()
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e3 + 10;
int num[20], k;
ll f[20][4600];//最多还能加多少权重
ll dfs(int pos, int mx, bool lead, bool limit)
{
if(pos == 0) return 1;
if(!lead && !limit && f[pos][mx] != -1) return f[pos][mx];
ll ans = 0;
int up = limit ? num[pos] : 9;
for(int i = 0; i <= up; i++)
{
if(mx >= (1 << (pos - 1)) * i)
ans += dfs(pos - 1, mx - (1 << (pos - 1)) * i, lead && i == 0, limit && i == up);
else
break;
}
if(!lead && !limit) f[pos][mx] = ans;
return ans;
}
ll run(ll x)
{
int len = 0;
while(x)
{
num[++len] = x % 10;
x /= 10;
}
return dfs(len, k, true, true);
}
int tot;
void solve()
{
ll A, B;
cin >> A >> B;
k = 0;
for(int i = 0; A; i++, A /= 10) k += (1 << i) * (A % 10);
cout << "Case #" << ++tot << ": " << run(B) << '\n';
}
signed main()
{
IOS;
int t = 1;
memset(f, -1, sizeof f);
cin >> t;
while (t--)
solve();
return 0;
}