#include <bits/stdc++.h>
using namespace std;
int t, n;
vector<int> G[300002];
int dp[300002][2];
int dfs(int x, int par) {
  int mx1 = 1, mx2 = 1, grad = 0, ans = 0;
  for (auto it : G[x]) {
    if (par == it) continue;
    ans = max(ans, dfs(it, x));
    grad++;
    if (dp[it][0] > mx1) {
      mx2 = mx1;
      mx1 = dp[it][0];
    } else if (dp[it][0] > mx2) {
      mx2 = dp[it][0];
    }
  }
  dp[x][0] = mx1 + 1 + (grad - 1);
  dp[x][1] = 1 + mx1 + mx2 + (grad - 2);
  ans = max(ans, dp[x][1] + (par > 0));
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      G[i].clear();
      dp[i][0] = dp[i][1] = 1;
    }
    int x, y;
    for (int i = 1; i < n; i++) {
      cin >> x >> y;
      G[x].push_back(y);
      G[y].push_back(x);
    }
    cout << dfs(1, 0) << "\n";
  }
  return 0;
}
