#include <bits/stdc++.h>
using namespace std;
int ans = 0;
vector<vector<int> > g;
vector<vector<pair<int, int> > > children;
vector<int> dp;
void dfs(int v, int pr = -1) {
  int res = 0;
  for (auto x : g[v]) {
    if (x == pr) continue;
    dfs(x, v);
    children[v].push_back({dp[x], x});
  }
  sort(children[v].begin(), children[v].end());
  if (children[v].size())
    dp[v] = children[v].back().first + (children[v].size() - 1) + 1;
  else
    dp[v] = 1;
  if (pr != -1) children[v].push_back({dp[pr], pr});
  sort(children[v].begin(), children[v].end());
}
void f(int v, int u) {
  if (g[v].size() == 1)
    dp[v] = 1;
  else if (g[v].size() == 2) {
    dp[v] = 1;
    for (int i = 0; i < 2; ++i)
      if (children[v][i].second != u) dp[v] += children[v][i].first;
  } else {
    dp[v] = 1;
    for (int i = children[v].size() - 1, cnt = 0; i >= 0 && cnt < 2; --i)
      if (children[v][i].second != u) {
        dp[v] += children[v][i].first;
        ++cnt;
      }
  }
  if (g[u].size() == 1)
    dp[u] = dp[v] + 1;
  else {
    dp[u] = (children[u].back()).first + (*(children[u].end() - 2)).first +
            (children[u].size() - 2) + 1;
  }
  ans = max(ans, dp[u]);
}
void big_dfs(int v, int pr = -1) {
  for (auto x : g[v]) {
    if (x == pr) continue;
    f(v, x);
    big_dfs(x, v);
    f(x, v);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    g.resize(n);
    dp.resize(n);
    children.resize(n);
    for (int i = 0; i < n - 1; ++i) {
      int v, u;
      cin >> v >> u;
      --v, --u;
      g[v].push_back(u);
      g[u].push_back(v);
    }
    int r;
    for (int i = 0; i < n; ++i)
      if (g[i].size() == 1) {
        r = i;
        break;
      }
    ans = 0;
    dfs(r);
    ans = dp[r];
    big_dfs(r);
    cout << ans << '\n';
    g.clear();
    dp.clear();
    children.clear();
  }
}
