#include <bits/stdc++.h>
using namespace std;
void solve() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<vector<int>> tree(n + 1);
for (int i = 2; i <= n; ++i) {
int p;
cin >> p;
tree[p].push_back(i);
}
vector<int> depth(n + 1, 0);
// Define dfs as a lambda function
function<void(int)> dfs = [&](int u) {
vector<int> child_depths;
for (int v : tree[u]) {
dfs(v);
child_depths.push_back(depth[v]);
}
if (child_depths.empty()) {
depth[u] = 1;
} else {
sort(child_depths.rbegin(), child_depths.rend()); // Sort in decreasing order
int max_temp = 0;
for (int i = 0; i < child_depths.size(); ++i) {
int temp = child_depths[i] + i;
if (temp > max_temp) {
max_temp = temp;
}
}
depth[u] = max_temp;
}
};
dfs(1);
cout << depth[1] << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CQoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgc29sdmUoKSB7CiAgICBpbnQgdDsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkgewogICAgICAgIGludCBuOwogICAgICAgIGNpbiA+PiBuOwogICAgICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gdHJlZShuICsgMSk7CiAgICAgICAgZm9yIChpbnQgaSA9IDI7IGkgPD0gbjsgKytpKSB7CiAgICAgICAgICAgIGludCBwOwogICAgICAgICAgICBjaW4gPj4gcDsKICAgICAgICAgICAgdHJlZVtwXS5wdXNoX2JhY2soaSk7CiAgICAgICAgfQoKICAgICAgICB2ZWN0b3I8aW50PiBkZXB0aChuICsgMSwgMCk7CgogICAgICAgIC8vIERlZmluZSBkZnMgYXMgYSBsYW1iZGEgZnVuY3Rpb24KICAgICAgICBmdW5jdGlvbjx2b2lkKGludCk+IGRmcyA9IFsmXShpbnQgdSkgewogICAgICAgICAgICB2ZWN0b3I8aW50PiBjaGlsZF9kZXB0aHM7CiAgICAgICAgICAgIGZvciAoaW50IHYgOiB0cmVlW3VdKSB7CiAgICAgICAgICAgICAgICBkZnModik7CiAgICAgICAgICAgICAgICBjaGlsZF9kZXB0aHMucHVzaF9iYWNrKGRlcHRoW3ZdKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoY2hpbGRfZGVwdGhzLmVtcHR5KCkpIHsKICAgICAgICAgICAgICAgIGRlcHRoW3VdID0gMTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHNvcnQoY2hpbGRfZGVwdGhzLnJiZWdpbigpLCBjaGlsZF9kZXB0aHMucmVuZCgpKTsgLy8gU29ydCBpbiBkZWNyZWFzaW5nIG9yZGVyCiAgICAgICAgICAgICAgICBpbnQgbWF4X3RlbXAgPSAwOwogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBjaGlsZF9kZXB0aHMuc2l6ZSgpOyArK2kpIHsKICAgICAgICAgICAgICAgICAgICBpbnQgdGVtcCA9IGNoaWxkX2RlcHRoc1tpXSArIGk7CiAgICAgICAgICAgICAgICAgICAgaWYgKHRlbXAgPiBtYXhfdGVtcCkgewogICAgICAgICAgICAgICAgICAgICAgICBtYXhfdGVtcCA9IHRlbXA7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZGVwdGhbdV0gPSBtYXhfdGVtcDsKICAgICAgICAgICAgfQogICAgICAgIH07CgogICAgICAgIGRmcygxKTsKICAgICAgICBjb3V0IDw8IGRlcHRoWzFdIDw8ICdcbic7CiAgICB9Cn0KaW50IG1haW4oKSB7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKG51bGxwdHIpOwogICAgc29sdmUoKTsKICAgIHJldHVybiAwOwp9Cg==