#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 50;
vector<int> e[N];
int dp[N], res, deg[N];
void dfs(int u, int pre) {
  dp[u] = deg[u];
  for (int v : e[u]) {
    if (v == pre) continue;
    dfs(v, u);
    res = max(res, dp[u] + dp[v]);
    dp[u] = max(dp[u], dp[v] + deg[u] - 2 + 1);
  }
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) e[i].clear(), deg[i] = 0;
    for (int i = 0; i < n - 1; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      e[u].push_back(v);
      e[v].push_back(u);
      deg[u]++;
      deg[v]++;
    }
    res = 0;
    dfs(1, 0);
    printf("%d\n", res);
  }
}
