#include <bits/stdc++.h>
using namespace std;
int T;
int n, u, v, dp[300010], ans[300010];
vector<int> G[300010];
void dfs(int u, int f) {
  int v, m1 = 0, m2 = 0, son = G[u].size() - (u != f);
  for (int i = 0; i < G[u].size(); i++) {
    v = G[u][i];
    if (v == f) continue;
    dfs(v, u);
    if (dp[v] >= m1) {
      m2 = m1;
      m1 = dp[v];
    } else if (dp[v] > m2)
      m2 = dp[v];
  }
  if (son != 0)
    dp[u] = m1 + son;
  else
    dp[u] = 1;
  if (son <= 1)
    ans[u] = m1 + 1 + (u != f);
  else
    ans[u] = m1 + m2 + son - 1 + (u != f);
}
int main() {
  cin >> T;
  while (T--) {
    cin >> n;
    for (int i = 1; i <= n; i++) ans[i] = dp[i] = 0, G[i].clear();
    for (int i = 1; i < n; i++) {
      cin >> u >> v;
      G[u].push_back(v);
      G[v].push_back(u);
    }
    dfs(1, 1);
    ans[0] = 0;
    for (int i = 1; i <= n; i++) ans[0] = max(ans[0], ans[i]);
    cout << ans[0] << endl;
  }
  return 0;
}
