#include<bits/stdc++.h>
#define fi first
#define se second
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define per(i,a,b) for(int i = (b) - 1; i >= (a); --i)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
const int maxn = 2e6 + 10;
const int mod = 1e9 + 7;
vi edge[maxn];
int mx;
int dfs(int x,int fa,int deep){
	int son = 0,mnn = mod;
	vi leaf;
	leaf.clear();
	for(auto it:edge[x]){
		if(it != fa){
			leaf.pb(dfs(it,x,deep+1));
		}
	}
	if(sz(leaf)){
		sort(all(leaf));
		int len = sz(leaf);
		if(sz(leaf) == 1){
			mx = max(mx,leaf[0] - deep);
		}
		else{
			if(x == 1)
				mx = max(mx,max(leaf[len-1],leaf[len-2]+1) - deep);
			else
				mx = max(mx,leaf[len-1]+1 - deep);
		}
	//	cout << mx << ' ';
		return leaf[0];
	}
	else
		return deep;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--){
		int n;
		cin >> n;
		rep(i,1,n+1)edge[i].clear();
		rep(i,1,n){
			int u,v;
			cin >> u >> v;
			edge[u].pb(v);
			edge[v].pb(u);
		}
		mx = 0;
		dfs(1,0,1);
		cout << mx << '\n';
	}
	return 0;
}
