#include <bits/stdc++.h>
#define snuke(i,x) for (__typeof((x).begin()) i = (x).begin(); i != (x).end(); ++i)
#define out(x) std::cout<<(#x)<<":"<<(x)<<std::endl
void Read(int&ret){ret=0;bool ok=0,u=0;for(;;){int c=getchar();if(c>='0'&&c<='9')ret=(ret<<3)+(ret<<1)+c-'0',ok=1;else if(c=='-')u=1;else if(ok){if(u)ret*=-1;return;}}}
long long pow_mod(long long p,int n,long long mod){long long ret=1;for(;n;n>>=1){if(n&1)ret=ret*p%mod;p=p*p%mod;}return ret;}
/****head****/
const int Max_N = 9 + (int)2e5;
const int Max_M = 21;
int fa[Max_N], q[Max_N], n;
int f[Max_N][Max_M], g[Max_N][Max_M];
std::vector<int> son[Max_N];
void bfs() {
int qr = 0; q[qr++] = 0;
for(int i = 0, x; i < qr; ++i) {
x = q[i];
snuke(it, son[x]) if(*it != fa[x]){
q[qr++] = *it;
}
}
for(int i = qr-1, x, j, fir, sec; i >= 0; --i) {
x = q[i];
if(son[x].empty()) for(j = 1; j < Max_M; ++j) f[x][j] = j;
else {
for(j = 1; j < Max_M; ++j) {
f[x][j] = j;
snuke(it, son[x]) f[x][j] += g[*it][j];
}
}
sec = 0; fir = 1;
for(j = 2; j < Max_M; ++j) {
if(f[x][j] >= f[x][fir]) {
if(!sec || f[x][sec] > f[x][j]) sec = j;
} else {
sec = fir; fir = j;
}
}
for(j = 1; j < Max_M; ++j) g[x][j] = j == fir ? f[x][sec] : f[x][fir];
}
}
int main() {
freopen("corporate_gifting.txt","r",stdin);
freopen("D.out","w",stdout);
int _; Read(_);
for(int cas = 0, ans; _--; ) {
Read(n);
for(int i = 0; i < n; ++i) son[i].clear();
for(int i = 0; i < n; ++i) {
Read(fa[i]);
son[--fa[i]].push_back(i);
}
bfs();
ans = ~0u>>1;
for(int i = 1; i < Max_M; ++i) ans = std::min(ans, f[0][i]);
printf("Case #%d: %d\n", ++cas, ans);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgc251a2UoaSx4KSBmb3IgKF9fdHlwZW9mKCh4KS5iZWdpbigpKSBpID0gKHgpLmJlZ2luKCk7IGkgIT0gKHgpLmVuZCgpOyArK2kpCiNkZWZpbmUgb3V0KHgpIHN0ZDo6Y291dDw8KCN4KTw8IjoiPDwoeCk8PHN0ZDo6ZW5kbAp2b2lkIFJlYWQoaW50JnJldCl7cmV0PTA7Ym9vbCBvaz0wLHU9MDtmb3IoOzspe2ludCBjPWdldGNoYXIoKTtpZihjPj0nMCcmJmM8PSc5JylyZXQ9KHJldDw8MykrKHJldDw8MSkrYy0nMCcsb2s9MTtlbHNlIGlmKGM9PSctJyl1PTE7ZWxzZSBpZihvayl7aWYodSlyZXQqPS0xO3JldHVybjt9fX0KbG9uZyBsb25nIHBvd19tb2QobG9uZyBsb25nIHAsaW50IG4sbG9uZyBsb25nIG1vZCl7bG9uZyBsb25nIHJldD0xO2Zvcig7bjtuPj49MSl7aWYobiYxKXJldD1yZXQqcCVtb2Q7cD1wKnAlbW9kO31yZXR1cm4gcmV0O30KLyoqKipoZWFkKioqKi8KY29uc3QgaW50IE1heF9OID0gOSArIChpbnQpMmU1Owpjb25zdCBpbnQgTWF4X00gPSAyMTsKaW50IGZhW01heF9OXSwgcVtNYXhfTl0sIG47CmludCBmW01heF9OXVtNYXhfTV0sIGdbTWF4X05dW01heF9NXTsKc3RkOjp2ZWN0b3I8aW50PiBzb25bTWF4X05dOwp2b2lkIGJmcygpIHsKICAgICAgICBpbnQgcXIgPSAwOyBxW3FyKytdID0gMDsKICAgICAgICBmb3IoaW50IGkgPSAwLCB4OyBpIDwgcXI7ICsraSkgewogICAgICAgICAgICAgICAgeCA9IHFbaV07CiAgICAgICAgICAgICAgICBzbnVrZShpdCwgc29uW3hdKSBpZigqaXQgIT0gZmFbeF0pewogICAgICAgICAgICAgICAgICAgICAgICBxW3FyKytdID0gKml0OwogICAgICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmb3IoaW50IGkgPSBxci0xLCB4LCBqLCBmaXIsIHNlYzsgaSA+PSAwOyAtLWkpIHsKICAgICAgICAgICAgICAgIHggPSBxW2ldOwogICAgICAgICAgICAgICAgaWYoc29uW3hdLmVtcHR5KCkpIGZvcihqID0gMTsgaiA8IE1heF9NOyArK2opIGZbeF1bal0gPSBqOwogICAgICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGZvcihqID0gMTsgaiA8IE1heF9NOyArK2opIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmW3hdW2pdID0gajsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzbnVrZShpdCwgc29uW3hdKSBmW3hdW2pdICs9IGdbKml0XVtqXTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICBzZWMgPSAwOyBmaXIgPSAxOwogICAgICAgICAgICAgICAgZm9yKGogPSAyOyBqIDwgTWF4X007ICsraikgewogICAgICAgICAgICAgICAgICAgICAgICBpZihmW3hdW2pdID49IGZbeF1bZmlyXSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmKCFzZWMgfHwgZlt4XVtzZWNdID4gZlt4XVtqXSkgc2VjID0gajsKICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzZWMgPSBmaXI7IGZpciA9IGo7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICBmb3IoaiA9IDE7IGogPCBNYXhfTTsgKytqKSBnW3hdW2pdID0gaiA9PSBmaXIgPyBmW3hdW3NlY10gOiBmW3hdW2Zpcl07CiAgICAgICAgfQp9CmludCBtYWluKCkgewogICAgICAgIGZyZW9wZW4oImNvcnBvcmF0ZV9naWZ0aW5nLnR4dCIsInIiLHN0ZGluKTsKICAgICAgICBmcmVvcGVuKCJELm91dCIsInciLHN0ZG91dCk7CiAgICAgICAgaW50IF87IFJlYWQoXyk7CiAgICAgICAgZm9yKGludCBjYXMgPSAwLCBhbnM7IF8tLTsgKSB7CiAgICAgICAgICAgICAgICBSZWFkKG4pOwogICAgICAgICAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IG47ICsraSkgc29uW2ldLmNsZWFyKCk7CiAgICAgICAgICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIFJlYWQoZmFbaV0pOwogICAgICAgICAgICAgICAgICAgICAgICBzb25bLS1mYVtpXV0ucHVzaF9iYWNrKGkpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYmZzKCk7CiAgICAgICAgICAgICAgICBhbnMgPSB+MHU+PjE7CiAgICAgICAgICAgICAgICBmb3IoaW50IGkgPSAxOyBpIDwgTWF4X007ICsraSkgYW5zID0gc3RkOjptaW4oYW5zLCBmWzBdW2ldKTsKICAgICAgICAgICAgICAgIHByaW50ZigiQ2FzZSAjJWQ6ICVkXG4iLCArK2NhcywgYW5zKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIDA7Cn0K