#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e18;
const long long mod = (long long)1e9 + 7;
const double eps = (double)1e-9;
const double pi = acos(-1.0);
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
const int N = 500500;
int dp[N], ans, n;
vector<int> g[N];
void dfs(int v, int pr = -1) {
  int mx1 = 0, mx2 = 0;
  int cnt = 0;
  for (int to : g[v]) {
    if (to == pr) continue;
    dfs(to, v);
    if (mx2 < dp[to]) mx2 = dp[to];
    if (mx1 < mx2) swap(mx1, mx2);
    cnt++;
  }
  if (cnt == 0)
    dp[v] = 1;
  else
    dp[v] = mx1 + cnt;
  if (cnt == 0)
    ans = max(ans, 1);
  else if (cnt == 1)
    ans = max(ans, mx1 + 1);
  else
    ans = max(ans, mx1 + mx2 + 1 + cnt - 2 + (pr != -1));
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; ++i) g[i].clear();
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  ans = 0;
  dfs(1);
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  while (T--) solve();
  return 0;
}
