#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<sstream>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define per(i,n,x) for(int i=n;i>=x;i--)
#define sz(a) int(a.size())
#define rson mid+1,r,p<<1|1
#define pii pair<int,int>
#define lson l,mid,p<<1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps=1e-8;
const int mod=1e9+7;
const int N=2e5+10;
const int inf=1e9;
int t,n;
vector<int>g[N];
int d[N],p[N],k;
void dfs(int u,int fa){
	int flag=1;
	for(int x:g[u]) if(x!=fa){
		d[x]=d[u]+1;
		flag=0;
		dfs(x,u);
	}
}
bool dfs1(int u,int fa){
	for(int x:g[u]) if(x!=fa){
		if(!dfs1(x,u)) return false;
	}
	vector<int>v;
	for(int x:g[u]) if(x!=fa){
		v.pb(p[x]);
	}
	sort(v.begin(), v.end());
	reverse(v.begin(), v.end());
	if(sz(v)>0&&v[0]-d[u]>k) return false;
	if(sz(v)>0&&v[0]-d[u]<k){
		p[u]=v[sz(v)-1];
	}else if(sz(v)>0){
		for(int i=1;i<sz(v);i++){
			if(v[i]-d[u]>=k) return false;
		}
		p[u]=v[0];
	}else p[u]=d[u];
	return true;
}
int main(){
	ios::sync_with_stdio(false);
	//freopen("in","r",stdin);
	cin>>t;
	while(t--){
		cin>>n;
		rep(i,1,n) g[i].clear();
		rep(i,2,n){
			int x,y;
			cin>>x>>y;
			g[x].pb(y);
			g[y].pb(x);
		}
		dfs(1,0);
		int l=1,r=n;
		while(l<=r){
			int mid=l+r>>1;
			k=mid;
			if(dfs1(1,0)) r=mid-1;
			else l=mid+1;
		}
		cout<<l<<endl;
	}
	return 0;
}