#include <bits/stdc++.h>
using namespace std;
const long long maxn = 300050;
long long n, root, dp[maxn], ans;
vector<long long> adj[maxn];
void dfs(long long u, long long p = root) {
  vector<long long> tmp;
  for (auto v : adj[u]) {
    if (v != p) {
      dfs(v, u);
      tmp.push_back(dp[v]);
    }
  }
  long long deg = adj[u].size(), sz = tmp.size();
  sort((tmp).begin(), (tmp).end(), greater<long long>());
  if (sz > 1) ans = max(ans, tmp[0] + tmp[1] + deg - 1);
  if (sz) {
    ans = max(ans, tmp[0] + deg);
    dp[u] = (tmp[0] + deg - 1);
  } else
    dp[u] = 1;
}
void solve() {
  cin >> n;
  for (long long i = 1; i <= n; i++) adj[i].clear();
  for (long long i = 1, u, v; i < n; i++)
    cin >> u >> v, adj[u].push_back(v), adj[v].push_back(u);
  if (n <= 2) {
    cout << n << '\n';
    return;
  }
  for (long long i = 1; i <= n; i++)
    if ((long long)adj[i].size() > 1) {
      root = i;
      break;
    }
  ans = 0;
  dfs(root);
  cout << ans << '\n';
}
signed main() {
  long long q;
  cin >> q;
  while (q--) solve();
}
