#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstring>
#include <iterator>
#include <fstream>
using namespace std;
#define pb push_back
#define rs resize
#define mp make_pair
#define inf 1e9
#define pi 3.1415926535897932384626433832795
#define sz(a) (int)(a).size()
#define Sort(a) sort((a).begin(), (a).end())
#define Reverse(a) reverse((a).begin(), (a).end())
#define sf scanf
#define pf printf
#define ms(a, x) memset((a), (x), sizeof(a))
#pragma comment(linker, "/STACK:200000000")
typedef vector <int> vi;
typedef vector <vi> vvi;
typedef vector <vvi> vvvi;
typedef vector <vvvi> vvvvi;
typedef vector <bool> vb;
typedef vector <vb> vvb;
typedef vector <vvb> vvvb;
typedef vector <vvvb> vvvvb;
typedef long long ll;
typedef vector <ll> vl;
typedef vector <vl> vvl;
typedef vector <vvl> vvvl;
typedef vector <vvvl> vvvvl;
typedef pair <int, int> ii;
typedef vector <ii> vii;
typedef vector <vii> vvii;
typedef pair <int, ll> il;
typedef vector <il> vil;
typedef vector <vil> vvil;
typedef pair <ll, int> li;
typedef vector <li> vli;
typedef vector <vli> vvli;
typedef set <int> si;
typedef set <li> sli;
typedef set <il> sil;
typedef vector <string> vs;
typedef vector <vs> vvs;
typedef vector <vvs> vvvs;
typedef map <string, int> msi;
typedef map <char, int> mci;
typedef pair <ll, ll> pll;
struct str {
ll x, take, untake;
str() {}
str(ll x, ll take, ll untake) : x(x), take(take), untake(untake) {}
bool operator <(const str &other) const {
return x < other.x;
}
};
void dfs(int v, vvi &g, vl &x, vl &ansx, vl &y, vl &ansy) {
if (sz(g[v]) == 0) {
ansx[v] = x[v] = 1;
ansy[v] = y[v] = 2;
return;
}
for (int i = 0; i < sz(g[v]); ++i) {
dfs(g[v][i], g, x, ansx, y, ansy);
}
vector <str> a;
for (int i = 0; i < sz(g[v]); ++i) {
a.pb(str(x[g[v][i]], ansx[g[v][i]], ansy[g[v][i]]));
}
Sort(a);
vector <str> b(1, a[0]);
for (int i = 1; i < sz(a); ++i) {
if (a[i].x != a[i - 1].x) {
b.pb(a[i]);
} else {
b.back().take += a[i].take;
b.back().untake += a[i].untake;
}
}
ll take = 0, untake = 0;
for (int i = 0; i < sz(b); ++i) {
take += b[i].take;
untake += b[i].untake;
}
ll X = 0, AX = (ll)1e12, Y = 0, AY = (ll)1e12;
for (int i = 0; i < sz(b); ++i) {
ll r = take - b[i].take + b[i].untake + b[i].x;
if (r < AX) {
Y = X;
AY = AX;
X = b[i].x;
AX = r;
}
else if (r < AY) {
Y = b[i].x;
AY = r;
}
}
int pos = 0;
while (pos < sz(b) && b[pos].x == pos + 1)
++pos;
if (take + pos + 1 < AX) {
AY = AX;
Y = X;
AX = take + pos + 1;
X = pos + 1;
}
else if (take + pos + 1 < AY) {
AY = take + pos + 1;
Y = pos + 1;
}
x[v] = X;
y[v] = Y;
ansx[v] = AX;
ansy[v] = AY;
return;
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T;
cin >> T;
for (int test = 1; test <= T; ++test) {
int n;
cin >> n;
vvi g(n);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
--x;
if (x >= 0)
g[x].pb(i);
}
vl x(n), y(n), ansx(n), ansy(n);
dfs(0, g, x, ansx, y, ansy);
cout << "Case #" << test << ": " << ansx[0] << endl;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8Y3RpbWU+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHN0YWNrPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxkZXF1ZT4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxpdG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ICAgICAgICByZXR1cm47CiAgICB9CiAgICBmb3IgKGludCBpID0gMDsgaSA8IHN6KGdbdl0pOyArK2kpIHsKICAgICAgICBkZnMoZ1t2XVtpXSwgZywgeCwgYW5zeCwgeSwgYW5zeSk7CiAgICB9CiAgICB2ZWN0b3IgPHN0cj4gYTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc3ooZ1t2XSk7ICsraSkgewogICAgICAgIGEucGIoc3RyKHhbZ1t2XVtpXV0sIGFuc3hbZ1t2XVtpXV0sIGFuc3lbZ1t2XVtpXV0pKTsKICAgIH0KICAgIFNvcnQoYSk7CiAgICB2ZWN0b3IgPHN0cj4gYigxLCBhWzBdKTsKICAgIGZvciAoaW50IGkgPSAxOyBpIDwgc3ooYSk7ICsraSkgewogICAgICAgIGlmIChhW2ldLnggIT0gYVtpIC0gMV0ueCkgewogICAgICAgICAgICBiLnBiKGFbaV0pOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGIuYmFjaygpLnRha2UgKz0gYVtpXS50YWtlOwogICAgICAgICAgICBiLmJhY2soKS51bnRha2UgKz0gYVtpXS51bnRha2U7CiAgICAgICAgfQogICAgfQogICAgbGwgdGFrZSA9IDAsIHVudGFrZSA9IDA7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IHN6KGIpOyArK2kpIHsKICAgICAgICB0YWtlICs9IGJbaV0udGFrZTsKICAgICAgICB1bnRha2UgKz0gYltpXS51bnRha2U7CiAgICB9CiAgICBsbCBYID0gMCwgQVggPSAobGwpMWUxMiwgWSA9IDAsIEFZID0gKGxsKTFlMTI7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IHN6KGIpOyArK2kpIHsKICAgICAgICBsbCByID0gdGFrZSAtIGJbaV0udGFrZSArIGJbaV0udW50YWtlICsgYltpXS54OwogICAgICAgIGlmIChyIDwgQVgpIHsKICAgICAgICAgICAgWSA9IFg7CiAgICAgICAgICAgIEFZID0gQVg7CiAgICAgICAgICAgIFggPSBiW2ldLng7CiAgICAgICAgICAgIEFYID0gcjsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZiAociA8IEFZKSB7CiAgICAgICAgICAgIFkgPSBiW2ldLng7CiAgICAgICAgICAgIEFZID0gcjsKICAgICAgICB9CiAgICB9CiAgICBpbnQgcG9zID0gMDsKICAgIHdoaWxlIChwb3MgPCBzeihiKSAmJiBiW3Bvc10ueCA9PSBwb3MgKyAxKQogICAgICAgICsrcG9zOwogICAgaWYgKHRha2UgKyBwb3MgKyAxIDwgQVgpIHsKICAgICAgICBBWSA9IEFYOwogICAgICAgIFkgPSBYOwogICAgICAgIEFYID0gdGFrZSArIHBvcyArIDE7CiAgICAgICAgWCA9IHBvcyArIDE7CiAgICB9CiAgICBlbHNlIGlmICh0YWtlICsgcG9zICsgMSA8IEFZKSB7CiAgICAgICAgQVkgPSB0YWtlICsgcG9zICsgMTsKICAgICAgICBZID0gcG9zICsgMTsKICAgIH0KICAgIHhbdl0gPSBYOwogICAgeVt2XSA9IFk7CiAgICBhbnN4W3ZdID0gQVg7CiAgICBhbnN5W3ZdID0gQVk7CiAgICByZXR1cm47Cn0KCmludCBtYWluKCkgewogICAgZnJlb3BlbigiaW5wdXQudHh0IiwgInIiLCBzdGRpbik7CiAgICBmcmVvcGVuKCJvdXRwdXQudHh0IiwgInciLCBzdGRvdXQpOwogICAgaW50IFQ7CiAgICBjaW4gPj4gVDsKICAgIGZvciAoaW50IHRlc3QgPSAxOyB0ZXN0IDw9IFQ7ICsrdGVzdCkgewogICAgICAgIGludCBuOwogICAgICAgIGNpbiA+PiBuOwogICAgICAgIHZ2aSBnKG4pOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICAgICAgICAgIGludCB4OwogICAgICAgICAgICBjaW4gPj4geDsKICAgICAgICAgICAgLS14OwogICAgICAgICAgICBpZiAoeCA+PSAwKQogICAgICAgICAgICAgICAgZ1t4XS5wYihpKTsKICAgICAgICB9CiAgICAgICAgdmwgeChuKSwgeShuKSwgYW5zeChuKSwgYW5zeShuKTsKICAgICAgICBkZnMoMCwgZywgeCwgYW5zeCwgeSwgYW5zeSk7CiAgICAgICAgY291dCA8PCAiQ2FzZSAjIiA8PCB0ZXN0IDw8ICI6ICIgPDwgYW5zeFswXSA8PCBlbmRsOwogICAgfQp9