#include <bits/stdc++.h>
using namespace std;
const int N = 300001;
vector<int> g[N];
int dp[N][2];
void dfs(int s, int e) {
  vector<int> d;
  dp[s][0] = 0, dp[s][1] = g[s].size() - 1;
  for (int u : g[s])
    if (u != e) {
      dfs(u, s);
      dp[s][0] = max(dp[s][0], dp[u][0]);
      if (g[u].size() > 1) {
        d.push_back(dp[u][1]);
        dp[s][1] = max(dp[s][1], (int)g[s].size() + dp[u][1] - 1);
      }
    }
  sort(d.begin(), d.end(), greater<int>());
  int k = g[s].size() + 1;
  for (int i = 0; i < 2; i++)
    if (i < d.size()) k += d[i];
  dp[s][0] = max(dp[s][0], k);
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    if (n == 2) {
      cin >> n >> n, cout << "2\n";
      continue;
    }
    for (int u = 1; u <= n; u++) g[u].clear();
    for (int i = 0; i < n - 1; i++) {
      int u, v;
      cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
    }
    int u = g[1].size() == 1 ? g[1][0] : 1;
    dfs(u, 0);
    cout << dp[u][0] << "\n";
  }
}
