/*
         Crimson coagulates
         Severed head bouquet
         Body incomplete
         Sealer of your fate

         Crimson calligraphy
         Written on the trees
         Creature from the grave
         Headless and hellbent for me!
*/
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define mod  1000000007
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)

vector<vector<int>> dp;
vector<int> ht,dpt;

bool ff;
void dfs1(int i,int j,int curr)
{
         dpt[i] = curr;
         if(dp[i].size()==1 && i != 0){ht[i] = curr; return;}
         ht[i] = mod;
         for(int k : dp[i])
         {
                  if(k != j)
                  {
                           dfs1(k,i,curr+1);
                           ht[i] = min(ht[i],ht[k]);
                  }
         }
}

void dfs(int i,int j,int curr)
{
         //cout<<i<<" "<<ht[i]<<endl;
         vector<int> a;
         for(int k : dp[i])
         {
                  if(k==j)continue;
                  dfs(k,i,curr);
                  a.push_back(ht[k]-dpt[i]);
         }

         int cp = 0;
         for(int k : a)
         {
                  if(k>curr)ff = false;
                  if(k==curr)cp++;
         }
         if(i!=0 && cp)ff = false;
         else if(i==0 && cp>1)ff =false;
}
signed main()
{
         speed;
         int t;
         cin>>t;
         while(t--)
         {
                  dp.clear();
                  ht.clear();
                  dpt.clear();
                  int n;
                  cin>>n;
                  dp.resize(n);
                  dpt.resize(n);
                  ht.resize(n);
                  for(int i = 0 ; i < n-1; i++){
                           int x,y;
                           cin>>x>>y;
                           --x;--y;
                           dp[x].push_back(y);
                           dp[y].push_back(x);
                  }
                  dfs1(0,0,0);
                  int lo = 1;
                  int hi = 2*n;
         //         dfs(0,0,1);
                  //return 0;
                  while(lo < hi)
                  {
                           ff = true;
                           int mid = (lo+hi)/2;
                           dfs(0,0,mid);
                           if(ff){
                                    hi = mid-1;
                                    dfs(0,0,mid-1);
                                    if(!ff){
                                             lo = mid;
                                             break;
                                    }
                           }
                           else{
                                    lo = mid+1;
                           }
                  }
                  cout<<lo<<endl;
         }
}
