#include <bits/stdc++.h>

using namespace std;

const int N = 1000 * 1000 + 10;
long long int a[N], mm[N], visited[N], anss[N], ans;
int n;
vector <int> e[N];

void dfs(int v){
	visited[v] = 1;
	if (e[v].size() == 1 and v != 1){
		anss[v] = 1;
		return;
	}
	vector <int> vec;
	for (auto u : e[v]){
		if (visited[u] == 0){
			dfs(u);/*
			if (v == 6){
				cout << u << " kk ";
			}*/
			vec.push_back(anss[u]);
		}
	}
	sort(vec.begin(), vec.end());
	
	if (v != 1){
		if (vec.size() > 1){
			ans = max(ans, vec.back() + 1ll);
		}
		
	}
	else{
		
		if (vec.size() > 1){
			ans = max(ans, vec.back() + 0ll);
			ans = max(ans, vec[vec.size() - 2] + 1ll);
		}
	}
	anss[v] = vec[0] + 1;
	ans = max(ans, vec[0] + 0ll);/*
	if (ans == 4){
		cout << v << " " << vec.back() + 1 << "\n";
	}*/
	vec.clear();
	
}

int main(){
	int tt;
	cin >> tt;
	while (tt--){
		ans = 0;
		int n;
		cin >> n;
		for (int i = 0; i < n - 1; i++){
			int x, y;
			cin >> x >> y;
			e[x].push_back(y);
			e[y].push_back(x);
		}//vec.clear();
		dfs(1);
		/*
		for (int i = 1; i <= n; i++){
			cout << anss[i] << " ";
		}
		cout << "\n";*/
		cout << ans << "\n";
		for (int i = 1; i <= n; i++){
			e[i].clear();
			visited[i] = 0;
			anss[i] = 0;
		}

	}
}