#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MOD = 1e9 + 7;
void solve(){
int n;
cin >> n;
vector<vector<int>> g(n + 1, vector<int>());
for(int i = 2; i <= n; i++){
int x;
cin >> x;
g[x].push_back(i);
}
int ans = n;
vector<int> dp(n + 1, 0);
auto dfs = [&](int x, auto && self) -> void {
for(auto y: g[x]){
self(y, self);
}
multiset<int> p;
for(auto y: g[x]){
p.insert(dp[y]);
}
if(!p.size())return;
while(p.size() > 2){
auto x = *(p.begin());
p.erase(p.find(x));
auto y = *(p.begin());
p.erase(p.find(y));
p.insert(max(x, y) + 1);
}
int h = *(p.begin());
p.erase(p.find(h));
if(p.size()){
int g = *(p.begin());
dp[x] = max(g, h) + 1;
}
else dp[x] = h + 1;
};
dfs(1, dfs);
cout << dp[1] << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
for(int i = 1; i <= t; i++){
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1PRCA9IDFlOSArIDc7Cgp2b2lkIHNvbHZlKCl7CglpbnQgbjsKCWNpbiA+PiBuOwoJCgl2ZWN0b3I8dmVjdG9yPGludD4+IGcobiArIDEsIHZlY3RvcjxpbnQ+KCkpOwoJCglmb3IoaW50IGkgPSAyOyBpIDw9IG47IGkrKyl7CgkJaW50IHg7CgkJY2luID4+IHg7CgkJZ1t4XS5wdXNoX2JhY2soaSk7Cgl9CgkKCWludCBhbnMgPSBuOwoJdmVjdG9yPGludD4gZHAobiArIDEsIDApOwoJYXV0byBkZnMgPSBbJl0oaW50IHgsIGF1dG8gJiYgc2VsZikgLT4gdm9pZCB7CgkJZm9yKGF1dG8geTogZ1t4XSl7CgkJCXNlbGYoeSwgc2VsZik7CgkJfQoJCQoJCW11bHRpc2V0PGludD4gcDsKCQkKCQlmb3Io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