#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int nax = 2e5+5;
const int INF = 1e9;

vector<vector<int>> adj(nax);

pair<int, int> dfs(int v, int p) {      // dp, shallowest node
    pair<int, int> ret = {0, INF};
    int count = 0;
    for (auto u : adj[v]) {
        if (u != p) {
            count++;
        }
    }
    if (!count) {
        return { 0, 0 };
    }
    int add = (count == 1 ? 0 : 1);
    int maxshallow = 0, maxshallow2 = 0;
    for (auto u : adj[v]) {
        if (u != p) {
            auto temp = dfs(u, v);
            ret.first = max(ret.first, temp.first);
            if (maxshallow < 1 + temp.second) {
                maxshallow2 = maxshallow;
                maxshallow = 1 + temp.second;
            }
            else if (maxshallow2 < 1 + temp.second) {
                maxshallow2 = 1 + temp.second;
            }
            maxshallow = max(maxshallow, temp.second + 1);
            ret.second = min(ret.second, 1 + temp.second);
        }
    }
    //cout << v << ' ' << maxshallow << ' ' << maxshallow2 << ' ' << ret.second << '\n';
    if(v == 1) ret.first = max(ret.first, max(maxshallow2 + add, maxshallow));
    else ret.first = max(ret.first, maxshallow + add);
    //cout << v << ' ' << ret.first << ' ' << ret.second << '\n';
    return ret;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t, n, x, y;
    int origt, count = 0;
    cin >> t;
    origt = 10000000;
    while (t--) {
        cin >> n;
        count++;
        for (int i = 1; i <= n; i++) {
            adj[i].clear();
        }
        for (int i = 0; i < n - 1; i++) {
            cin >> x >> y;
            if (origt == 10000 && count == 157) {
                cout << x << ' ' << y << '\n';
            }
            adj[x].push_back(y);
            adj[y].push_back(x);
        }
        if(origt != 10000)cout << dfs(1, -1).first << '\n';
    }
    return 0;
}