#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct IoSetup {
    IoSetup() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
        cerr << fixed << setprecision(10);
    }
} iosetup;
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
    os << p.first << " " << p.second;
    return os;
}
template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
    is >> p.first >> p.second;
    return is;
}
template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
    for(int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 != (int)v.size() ? " " : "");
    }
    return os;
}
template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
    for(T &in : v) is >> in;
    return is;
}

const int mxn = 200200;
vector<int> adj[mxn];
vector<int> dp;
int n, ans;

void dfs(int cur, int prev = -1) {
    if(adj[cur].size() == 1 && cur != 0) {
        dp[cur] = 1;
        return;
    }
    vector<int> v;
    for(int to : adj[cur]) {
        if(to == prev) continue;
        dfs(to, cur);
        v.push_back(dp[to]);
    }
    sort(v.begin(), v.end());
    if((int)v.size() > 1) {
      if (cur == 0) { // root is a bit different
            ans = max({ans, v.back(), v[v.size() - 2] + 1});
        } else {
            ans = max(ans, (int)v.back() + 1);
        }
    }
    ans = max(ans, v[0]);
    dp[cur] = v[0] + 1;
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        cin >> n;
        for (int i = 0; i < n; ++i) {
            adj[i].clear();
        }
        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;
        dp.resize(n, 0);
        dfs(0);
        cout << ans << endl;
    }
}
