#include <bits/stdc++.h>
using namespace std;
int inv(int x)
{
int r=0;
while(x)
{
r*=10;
r+=x%10;
x/=10;
}
return r;
}
int n,dp[1000005];
int call(int x)
{
if(x==n) return 0;
if(x>n) return n;
if(dp[x]!=-1) return dp[x];
int ans=n;
ans=min(ans,1+call(x+1));
if(inv(x)>x&&inv(x)<=n) ans=min(ans,1+call(inv(x)));
return dp[x]=ans;
}
int main()
{
int t;
cin >> t ;
for(int ct = 1 ; ct <= t ; ct++)
{
cin >> n;
for(int i=0;i<=n+5;i++) dp[i]=-1;
cout << "Case #" << ct << ": " << call(0) << "\n" ;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgaW52KGludCB4KQp7CiAgICBpbnQgcj0wOwogICAgd2hpbGUoeCkKICAgIHsKICAgICAgICByKj0xMDsKICAgICAgICByKz14JTEwOwogICAgICAgIHgvPTEwOwogICAgfQogICAgcmV0dXJuIHI7Cn0KaW50IG4sZHBbMTAwMDAwNV07CmludCBjYWxsKGludCB4KQp7CiAgICBpZih4PT1uKSByZXR1cm4gMDsKICAgIGlmKHg+bikgcmV0dXJuIG47CiAgICBpZihkcFt4XSE9LTEpIHJldHVybiBkcFt4XTsKICAgIGludCBhbnM9bjsKICAgIGFucz1taW4oYW5zLDErY2FsbCh4KzEpKTsKICAgIGlmKGludih4KT54JiZpbnYoeCk8PW4pIGFucz1taW4oYW5zLDErY2FsbChpbnYoeCkpKTsKICAgIHJldHVybiBkcFt4XT1hbnM7Cn0KCmludCBtYWluKCkKewogICAgaW50IHQ7CiAgICBjaW4gPj4gdCA7CiAgICBmb3IoaW50IGN0ID0gMSA7IGN0IDw9IHQgOyBjdCsrKQogICAgewogICAgICAgICAgY2luID4+IG47CiAgICAgICAgICBmb3IoaW50IGk9MDtpPD1uKzU7aSsrKSBkcFtpXT0tMTsKICAgICAgICAgIGNvdXQgPDwgIkNhc2UgIyIgPDwgY3QgPDwgIjogIiA8PCBjYWxsKDApIDw8ICJcbiIgOwogICAgfQp9Cg==