#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
const int INF = 1e9;
int n;
int dp[N][2];
int ans = -INF;
vector<int> adj[N];
void DFS(int u, int par = -1) {
  dp[u][1] = 1;
  int child = 0;
  int idx[2];
  idx[0] = idx[1] = -1;
  for (int v : adj[u])
    if (v != par) {
      DFS(v, u);
      ++child;
      if (dp[v][0] >= 0) {
        if (idx[0] == -1)
          idx[0] = v;
        else if (idx[1] == -1) {
          idx[1] = v;
          if (dp[idx[0]][0] < dp[idx[1]][0]) swap(idx[0], idx[1]);
        } else {
          if (dp[v][0] > dp[idx[1]][0]) idx[1] = v;
          if (dp[idx[0]][0] < dp[idx[1]][0]) swap(idx[0], idx[1]);
        }
      }
    }
  if (child > 0) {
    dp[u][0] = max(dp[u][0], child + 1);
    if (idx[0] != -1) dp[u][0] = max(dp[u][0], dp[idx[0]][0] + child);
  }
  ans = max(ans, dp[u][0]);
  ans = max(ans, dp[u][1]);
  if (idx[0] != -1 && idx[1] != -1) {
    assert(child >= 2);
    ans = max(ans,
              dp[idx[0]][0] + dp[idx[1]][0] + child - 1 + (par == -1 ? 0 : 1));
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int TC;
  cin >> TC;
  while (TC--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) adj[i].clear();
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    for (int i = 1; i <= n; ++i)
      for (int j = 0; j < 2; ++j) dp[i][j] = -INF;
    ans = -INF;
    DFS(1);
    cout << ans << '\n';
  }
  return 0;
}
