#include <bits/stdc++.h>
using namespace std;
vector<int> g[300050];
int dp[300050], res;
void dfs(int u, int p) {
  int val = 0;
  dp[u] = 0;
  for (auto v : g[u]) {
    if (v == p) continue;
    dfs(v, u);
    val = max(val, dp[u] + dp[v]);
    dp[u] = max(dp[u], dp[v]);
  }
  val += g[u].size() + 1;
  res = max(res, val);
  dp[u] += g[u].size() - (u != 1);
}
int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) g[i].clear();
    for (int i = 1; i < n; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      g[u].push_back(v);
      g[v].push_back(u);
    }
    res = 0;
    dfs(1, 0);
    printf("%d\n", res);
  }
  return 0;
}
