#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> add_pair(pair<long long, long long> a,
                                    pair<long long, long long> b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
long long power(long long a, long long b) {
  long long res = 1;
  a = a % ((long long)1e9 + 7);
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % ((long long)1e9 + 7);
      b--;
    }
    a = (a * a) % ((long long)1e9 + 7);
    b >>= 1;
  }
  return res;
}
long long fermat_inv(long long y) { return power(y, ((long long)1e9 + 7) - 2); }
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long mul(long long a, long long b) {
  return (a * (b % ((long long)1e9 + 7))) % ((long long)1e9 + 7);
}
long long sub(long long a, long long b) {
  return ((a - b) % ((long long)1e9 + 7) + ((long long)1e9 + 7)) %
         ((long long)1e9 + 7);
}
long long add(long long a, long long b) {
  return ((a + b) % ((long long)1e9 + 7) + ((long long)1e9 + 7)) %
         ((long long)1e9 + 7);
}
vector<vector<long long> > G;
vector<long long> dp;
long long ans = 1;
void dfs(long long rt, long long par) {
  dp[rt] = 0;
  long long cnt = 0;
  vector<long long> aux;
  for (auto it = (G[rt]).begin(); it != (G[rt]).end(); it++) {
    if (*it != par) {
      dfs(*it, rt);
      cnt++;
      aux.push_back(dp[*it]);
      dp[rt] = max(dp[rt], dp[*it]);
    }
  }
  dp[rt]++;
  if (cnt > 1) {
    dp[rt] += cnt - 1;
    sort(aux.begin(), aux.end(), greater<long long>());
    ans = max(ans, aux[0] + aux[1] + 1 + (cnt - 2) + (par != rt));
  } else {
    ans = max(ans, dp[rt]);
  }
}
void solve() {
  long long n, i, u, v;
  cin >> n;
  G = vector<vector<long long> >(n + 1, vector<long long>());
  dp = vector<long long>(n + 1);
  for (i = (0); i < (n - 1); i += (1)) {
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  if (n == 2) {
    cout << 2 << '\n';
    return;
  }
  for (i = (1); i < (n + 1); i += (1))
    if ((G[i]).size() > 1) break;
  ans = 1;
  dfs(i, i);
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
