#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 20;
int h[N], deg[N], nxt[N], to[N], cnt;
inline void add(int u, int v) {
  to[++cnt] = v;
  nxt[cnt] = h[u];
  h[u] = cnt;
}
int tot[N], Ans, dp[N], root;
void dfs(int u, int f) {
  int mx = 0, mx2 = 0;
  tot[u] = 0;
  for (int i = h[u]; i; i = nxt[i]) {
    if (to[i] == f) continue;
    dfs(to[i], u);
    ++tot[u];
    mx2 = max(mx2, dp[to[i]]);
    if (mx < mx2) swap(mx, mx2);
  }
  Ans = max(Ans, mx + mx2 + tot[u] + 1 + (u != root));
  dp[u] = tot[u] + mx;
}
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    cnt = 0;
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) h[i] = deg[i] = 0;
    Ans = 0;
    for (int u, v, i = 1; i < n; i++) {
      scanf("%d%d", &u, &v);
      add(u, v);
      add(v, u);
      ++deg[u], ++deg[v];
    }
    if (n <= 6) {
      printf("%d\n", n);
      continue;
    }
    for (int i = 1; i <= n; i++)
      if (deg[i] > 1) root = i;
    dfs(root, 0);
    printf("%d\n", Ans);
  }
}
