#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
using namespace std;
const int N = 3e5 + 10;
vector<int> g[N];
int n, x, y, q, dp[N][2], ans;
inline void dfs(int u, int o) {
  int mx1 = 0, c = 0;
  dp[u][0] = 1;
  for (int v : g[u]) {
    if (v == o) continue;
    dfs(v, u);
    if (dp[v][0] >= mx1) {
      mx1 = dp[v][0];
    }
    ++c;
  }
  if (c == 0)
    ;
  else
    dp[u][0] += mx1 + c - 1;
}
inline void DFS(int u, int o) {
  int mx1 = 0, mx2 = 0, c = 0;
  int v1 = -1, v2 = -1;
  if (u != 1) {
    if (dp[u][1] >= mx1) {
      mx2 = mx1;
      v2 = v1;
      mx1 = dp[u][1];
      v1 = -1;
    } else if (dp[u][1] >= mx2) {
      mx2 = dp[u][1];
      v2 = -1;
    }
    ++c;
  }
  for (int v : g[u]) {
    if (v == o) continue;
    if (dp[v][0] >= mx1) {
      mx2 = mx1;
      v2 = v1;
      mx1 = dp[v][0];
      v1 = v;
    } else if (dp[v][0] >= mx2) {
      mx2 = dp[v][0];
      v2 = v;
    }
    ++c;
  }
  for (int v : g[u]) {
    if (v == o) continue;
    if (v == v1) {
      if (c - 1 == 0)
        dp[v][1] = 1;
      else
        dp[v][1] = mx2 + c - 1 - 1 + 1;
    } else {
      if (c - 1 == 0)
        dp[v][1] = 1;
      else
        dp[v][1] = mx1 + c - 1 - 1 + 1;
    }
    DFS(v, u);
  }
}
inline void solve(int u, int o) {
  int mx1 = 0, mx2 = 0, c = 0;
  for (int v : g[u]) {
    if (v == o) continue;
    solve(v, u);
    if (dp[v][0] >= mx1) {
      mx2 = mx1;
      mx1 = dp[v][0];
    } else if (dp[v][0] >= mx2) {
      mx2 = dp[v][0];
    }
    ++c;
  }
  if (u != 1) {
    if (dp[u][1] >= mx1) {
      mx2 = mx1;
      mx1 = dp[u][1];
    } else if (dp[u][1] >= mx2) {
      mx2 = dp[u][1];
    }
    ++c;
  }
  if (c == 0)
    ans = max(ans, 1);
  else if (c == 1)
    ans = max(ans, mx1 + 1);
  else
    ans = max(ans, mx1 + mx2 + 1 + c - 2);
}
int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    for (int i = 1; i < n; ++i)
      scanf("%d %d", &x, &y), g[x].push_back(y), g[y].push_back(x);
    dfs(1, 0);
    DFS(1, 0);
    ans = 0;
    solve(1, 0);
    printf("%d\n", ans);
    for (int i = 1; i <= n; ++i) g[i].clear(), dp[i][0] = dp[i][1] = 0;
  }
  return 0;
}
