#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5 + 10;
int dp[N];
vector<int> g[N];
int ans = 0;
multiset<int, greater<int>> adj[N];
void clear(int n) {
  for (int i = 0; i <= n; ++i) {
    g[i].clear();
    adj[i].clear();
    dp[i] = 1;
    ans = 0;
  }
}
void add(int u, int p) {
  int cnt = 0;
  dp[u] += (int)adj[u].size();
  for (auto i : adj[u]) {
    if (cnt == 0) {
      dp[u] += i;
      --dp[u];
    } else if (cnt == 1 && p == -1) {
      dp[u] += i;
      --dp[u];
    } else if (cnt == 2)
      break;
    ++cnt;
  }
}
void remove(int u, int p) {
  int cnt = 0;
  dp[u] -= (int)adj[u].size();
  for (auto i : adj[u]) {
    if (cnt == 0) {
      dp[u] -= i;
      ++dp[u];
    } else if (cnt == 1 && p == -1) {
      dp[u] -= i;
      ++dp[u];
    } else if (cnt == 2)
      break;
    ++cnt;
  }
}
void dfs1(int u, int p) {
  dp[u] = 1;
  for (auto i : g[u]) {
    if (i == p) continue;
    dfs1(i, u);
    adj[u].insert(dp[i]);
  }
  add(u, p);
}
void dfs2(int u, int p) {
  ans = max(ans, dp[u]);
  for (auto to : g[u]) {
    if (to == p) continue;
    remove(u, -1);
    adj[u].erase(adj[u].find(dp[to]));
    add(u, to);
    remove(to, u);
    adj[to].insert(dp[u]);
    add(to, -1);
    dfs2(to, u);
    remove(to, -1);
    adj[to].erase(adj[to].find(dp[u]));
    add(to, u);
    remove(u, to);
    adj[u].insert(dp[to]);
    add(u, -1);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    clear(n);
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    dfs1(1, -1);
    dfs2(1, -1);
    cout << ans << "\n";
  }
}
