#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
vector<long long> a[300005];
long long vis[300005];
void clear_me(long long n) {
  for (long long i = 0; i <= n; i++) {
    vis[i] = 0;
    a[i].clear();
  }
  ans = 0;
}
long long dfs(long long n) {
  vis[n] = 1;
  long long temp = 1;
  vector<long long> arr;
  for (long long i = 0; i < a[n].size(); i++) {
    if (vis[a[n][i]] == 0) {
      long long temp = dfs(a[n][i]);
      arr.push_back(temp);
    }
  }
  sort(arr.begin(), arr.end());
  long long p = arr.size();
  if (p == 0) {
    ans = max(ans, 1ll);
    return 1;
  } else if (p == 1) {
    ans = max(ans, arr[0] + 1);
    return arr[0] + p;
  } else {
    ans = max(ans, arr[p - 1] + arr[p - 2] + (long long)a[n].size() - 1);
    return arr[p - 1] + p - 1 + 1;
  }
  return 0;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    for (long long i = 0; i < n - 1; i++) {
      long long u, v;
      cin >> u >> v;
      a[u].push_back(v);
      a[v].push_back(u);
    }
    dfs(1);
    cout << ans << '\n';
    clear_me(n);
  }
  return 0;
}
