#include <bits/stdc++.h>
using namespace std;
mt19937 bruh(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rofl(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e6 + 2;
const int M = 1e5;
const int mod = 0;
const int inf = 2e9 + 3;
const long long INF = 1e18;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
void files() {
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
}
int T, n, dp[3][N], ans;
vector<int> g[N];
void prec(int v = 1, int p = 0) {
  int mx2 = 0, mx1 = 0, mx0 = 0;
  for (auto to : g[v]) {
    if (to == p) continue;
    prec(to, v);
    if (mx1 <= dp[1][to]) {
      mx2 = mx1;
      mx1 = dp[1][to];
    } else if (mx2 <= dp[1][to]) {
      mx2 = dp[1][to];
    }
  }
  dp[1][v] = max(1, 1 + mx1 + (int)g[v].size() - 1 - (v != 1));
  dp[2][v] = max(1, 1 + mx1 + mx2 + (int)g[v].size() - 2 - (v != 1));
}
void dfs(int v = 1, int p = 0) {
  set<pair<int, int> > mx1;
  mx1.insert({0, -2});
  mx1.insert({0, -1});
  for (auto to : g[v]) mx1.insert({-dp[1][to], to});
  int dp1v = dp[1][v], dp2v = dp[2][v];
  dp[1][v] = max(1, 1 - mx1.begin()->first + (int)g[v].size() - 1);
  dp[2][v] = max(1, 1 - mx1.begin()->first - next(mx1.begin(), 1)->first +
                        (int)g[v].size() - 2);
  ans = max(ans, max(dp[1][v], dp[2][v]));
  for (auto to : g[v]) {
    if (to == p) continue;
    mx1.erase({-dp[1][to], to});
    dp[1][v] = max(1, 1 - mx1.begin()->first + (int)g[v].size() - 2);
    dp[2][v] = max(1, 1 - mx1.begin()->first - next(mx1.begin(), 1)->first +
                          (int)g[v].size() - 3);
    dfs(to, v);
    mx1.insert({-dp[1][to], to});
  }
  dp[1][v] = dp1v;
  dp[2][v] = dp2v;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) g[i].clear();
    for (int i = 1, v, u; i < n; ++i) {
      scanf("%d %d", &v, &u);
      g[v].push_back(u);
      g[u].push_back(v);
    }
    ans = 0;
    prec();
    dfs();
    printf("%d\n", ans);
  }
}
