#include <bits/stdc++.h>
using namespace std;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long NLINF = 0xf7f7f7f7f7f7f7f7;
const int INF = 0x3f3f3f3f, NINF = 0xf7f7f7f7;
const int MOD1 = 1e9 + 7, MOD2 = 1e9 + 9;
const int N = 3e5 + 10;
int n, q, ans, dp[N];
vector<int> adj[N];
void dfs(int v = 1, int p = -1) {
  dp[v] = 1;
  int k = max(0, (int)adj[v].size() - 2);
  for (auto &u : adj[v]) {
    if (u == p) continue;
    dfs(u, v), dp[v] = max(dp[v], dp[u] + k + 1);
  }
}
void solve(int v = 1, int p = -1) {
  vector<int> cur;
  int k = max(0, (int)adj[v].size() - 2), val = 0;
  for (auto &u : adj[v])
    if (u != p) cur.emplace_back(dp[u]);
  sort(cur.begin(), cur.end());
  for (int j = 0; j < 2; j++)
    if (!cur.empty()) val += cur[(int)cur.size() - 1], cur.pop_back();
  ;
  ans = max(ans, 1 + k + val);
  ;
  for (auto &u : adj[v])
    if (u != p) solve(u, v);
}
int main() {
  for (scanf("%d", &q); q--;) {
    scanf("%d", &n), ans = 0;
    for (int i = 1; i <= n; i++) adj[i].clear(), dp[i] = 0;
    for (int i = 1, u, v; i < n; i++)
      scanf("%d%d", &u, &v), adj[u].emplace_back(v), adj[v].emplace_back(u);
    dfs(), solve();
    printf("%d\n", ans);
  }
}
