#include <bits/stdc++.h>
using namespace std;
inline int read(){
	int s=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
	return s*f;
}
const int N=2e5+5,inf=1e9;
struct Edge{int to,next;}e[N<<1];
int head[N],ecnt;
inline void adde(int u,int v){e[++ecnt]=(Edge){v,head[u]};head[u]=ecnt;}
int ans,f[N],n,T;
void dfs(int u,int fa){
	int son=0,mx=0,smx=0;
	f[u]=inf;
	for(int i=head[u],v;i;i=e[i].next){
		v=e[i].to;if(v==fa)continue;
		dfs(v,u);++son;
		f[u]=min(f[u],f[v]+1);
		if(f[v]>mx)smx=mx,mx=f[v];
		else if(f[v]>smx)smx=f[v];
	}
	if(!son){f[u]=0;return;}
	if(!fa){
		if(son==1)ans=max(ans,mx+1);
		else ans=max(ans,max(smx+2,mx+1));
	}else if(son>=2)ans=max(ans,mx+2);
}
int main(){
	T=read();
	while(T--){
		n=read();
		ecnt=0;memset(head+1,0,n<<2);
		for(int i=1,u,v;i<n;++i){
			u=read();v=read();
			adde(u,v);adde(v,u);
		}
		ans=0;
		dfs(1,0);
		printf("%d\n",ans);
	}
	return 0;
}
					    		   			 		  	 	 			 	