#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;
}