#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
using namespace std;

#define int long long

const int MOD = 1e9 + 9;
const int maxn = 2e5 + 13;
const int INF = 1e18;

vector<int> gr[maxn];
int h[maxn];
int lwlst[maxn];
int up[20][maxn];
int res = 0;
int lca(int u, int v) {
	if (h[u] < h[v])swap(u, v);
	int d = h[u] - h[v];
	int lg = 19;
	while (d > 0) {
		if (d >= (1 << lg))
			d -= (1 << lg), u = up[lg][u];
		lg--;
	}
	if (u == v) return u;
	lg = 19;
	while (lg >= 0) {
		if (up[lg][u] != up[lg][v])
			u = up[lg][u], v = up[lg][v];
		lg--;
	}
	return up[0][u];
}

void dfs(int v, int p, int cnt = 0) {
	h[v] = cnt;
	up[0][v] = p;
	
	for (auto t : gr[v]) {
		if (t == p)continue;
		dfs(t, v, cnt + 1);
		if (h[lwlst[v]] > h[lwlst[t]])
			lwlst[v] = lwlst[t];
		
	}
	

	if (gr[v].size() == 1 && v != 1) {
		lwlst[v] = v;
	}
}
void dfs2(int v, int p) {
	vector<int> x;
	for (auto t : gr[v]) {
		if (t == p)continue;
		dfs2(t, v);
		x.push_back(lwlst[t]);
	}
	if (x.size() <= 1)return;
	sort(x.begin(), x.end(), [&](int u, int v) {return h[u] > h[v]; });
	res = max(res, h[x[0]] - h[v] + 1);
}

int dist(int u, int v) {
	int lc = lca(u, v);
	return h[u] + h[v] - 2 * h[lc];
}

void solve() {
	int n;
	cin >> n;
	res = 0;
	for (int i = 0; i <= n; ++i)gr[i].clear();
	fill(h, h + n + 10, 0);
	fill(lwlst, lwlst + n + 10, 0);
	for (int i = 0; i < 20; ++i)fill(up[i], up[i] + n + 10, 0);

	for (int i = 1; i <= n - 1; ++i) {
		int u, v;
		cin >> u >> v;
		gr[u].push_back(v);
		gr[v].push_back(u);
	}
	h[0] = INF;
	dfs(1, 1);

	for (int i = 1; i < 20; ++i) {
		for (int j = 1; j <= n; ++j) {
			up[i][j] = up[i - 1][up[i - 1][j]];
		}
	}

	sort(gr[1].begin(), gr[1].end(), [&](int u, int v) {return h[lwlst[u]] > h[lwlst[v]]; });
	res = h[lwlst[gr[1][0]]];
	if (gr[1].size() != 1)res = max(res, h[lwlst[gr[1][1]]] + 1);

	for (auto t : gr[1]) {
		dfs2(t, 1);
	}

	cout << res << "\n";
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int q;
	cin >> q;
	while (q--)solve();

	
	return 0;
}