#include <bits/stdc++.h>
#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define For(i, st, en) for(int i=(st); i<=(int)(en); i++)
#define Ford(i, st, en) for(int i=(st); i>=(int)(en); i--)
#define forn(i, n) for(int i=0; i<(int)(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define in(x) int (x); input((x));
#define x first
#define y second
#define less(a,b) ((a) < (b) - EPS)
#define more(a,b) ((a) > (b) + EPS)
#define eq(a,b) (fabs((a) - (b)) < EPS)
#define remax(a, b) ((a) = (b) > (a) ? (b) : (a))
#define remin(a, b) ((a) = (b) < (a) ? (b) : (a))
using namespace std;
template < typename T>
T gcd( T a, T b) {
while ( b > 0 ) {
a % = b;
swap( a, b) ;
}
return a;
}
typedef long double ld; typedef unsigned int uint; template < class _T> inline _T sqr( const _T& x) { return x * x; } template < class _T> inline string tostr( const _T& a) { ostringstream os( "" ) ; os << a; return os.str ( ) ; } const ld PI = 3.1415926535897932384626433832795L; const double EPS = 1e-9 ; char TEMPORARY_CHAR;
typedef long long ll; typedef unsigned long long ull; typedef set < int > SI; typedef vector < int > VI; typedef vector < vector < int > > VVI; typedef map < string, int > MSI; typedef pair < int , int > PII; const int INF = 1e9 ; inline void input( int & a) { a = 0 ; while ( ( ( TEMPORARY_CHAR = getchar ( ) ) > '9' || TEMPORARY_CHAR < '0' ) && ( TEMPORARY_CHAR ! = '-' ) ) { } char neg = 0 ; if ( TEMPORARY_CHAR == '-' ) { neg = 1 ; TEMPORARY_CHAR = getchar ( ) ; } while ( TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0' ) { a = 10 * a + TEMPORARY_CHAR - '0' ; TEMPORARY_CHAR = getchar ( ) ; } if ( neg) a = - a; } inline void out( long long a) { if ( ! a) putchar ( '0' ) ; if ( a < 0 ) { putchar ( '-' ) ; a = - a; } char s[ 20 ] ; int i; for ( i = 0 ; a; ++ i) { s[ i] = '0' + a % 10 ; a / = 10 ; } ford( j, i) putchar ( s[ j] ) ; } inline int nxt( ) { in( ret) ; return ret; }
struct node {
long long x;
int pos;
node * l;
node * r;
node( long long y, int p) {
x = y;
pos = p;
l = r = 0 ;
}
node( node * L, node * R) {
if ( L- > x < R- > x) {
x = L- > x;
pos = L- > pos;
} else {
x = R- > x;
pos = R- > pos;
}
l = L;
r = R;
}
} ;
node * add( node * v, int tl, int tr, int pos, long long ad) {
if ( tl == tr) {
return new node( v- > x + ad, tl) ;
}
int tm = ( tl + tr) / 2 ;
if ( pos <= tm ) {
return new node( add( v- > l, tl, tm , pos, ad) , v- > r) ;
} else {
return new node( v- > l, add( v- > r, tm + 1 , tr, pos, ad) ) ;
}
}
node * build( int tl, int tr) {
if ( tl == tr) {
return new node( tl, tl) ;
}
int tm = ( tl + tr) / 2 ;
return new node( build( tl, tm ) , build( tm + 1 , tr) ) ;
}
struct solution {
vector < vector < int > > g;
int n;
node * root;
struct data {
long long addVal;
int pos;
long long val;
} ;
void dfs( int v, data & res) {
node * rt = root;
long long sum = 0 ;
for ( int to : g[ v] ) {
data x;
dfs( to, x) ;
sum + = x.val ;
rt = add( rt, 1 , n, x.pos , x.addVal ) ;
}
res.pos = rt- > pos;
res.val = rt- > first;
rt = add( rt, 1 , n, res.pos , 1e12 ) ;
res.addVal = rt- > first - res.val ;
res.val + = sum;
}
void solve( int c) {
cin >> n;
g.resize ( n) ;
root = build( 1 , n) ;
for ( int i = 0 ; i < n; ++ i) {
int x;
cin >> x;
-- x;
if ( x >= 0 ) {
g[ x] .push_back ( i) ;
}
}
data ans;
dfs( 0 , ans) ;
cout << "Case #" << c << ": " << ans.val << endl;
}
} ;
int main( int argc, char ** argv) {
#ifdef LOCAL
freopen ( "input.txt" , "r" , stdin ) ;
//freopen("corporate_gifting.txt", "r", stdin);
freopen ( "output.txt" , "w" , stdout ) ;
#else
#endif
int t;
cin >> t;
for ( int i = 1 ; i <= t; ++ i) {
solution sol;
sol.solve ( i) ;
}
#ifdef LOCAL
cerr << "Time elapsed: " << ( double ) clock ( ) / CLOCKS_PER_SEC * 1000 << " ms.\n " ;
#endif // LOCAL
return 0 ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIGNscih4KSBtZW1zZXQoKHgpLCAwLCBzaXplb2YoeCkpCiNkZWZpbmUgYWxsKHgpICh4KS5iZWdpbigpLCAoeCkuZW5kKCkKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBGb3IoaSwgc3QsIGVuKSBmb3IoaW50IGk9KHN0KTsgaTw9KGludCkoZW4pOyBpKyspCiNkZWZpbmUgRm9yZChpLCBzdCwgZW4pIGZvcihpbnQgaT0oc3QpOyBpPj0oaW50KShlbik7IGktLSkKI2RlZmluZSBmb3JuKGksIG4pIGZvcihpbnQgaT0wOyBpPChpbnQpKG4pOyBpKyspCiNkZWZpbmUgZm9yZChpLCBuKSBmb3IoaW50IGk9KG4pLTE7IGk+PTA7IGktLSkKI2RlZmluZSBmb3JpKGl0LCB4KSBmb3IgKF9fdHlwZW9mKCh4KS5iZWdpbigpKSBpdCA9ICh4KS5iZWdpbigpOyBpdCAhPSAoeCkuZW5kKCk7IGl0KyspCiNkZWZpbmUgaW4oeCkgaW50ICh4KTsgaW5wdXQoKHgpKTsKI2RlZmluZSB4IGZpcnN0CiNkZWZpbmUgeSBzZWNvbmQKI2RlZmluZSBsZXNzKGEsYikgKChhKSA8IChiKSAtIEVQUykKI2RlZmluZSBtb3JlKGEsYikgKChhKSA+IChiKSArIEVQUykKI2RlZmluZSBlcShhLGIpIChmYWJzKChhKSAtIChiKSkgPCBFUFMpCiNkZWZpbmUgcmVtYXgoYSwgYikgKChhKSA9IChiKSA+IChhKSA/IChiKSA6IChhKSkKI2RlZmluZSByZW1pbihhLCBiKSAoKGEpID0gKGIpIDwgKGEpID8gKGIpIDogKGEpK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ICogYWRkKG5vZGUgKiB2LCBpbnQgdGwsIGludCB0ciwgaW50IHBvcywgbG9uZyBsb25nIGFkKSB7CiAgICBpZiAodGwgPT0gdHIpIHsKICAgICAgICByZXR1cm4gbmV3IG5vZGUodi0+eCArIGFkLCB0bCk7CiAgICB9CiAgICBpbnQgdG0gPSAodGwgKyB0cikgLyAyOwogICAgaWYgKHBvcyA8PSB0bSkgewogICAgICAgIHJldHVybiBuZXcgbm9kZShhZGQodi0+bCwgdGwsIHRtLCBwb3MsIGFkKSwgdi0+cik7CiAgICB9IGVsc2UgewogICAgICAgIHJldHVybiBuZXcgbm9kZSh2LT5sLCBhZGQodi0+ciwgdG0gKyAxLCB0ciwgcG9zLCBhZCkpOwogICAgfQp9Cgpub2RlICogYnVpbGQoaW50IHRsLCBpbnQgdHIpIHsKICAgIGlmICh0bCA9PSB0cikgewogICAgICAgIHJldHVybiBuZXcgbm9kZSh0bCwgdGwpOwogICAgfQogICAgaW50IHRtID0gKHRsICsgdHIpIC8gMjsKICAgIHJldHVybiBuZXcgbm9kZShidWlsZCh0bCwgdG0pLCBidWlsZCh0bSArIDEsIHRyKSk7Cn0KCnN0cnVjdCBzb2x1dGlvbiB7CiAgICB2ZWN0b3IgPHZlY3RvciA8aW50PiA+IGc7CgogICAgaW50IG47CgogICAgbm9kZSAqIHJvb3Q7CgogICAgc3RydWN0IGRhdGEgewogICAgICAgIGxvbmcgbG9uZyBhZGRWYWw7CiAgICAgICAgaW50IHBvczsKICAgICAgICBsb25nIGxvbmcgdmFsOwogICAgfTsKCiAgICB2b2lkIGRmcyhpbnQgdiwgZGF0YSAmIHJlcykgewogICAgICAgIG5vZGUgKiBydCA9IHJvb3Q7CiAgICAgICAgbG9uZyBsb25nIHN1bSA9IDA7CiAgICAgICAgZm9yIChpbnQgdG8gOiBnW3ZdKSB7CiAgICAgICAgICAgIGRhdGEgeDsKICAgICAgICAgICAgZGZzKHRvLCB4KTsKICAgICAgICAgICAgc3VtICs9IHgudmFsOwogICAgICAgICAgICBydCA9IGFkZChydCwgMSwgbiwgeC5wb3MsIHguYWRkVmFsKTsKICAgICAgICB9CiAgICAgICAgcmVzLnBvcyA9IHJ0LT5wb3M7CiAgICAgICAgcmVzLnZhbCA9IHJ0LT5maXJzdDsKICAgICAgICBydCA9IGFkZChydCwgMSwgbiwgcmVzLnBvcywgMWUxMik7CiAgICAgICAgcmVzLmFkZFZhbCA9IHJ0LT5maXJzdCAtIHJlcy52YWw7CiAgICAgICAgcmVzLnZhbCArPSBzdW07CiAgICB9CgogICAgdm9pZCBzb2x2ZShpbnQgYykgewogICAgICAgIGNpbiA+PiBuOwogICAgICAgIGcucmVzaXplKG4pOwogICAgICAgIHJvb3QgPSBidWlsZCgxLCBuKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgICAgICAgICBpbnQgeDsKICAgICAgICAgICAgY2luID4+IHg7CiAgICAgICAgICAgIC0teDsKICAgICAgICAgICAgaWYgKHggPj0gMCkgewogICAgICAgICAgICAgICAgZ1t4XS5wdXNoX2JhY2soaSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZGF0YSBhbnM7CiAgICAgICAgZGZzKDAsIGFucyk7CiAgICAgICAgY291dCA8PCAiQ2FzZSAjIiA8PCBjIDw8ICI6ICIgPDwgYW5zLnZhbCA8PCBlbmRsOwogICAgfQoKCgp9OwoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgKiogYXJndikgewojaWZkZWYgTE9DQUwKICAgIGZyZW9wZW4oImlucHV0LnR4dCIsICJyIiwgc3RkaW4pOwogICAgLy9mcmVvcGVuKCJjb3Jwb3JhdGVfZ2lmdGluZy50eHQiLCAiciIsIHN0ZGluKTsKICAgIGZyZW9wZW4oIm91dHB1dC50eHQiLCAidyIsIHN0ZG91dCk7CiNlbHNlCgojZW5kaWYKCiAgICBpbnQgdDsKICAgIGNpbiA+PiB0OwoKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IHQ7ICsraSkgewogICAgICAgIHNvbHV0aW9uIHNvbDsKICAgICAgICBzb2wuc29sdmUoaSk7CiAgICB9CgojaWZkZWYgTE9DQUwKICAgIGNlcnIgPDwgIlRpbWUgZWxhcHNlZDogIiA8PCAoZG91YmxlKWNsb2NrKCkgLyBDTE9DS1NfUEVSX1NFQyAqIDEwMDAgPDwgIiBtcy5cbiI7CiNlbmRpZiAvLyBMT0NBTAogICAgcmV0dXJuIDA7Cn0K