#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int dp[200010][21];
vector<int> g[200010];
void dfs(int u, int fa) {
int i;
for (i = 1; i <= 20; i++)
dp[u][i] = i;
for (i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == fa) continue;
dfs(v, u);
for (int i = 1; i <= 20; i++) {
int tmp = INF;
for (int j = 1; j <= 20; j++) {
if (i == j) continue;
tmp = min(tmp, dp[v][j]);
}
dp[u][i] += tmp;
}
}
}
int main()
{
int t,i,j,k,xx,n;
cin>>t;
for(xx=1;xx<=t;xx++)
{
memset(dp,0,sizeof(dp));
memset(g, 0, sizeof(g));
cin>>n;
for(i=0;i<n;i++)
{
cin>>j;
g[i+1].push_back(j);
g[j].push_back(i+1);
}
dfs(0, -1);
int ans = INF;
for (int i = 1; i <= 20; i++)
ans = min(ans, dp[0][i]-i);
printf("Case #%d: %d\n",xx, ans);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgSU5GIDB4M2YzZjNmM2YKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IGRwWzIwMDAxMF1bMjFdOwp2ZWN0b3I8aW50PiBnWzIwMDAxMF07CnZvaWQgZGZzKGludCB1LCBpbnQgZmEpIHsKICAgIGludCBpOwogICAgZm9yIChpID0gMTsgaSA8PSAyMDsgaSsrKQogICAgICAgIGRwW3VdW2ldID0gaTsKICAgIGZvciAoaSA9IDA7IGkgPCBnW3VdLnNpemUoKTsgaSsrKSB7CiAgICAgICAgaW50IHYgPSBnW3VdW2ldOwogICAgICAgIGlmICh2ID09IGZhKSBjb250aW51ZTsKICAgICAgICBkZnModiwgdSk7CiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gMjA7IGkrKykgewogICAgICAgICAgICBpbnQgdG1wID0gSU5GOwogICAgICAgICAgICBmb3IgKGludCBqID0gMTsgaiA8PSAyMDsgaisrKSB7CiAgICAgICAgICAgICAgICBpZiAoaSA9PSBqKSBjb250aW51ZTsKICAgICAgICAgICAgICAgIHRtcCA9IG1pbih0bXAsIGRwW3ZdW2pdKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBkcFt1XVtpXSArPSB0bXA7CiAgICAgICAgfQogICAgfQp9CmludCBtYWluKCkKewppbnQgdCxpLGosayx4eCxuOwpjaW4+PnQ7CmZvcih4eD0xO3h4PD10O3h4KyspCnsKICAgIG1lbXNldChkcCwwLHNpemVvZihkcCkpOwogICAgbWVtc2V0KGcsIDAsIHNpemVvZihnKSk7CmNpbj4+bjsKZm9yKGk9MDtpPG47aSsrKQp7CmNpbj4+ajsKZ1tpKzFdLnB1c2hfYmFjayhqKTsKZ1tqXS5wdXNoX2JhY2soaSsxKTsKfQpkZnMoMCwgLTEpOwogICAgICAgIGludCBhbnMgPSBJTkY7CiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gMjA7IGkrKykKICAgICAgICAgICAgYW5zID0gbWluKGFucywgZHBbMF1baV0taSk7CiAgICAgICAgcHJpbnRmKCJDYXNlICMlZDogJWRcbiIseHgsIGFucyk7Cn0KcmV0dXJuIDA7Cn0K