#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>

#define maxn 200010

using namespace std;

int flag[maxn],n,ans,f[maxn];
vector<int> e[maxn],tans;

void dfs(int x)
{
    flag[x]=1;
    f[x]=n+1;
    for(auto y:e[x]){
        if(flag[y]) continue;
        dfs(y);
        f[x]=min(f[y]+1,f[x]);
        if(x!=1) ans=max(ans,f[y]+2);
        else tans.push_back(f[y]+1);
    }
    if(f[x]==n+1) f[x]=0;
    
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        tans.clear();
        ans=0;
        for(int i=1;i<=n;i++){
            e[i].clear();
            flag[i]=0;
        }
        for(int i=1;i<n;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            e[x].push_back(y);
            e[y].push_back(x);
        }
        dfs(1);
        sort(tans.begin(),tans.end());
        for(int i=0;i<tans.size()-1;i++){
            tans[i]+=1;
            ans=max(ans,tans[i]);
        }
        ans=max(ans,tans.back());
        printf("%d\n",ans);
    }
}