#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const int inf = INT_MAX;
const long long inff = 1e18;
const long long mod = 1e9 + 7;
int q, n, x, y;
vector<int> G[maxn];
int indeg[maxn], dp[maxn], ans = -1;
void dfs(int now, int p) {
  int maxx = -inf, maxx2 = -inf;
  for (int i : G[now]) {
    if (i != p) dfs(i, now);
    if (dp[i] >= maxx)
      maxx2 = maxx, maxx = dp[i];
    else if (dp[i] >= maxx2)
      maxx2 = dp[i];
  }
  if (maxx != -inf) ans = max(ans, maxx + indeg[now]);
  if (maxx2 != -inf) ans = max(ans, maxx + maxx2 + indeg[now] - 1);
  dp[now] = max(1, maxx + indeg[now] - 1);
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> q;
  while (q--) {
    cin >> n;
    ans = -1;
    for (int i = 1; i <= n; i++) {
      G[i].clear();
      indeg[i] = 0;
      dp[i] = 0;
    }
    for (int i = 1; i <= n - 1; i++) {
      cin >> x >> y;
      G[x].push_back(y);
      G[y].push_back(x);
      indeg[x]++, indeg[y]++;
    }
    if (n == 2) {
      cout << 2 << '\n';
      continue;
    }
    int root;
    for (int i = 1; i <= n; i++)
      if (indeg[i] > 1) root = i;
    dfs(root, -1);
    cout << ans << '\n';
  }
}
