#include <bits/stdc++.h>
using namespace std;
const int N=200050;

int t,n;
int first[N],dext[N<<1],d[N<<1],ecnt=0;
int deep[N],mindeep[N],ans;
void add(int x,int y){dext[++ecnt]=first[x],first[x]=ecnt,d[ecnt]=y;}
void dfs(int x,int fa)
{
	//cout<<x<<" "<<fa<<endl;
	vector<int> a;
	deep[x]=deep[fa]+1;
	mindeep[x]=1<<30;
	int cnt=0;
	for(int i=first[x];i;i=dext[i])
	{
		int v=d[i];
		if(v==fa) continue;
		cnt++;
		dfs(v,x);
		mindeep[x]=min(mindeep[x],mindeep[v]);
		a.push_back(mindeep[v]);
	}
	if(cnt==0) mindeep[x]=deep[x];
	else
	{
		sort(a.begin(),a.end());
		//cout<<x<<endl;
		//for(int i=0;i<(int)a.size();i++)
			//cout<<a[i]<<" ";
		//cout<<"A"<<endl;
		for(int i=0;i<(int)a.size()-1;i++)
			ans=max(ans,a[i]-deep[x]+1);
		if(fa==0) ans=max(ans,a[0]-deep[x]);
		else ans=max(ans,a[a.size()-1]-deep[x]+1);
	}
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++) first[i]=0;
		ecnt=0;
		for(int i=1;i<n;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			add(x,y);
			add(y,x);
		}
		ans=0;
		//puts("A");
		dfs(1,0);
		//return 0;
		printf("%d\n",ans);
		//puts("A");
	}
	return 0;
}