#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
vector <int> adj[100010];
vector <int> adv[100010];
int arr[100010],crr[100010];
//vector <int> xtra;
void init()
{
for(int i=0;i<100010;i++)
{
adj[i].clear();
adv[i].clear();
arr[i]=crr[i]=0;
}
return;
}
void bfs(int idx)
{
queue <int> q;
q.push(idx);
while(!q.empty())
{
int x=q.front();
q.pop();
arr[adv[x][0]]=max(arr[adv[x][0]],arr[x]+1);
if(adv[x][0]!=1)
q.push(adv[x][0]);
else break;
}
queue <int> p;
p.push(1);
while(!p.empty())
{
int x=p.front();
p.pop();
int max1=0,max2=0;
for(int i=0;i<adj[x].size();i++)
{
//cout<<"adj["<<x<<"]="<<adj[x][i]<<endl;;
if(arr[adj[x][i]]>=max1)
{
max2=max1;
max1=arr[adj[x][i]];
}
}
//cout<<max1<<" "<<max2<<endl;
crr[x]=max(max1+max2,crr[x]);
}
return;
}
int main()
{
int tc;
scanf("%d",&tc);
while(tc--)
{
init();
int n;
scanf("%d",&n);
arr[1]=1;crr[1]=1;
for(int i=2;i<=n;i++)
{
int a;
scanf("%d",&a);
adj[a].push_back(i);
adv[i].push_back(a);
arr[i]=1;crr[i]=1;
bfs(i);
int x,y;
x=*(max_element(arr,arr+n));
y=*(max_element(crr,crr+n));
//cout<<"x="<<x-1<<" y="<<y<<endl;
printf("%d\n",max(x-1,y));
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxhbGdvcml0aG0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnZlY3RvciA8aW50PiBhZGpbMTAwMDEwXTsKdmVjdG9yIDxpbnQ+IGFkdlsxMDAwMTBdOwppbnQgYXJyWzEwMDAxMF0sY3JyWzEwMDAxMF07Ci8vdmVjdG9yIDxpbnQ+IHh0cmE7CnZvaWQgaW5pd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