#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
const int mod = 1e9 + 7;
int add(int a, int b) { return a + b < mod ? a + b : a + b - mod; }
int sub(int a, int b) { return a < b ? a - b + mod : a - b; }
int mul(int a, int b) { return 1LL * a * b % mod; }
int fpow(int x, int n) {
  int res = 1;
  while (n) {
    if (n & 1) res = mul(res, x);
    x = mul(x, x);
    n >>= 1;
  }
  return res;
}
int n, m;
int dp[maxn], val[maxn];
vector<int> sval[maxn];
vector<int> g[maxn];
void dfs(int o, int p) {
  dp[o] = 1;
  val[o] = 1;
  for (auto u : g[o]) {
    if (u == p) continue;
    dfs(u, o);
    sval[o].push_back(val[u]);
  }
  sort(sval[o].begin(), sval[o].end(), greater<int>());
  if ((int)sval[o].size() >= 2)
    dp[o] += sval[o][0] + sval[o][1] + (int)sval[o].size() - 2 + (p != 0),
        val[o] += sval[o][0] + (int)sval[o].size() - 1;
  else if (sval[o].size() == 1)
    dp[o] += sval[o][0], val[o] += sval[o][0];
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; ++i) g[i].clear(), sval[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);
  }
  dfs(1, 0);
  cout << *max_element(dp + 1, dp + 1 + n) << endl;
}
int main() {
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) solve();
  return 0;
}
