#include <bits/stdc++.h>
using namespace std;
vector<long long> g[300100];
long long dp[300100];
long long cnt[300100];
void relax(long long x, long long &ma1, long long &ma2) {
  if (x > ma1) {
    ma2 = ma1;
    ma1 = x;
  } else if (x > ma2)
    ma2 = x;
}
long long ans = 0;
long long dfs(long long v, long long pr = -1) {
  cnt[v] = 1;
  long long ma1 = 0;
  long long ma2 = 0;
  for (auto to : g[v]) {
    if (to != pr) {
      long long x = dfs(to, v);
      ++cnt[v];
      relax(x, ma1, ma2);
    }
  }
  dp[v] = cnt[v] + max(ma1 - 1, 0ll) + max(ma2 - 1, 0ll);
  ans = max(ans, dp[v] + (pr != -1));
  return max(ma1 - 1, 0ll) + cnt[v];
}
void solve() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    dp[i] = 0, g[i].clear();
    cnt[i] = 0;
  }
  ans = 0;
  for (long long i = 1; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(1);
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long q;
  cin >> q;
  for (long long i = 1; i <= q; i++) solve();
  return 0;
}
