#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5;
int TC, N;
vector<int> adj[MAXN + 10];
int dp[MAXN + 10], ans = 0;
void dfs(int now, int bef) {
  int a = 0, b = 0;
  int chd = 0;
  for (int nxt : adj[now]) {
    if (nxt == bef) continue;
    chd++;
    dfs(nxt, now);
    dp[now] = max(dp[nxt], dp[now]);
    if (a <= dp[nxt])
      b = a, a = dp[nxt];
    else if (b <= dp[nxt])
      b = dp[nxt];
  }
  ans = max(ans, 1 + (now != 1) + max(chd - 2, 0) + a + b);
  dp[now] += max(0, chd - 1) + 1;
}
int main() {
  int i, j;
  scanf("%d", &TC);
  while (TC--) {
    scanf("%d", &N);
    for (i = 1; i <= N; i++) adj[i].clear(), dp[i] = 0;
    for (i = 1; i < N; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    ans = 0;
    dfs(1, 1);
    printf("%d\n", ans);
  }
}
