#include <bits/stdc++.h>
using namespace std;
template <typename T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using MaxHeap = priority_queue<T>;
const long double PI = 3.14159265358979323846;
const long double E = 2.718281828459;
long long MOD = 1e9 + 7;
long long M(long long n) { return (n % MOD + MOD) % MOD; }
long long pow2(long long n) { return 1ll << n; }
mt19937 rng(
    (unsigned int)chrono::steady_clock::now().time_since_epoch().count());
vector<vector<long long>> G;
vector<long long> dp;
long long ans;
void dfs(long long u, long long par) {
  long long child = G[u].size() - 1;
  if (par == 0) child++;
  dp[u] = child + 1;
  for (auto v : G[u])
    if (v != par) {
      dfs(v, u);
      long long best = dp[u] + dp[v];
      if (!par) best--;
      ;
      ;
      ans = max(ans, best);
      dp[u] = max(dp[u], dp[v] + child);
    }
}
int main(int argn, char **argv) {
  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);
  int T = 1;
  cin >> T;
  for (int(I) = (0); (I) <= ((T)-1); (I)++) {
    long long n;
    cin >> n;
    G = vector<vector<long long>>(n + 2), dp = vector<long long>(n + 2, 1),
    ans = 0;
    for (int(i) = (0); (i) <= ((n - 1) - 1); (i)++) {
      long long u, v;
      cin >> u >> v;
      G[u].emplace_back(v), G[v].emplace_back(u);
    }
    dfs(1, 0);
    ;
    ;
    cout << ans << endl;
  }
  return 0;
}
