#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2e5 + 7, INF = 1e9 + 7;

vector <int> adj[MAXN];
int ans, n;

int dfs(int u, int p, int mid)
{
    if (adj[u].size() == 1 && p) return 0;

    vector <int> canjump, cantjump;
    for (auto v: adj[u])
        if (v != p)
        {
            int tmp = dfs(v, u, mid) + 1;
            if (tmp > mid) return mid + 1;
            else if (tmp + 1 <= mid) canjump.push_back(tmp);
            else cantjump.push_back(tmp);
        }

    if (cantjump.empty()) return *min_element(canjump.begin(), canjump.end());
    else if (cantjump.size() == 1) return cantjump[0];
    return mid + 1;
}

bool check(int mid)
{
    return dfs(1, 0, mid) <= mid;
}

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

    int l = 1, r = n - 1, ans;
    while (l <= r)
    {
        int mid = (l + r) >> 1;
        if (check(mid)) ans = mid, r = mid - 1;
        else l = mid + 1;
    }

    return ans;
}

int main()
{
    if (fopen("tst.inp", "r"))
    {
        freopen("tst.inp", "r", stdin);
        freopen("tst.out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int test;
    cin >> test;
    while (test--)
    {
        cin >> n;
        cout << solve() << '\n';
    }

    return 0;
}
