#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long MOD = 1000000007;
long double EPS = 1e-9;
long long binpow(long long b, long long p, long long mod) {
  long long ans = 1;
  b %= mod;
  for (; p; p >>= 1) {
    if (p & 1) ans = ans * b % mod;
    b = b * b % mod;
  }
  return ans;
}
void pre() {}
vector<vector<long long> > g;
long long dp[300300];
long long ans = 0;
void dfs(int nn, int pp) {
  dp[nn] = 1;
  vector<long long> ch;
  for (auto v : g[nn]) {
    if (v != pp) {
      dfs(v, nn);
      ch.emplace_back(v);
    }
  }
  long long maxi = 0;
  for (auto v : ch) {
    (maxi = max((maxi), (dp[v])));
  }
  if ((long long)(ch).size()) dp[nn] += maxi + (long long)(ch).size() - 1;
}
void dfs2(int nn, int pp, long long parentout) {
  set<pair<long long, long long> > pq;
  int cn = 0;
  for (auto v : g[nn]) {
    if (v != pp) {
      cn++;
      pq.insert(make_pair(dp[v], v));
      if (pq.size() > 4) {
        pq.erase(*pq.begin());
      }
    }
  }
  if (parentout != 0) {
    cn++;
    pq.insert(make_pair(parentout, pp));
    if (pq.size() > 4) {
      pq.erase(*pq.begin());
    }
  }
  for (auto v : g[nn]) {
    if (v != pp) {
      long long temp = cn;
      vector<pair<long long, long long> > xx((pq).begin(), (pq).end());
      reverse((xx).begin(), (xx).end());
      for (long long i = 0; i < (4); ++i) {
        if (i < (long long)(xx).size() && xx[i].second != v) {
          temp += xx[i].first - 1;
          break;
        }
      }
      dfs2(v, nn, temp);
    }
  }
  long long tt = 1 + max((long long)(g[nn]).size(), 0LL);
  vector<pair<long long, long long> > xx((pq).begin(), (pq).end());
  reverse((xx).begin(), (xx).end());
  for (long long i = 0; i < (2); ++i) {
    if (i < (long long)(xx).size()) {
      tt += xx[i].first - 1;
    }
  }
  (ans = max((ans), (tt)));
}
void solve() {
  long long n;
  cin >> n;
  g.resize(n + 1);
  for (long long i = 0; i < (n - 1); ++i) {
    long long a, b;
    cin >> a >> b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  ans = 0;
  dfs(1, 0);
  dfs2(1, 0, 0);
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  pre();
  long long _t = 1;
  cin >> _t;
  for (long long i = 1; i <= _t; i++) {
    solve();
    g.clear();
  }
}
