#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back
#define debug(x)   printf("x=%d\n",x);
#define ls(x) arr[x].child[0]
#define rs(x) arr[x].child[1]
using namespace std;

const int N=1e6+10;
const ll mod=9797;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}


int n,m,k;
ll ar[N],br[N];
int ans;

int dp[N];
vector<int>ed[N];


void dfs(int u,int fa)
{
    vector<int>vt;
    bool fg=1;
    for(int v:ed[u])
    {
        if(v==fa) continue;
        fg=0;
        dfs(v,u);
        dp[u]=min(dp[u],dp[v]+1);
        vt.pb(dp[v]);
    }
    if(fg) dp[u]=0;
    if(vt.size())
    {
        sort(vt.begin(),vt.end());
        for(int i=0;i<vt.size()-1;i++)
        {
         //   printf("vt=%d  ",vt[i]);
            ans=max(ans,vt[i]+2);
        }
        if(u==1) ans=max(ans,vt.back()+1);
        else ans=max(ans,vt.back()+2);
    }




}

void solvve()
{
    n=read(); ans=0;
    rep(i,1,n) ed[i].clear(),dp[i]=1e9;
    rep(i,1,n-1)
    {
        int u=read(),v=read();
        ed[u].pb(v); ed[v].pb(u);
    }
    dfs(1,0);
  //  rep(i,1,n) printf("%d ",dp[i]); puts("");
    cout<<ans<<endl;



}
int main()
{
    // gbtb;
  //   freopen("input.txt","r",stdin);

      //  while(scanf("%lld",&n)!=EOF)

       int T;T=read();while(T--)
    {
        //printf("cas=%d   ",++cc);
        solvve();
    }
    return 0;

}
