#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[15];
ll dp[15][200005];
ll po[15], maxx;
ll F(ll x)
{
ll pos = 0, ans = 0, num, pi = 1;
while(x)
{
num = x % 10;
x /= 10;
ans += num * pi;
pi *= 2;
}
return ans;
}
ll dfs(ll pos, ll sta, ll lim)
{
if(sta < 0)return 0;
if(pos == -1)return 1;
if(lim == 0 && dp[pos][sta] != -1)return dp[pos][sta];
ll len = lim ? a[pos] : 9;
ll ans = 0;
for(ll i = 0; i <= len; i++)
{
ll w = po[pos] * i;
if(sta - w < 0)continue;
else
{
ans += dfs(pos - 1, sta - w, lim && i == a[pos]);
}
}
if(lim == 0)dp[pos][sta] = ans;
return ans;
}
ll slove(ll x)
{
ll pos = 0;
while(x)
{
a[pos++] = x % 10;
x /= 10;
}
ll ans = dfs(pos - 1, maxx, 1);
return ans;
}
int main()
{
ll t, k, i, j, m, n;
k = 1;
for(i = 0; i <= 15; i++)po[i] = k, k *= 2;
scanf("%lld", &t);
for(ll q = 0; q < 15; q++)
{
for(ll w = 0; w < 200005; w++)dp[q][w] = -1;
}
for(i = 1; i <= t; i++)
{
scanf("%lld %lld", &n, &m);
maxx = F(n);
//cout << maxx << endl;
ll ans = slove(m);
if(0 <= maxx)ans++;
printf("Case #%lld: %lld\n",i, ans - 1);
}
return 0;
}