#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);
}
vector<int> p;
for(auto y: g[x]){
p.push_back(dp[y]);
}
if(!p.size())return;
sort(p.begin(), p.end());
dp[x] = p.back() + 1;
int cur = 1;
for(int i = p.size() - 2; i >= 0; i--){
dp[x] = max(dp[x], p[i] + cur);
if(i >= 1)cur++;
}
};
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+IHg7CgkJZ1t4XS5wdXNoX2JhY2soaSk7Cgl9CgkKCWludCBhbnMgPSBuOwoJdmVjdG9yPGludD4gZHAobiArIDEsIDApOwoJYXV0byBkZnMgPSBbJl0oaW50IHgsIGF1dG8gJiYgc2VsZikgLT4gdm9pZCB7CgkJZm9yKGF1dG8geTogZ1t4XSl7CgkJCXNlbGYoeSwgc2VsZik7CgkJfQoJCQoJCXZlY3RvcjxpbnQ+IHA7CgkJCgkJZm9yKGF1dG8geTogZ1t4XSl7CgkJCXAucHVzaF9iYWNrKGRwW3ldKTsKCQl9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