#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <string>
#include <cstring>
#include <climits>
using namespace std;

typedef long long      ll;
typedef double         db;
typedef pair<int, int> pii;
typedef map<int, int>  mii;
typedef set<int>       si;
typedef vector<int>    vi;
typedef vector<vi>     vvi;
typedef vector<ll>     vl;
typedef vector<db>     vd;
typedef vector<pii>    vp;

void fastIO () {
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
}
void usaco (string file) {
	freopen((file + ".in").c_str(), "r", stdin);
	freopen((file + ".out").c_str(), "w", stdout);
}

const ll MOD1 = 1000000007, MOD2 = 998244353, MOD3 = 999999937, MOD4 = 9973;
ll MOD = MOD1;
ll modExp (ll a, ll n, ll m) {
	ll res = 1;
	while (n) {
		if (n & 1) res = res * a % m;
		a = a * a % m;
		n >>= 1;
	}
	return res;
}
ll flt (ll a, ll p) { return modExp(a, p - 2, p); }
ll ma (ll x, ll y) { return (x + y) % MOD; }
ll ms (ll x, ll y) { return ma(ma(x, -y), MOD); }
ll mm (ll x, ll y) { return (x * y) % MOD; }
ll md (ll x, ll y) { return mm(x, flt(y, MOD)); }
ll me (ll x, ll y) { return modExp(x, y, MOD); }
ll mm2 (ll x, ll y) {
	ll res = 1;
	for (ll i = x; i <= y; i++) res = mm(res, i);
	return res;
}
ll mf (ll x) { return mm2(1, x); }
ll mp (ll x, ll y) { return y <= x ? mm2(x - y + 1, x) : 0; }
ll mc (ll x, ll y) { return y <= x ? md(mp(x, y), mf(y)) : 0; }

int ans;
vvi adj;
int dfs (int u, int p) {
	if (adj[u].size() == 1 && u != 0) return 1;
	vi c;
	for (int v : adj[u]) {
		if (v != p) c.push_back(dfs(v, u));
	}
	sort(c.begin(), c.end());
	int s = c.size();
	if (s > 1) {
		if (u == 0) ans = max({c[s - 1], c[s - 2] + 1, ans});
		else ans = max(c[s - 1] + 1, ans);
	} else ans = max(c[0], ans);
	return c[0] + 1;
}

void solve () {
	int n;
	cin >> n;
	adj = vvi(n);
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	ans = 0;
	dfs(0, -1);

	cout << ans << '\n';
}

int main () {
	fastIO();

	int t;
	cin >> t;
	while (t--) solve();
}