#include <stdio.h>
#include <stdlib.h>
#define FOR(i,a,b) for(i=a;i<=b;++i)
#define N 100010
int t;
void start(void){
freopen("zvon.in","r",stdin);
freopen("zvon.out","w",stdout);
scanf("%d",&t);
}
int n;
int *cine[N];
int timp[N],cati[N];
void scan(void){
int i,x,y;
scanf("%d",&n);
FOR(i,1,n-1){
scanf("%d%d",&x,&y);
++cati[x];
cine[x]=(int*)realloc(cine[x],cati[x]*sizeof(int)+4);
cine[x][cati[x]]=y;
}
}
int max(int a,int b){
if (a>b)
return a;
return b;
}
int cmp(const void *a,const void *b){
return timp[*(int*)b]-timp[*(int*)a];
}
void swap(int a,int b,int h[]){
int aux;
aux=h[a];
h[a]=h[b];
h[b]=aux;
}
void down_heap(int i,int nr,int h[]){
int max=i;
if (2*i<=nr && timp[h[max]] > timp[h[2*i]])
max=2*i;
if (2*i+1<=nr && timp[h[max]] > timp[h[2*i+1]])
max=2*i+1;
if (max!=i){
swap(max,i,h);
down_heap(max,nr,h);
}
}
void sort(int x){
//qsort(cine[x]+1,cati[x],sizeof(int),cmp);
int i,nr=cati[x];
for (i=nr/2;i;--i)
down_heap(i,nr,cine[x]);
for (i=nr;i>1;--i){
swap(1,i,cine[x]);
down_heap(1,i-1,cine[x]);
}
}
void df(int x){
int i,y;
FOR(i,1,cati[x])
df(cine[x][i]);
if (cati[x]>0)
cine[x][0]=0;
sort(x);
FOR(i,1,cati[x]){
y=cine[x][i];
timp[x]=max(timp[x],i+timp[y]);
}
}
void solve(void){
int i;
FOR(i,1,n)timp[i]=0;
df(1);
FOR(i,1,n){
cine[i]=(int*)realloc(cine[i],0);
//free(cine[i]);
cati[i]=0;
}
printf("%d\n",timp[1]);
}
int main(void){
start();
while(t--){
scan();
solve();
}
exit(0);
}