/*
 * @Author: your name
 * @Date: 2021-04-20 23:10:04
 * @LastEditTime: 2021-04-20 23:19:16
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \code_formal\cf\CF_688_5.cpp
 */
#include <bits/stdc++.h>
#define pr printf
#define sc scanf
#define for0(i, n) for (i = 0; i < n; i++)
#define for1n(i, n) for (i = 1; i <= n; i++)
#define forab(i, a, b) for (i = a; i <= b; i++)
#define forba(i, a, b) for (i = b; i >= a; i--)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define int long long
#define endl '\n' 
#define vi vector<int>
#define vii vector<vector<int>>
#define vt3 vector<tuple<int,int,int>>
#define mem(ara, n) memset(ara, n, sizeof(ara))
#define memb(ara) memset(ara, false, sizeof(ara))
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define sz(x) x.size()
const int N = 2e5 + 100;
const int mod = 1e9 + 7;
namespace often
{
    inline void input(int &res)
    {
        char c = getchar();
        res = 0;
        int f = 1;
        while (!isdigit(c))
        {
            f ^= c == '-';
            c = getchar();
        }
        while (isdigit(c))
        {
            res = (res << 3) + (res << 1) + (c ^ 48);
            c = getchar();
        }
        res = f ? res : -res;
    }
    inline int qpow(int a, int b)
    {
        int ans = 1, base = a;
        while (b)
        {
            if (b & 1)
                ans = (ans * base % mod + mod) % mod;
            base = (base * base % mod + mod) % mod;
            b >>= 1;
        }
        return ans;
    }
    int fact(int n)
    {
        int res = 1;
        for (int i = 1; i <= n; i++)
            res = res * 1ll * i % mod;
        return res;
    }
    int C(int n, int k)
    {
        return fact(n) * 1ll * qpow(fact(k), mod - 2) % mod * 1ll * qpow(fact(n - k), mod - 2) % mod;
    }
    int exgcd(int a, int b, int &x, int &y)
    {
        if (b == 0)
        {
            x = 1, y = 0;
            return a;
        }
        int res = exgcd(b, a % b, x, y);
        int t = y;
       y = x - (a / b) * y;
        x = t;
        return res;
    }
    int invmod(int a, int mod)
    {
        int x, y;
        exgcd(a, mod, x, y);
        x %= mod;
        if (x < 0)
            x += mod;
        return x;
    }
}
using namespace often;
using namespace std;
 
int n,ans;
vi g[N];

int dfs(int cur,int p)
{
    if(sz(g[cur]) == 1 && cur != 1)
        return 1;
    vi v;
    for(auto x:g[cur])
    {
        if(x == p)
            continue;
        int r = dfs(x,cur);
        v.emplace_back(r);
    }
    sort(all(v));
    if(sz(v) > 1)
    {
        if(cur == 1)
            ans = max({ans,v.back(),v[sz(v)-2]+1});
        else 
            ans = max(ans,v.back()+1);
    }
    ans = max(ans,v[0]);
    return v[0]+1;
}

signed main()
{
    int _ = 1;
    input(_);
    while (_--)
    {
        input(n);
        for(int i = 1; i <= n; i++)
            g[i].clear();
        
        for(int i = 1; i < n; i++)
        {
            int u,v;
            input(u),input(v);
            g[u].emplace_back(v);
            g[v].emplace_back(u);
        }
        ans = 0;
        dfs(1,0);
        cout << ans << endl;
    }
 
    return 0;
}