#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<ii> vii;
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repd(i, n) for(int i = n-1; i >= 0; --i)
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
struct st { int m1, rv, m2; };
st dfs(int u, int p, vvi& g) {
unordered_map<int, int> diff; int sum = 0;
for (int v : g[u]) if (v!=p) {
st ch = dfs(v, u, g);
sum += ch.m1;
diff[ch.rv] += ch.m2-ch.m1;
}
int rv = 1, gap = 0; st ch{INT_MAX, 0, 0};
while (true) {
int val = rv+sum+diff[rv];
if (val<ch.m1) ch.m2 = ch.m1, ch.m1 = val, ch.rv = rv;
else if (val<ch.m2) ch.m2 = val;
if (diff[rv]==0) gap++;
if (gap==2) break;
rv++;
}
return ch;
}
int main(int argc, char* argv[]) {
int t; cin>>t;
rep(ti, t) {
int n; cin>>n;
vvi g(n, vi());
rep(i, n) {
int u = i, v; cin>>v; v--;
if (v<0) continue;
g[u].push_back(v);
g[v].push_back(u);
}
int res = dfs(0, -1, g).m1;
cout<<"Case #"<<(ti+1)<<": "<<res<<endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGludCBsbDsKdHlwZWRlZiBwYWlyPGludCwgaW50PiBpaTsKdHlwZWRlZiB2ZWN0b3I8aW50PiB2aTsKdHlwZWRlZiB2ZWN0b3I8bGw+IHZsOwp0eXBlZGVmIHZlY3Rvcjx2aT4gdnZpOwp0eXBlZGVmIHZlY3Rvcjx2bD4gdnZsOwp0eXBlZGVmIHZlY3RvcjxpaT4gdmlpOwojZGVmaW5lIHJlcChpLCBuKSBmb3IoaW50IGkgPSAwOyBpIDwgbjsgKytpKQojZGVmaW5lIHJlcGQoaSwgbikgZm9yKGludCB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