#include <bits/stdc++.h>
using namespace std;
const long long maxn = 300100, maxv = 100, mod = 1e9 + 7, maxa = 1005,
                maxs = 820, maxb = 20, base = 737, base2 = 3079,
                mod3 = 998244353, delt = 10513;
const long long inf = 2e16;
const long long infint = 1e9 + 11;
long long max(long long x, long long y) { return (x > y ? x : y); }
long long min(long long x, long long y) { return (x < y ? x : y); }
vector<long long> v[maxn];
long long dp[maxn][2];
bool visited[maxn];
long long ans = 0;
void dfs(long long x) {
  visited[x] = 1;
  long long kid = v[x].size();
  vector<long long> tmp;
  if (x != 0) {
    kid--;
  }
  for (auto u : v[x]) {
    if (!visited[u]) {
      dfs(u);
      ans = max(ans, dp[u][1] + 1);
      dp[x][0] = max(dp[x][0], dp[u][0] + kid);
      tmp.push_back(dp[u][0]);
      sort(tmp.begin(), tmp.end());
      reverse(tmp.begin(), tmp.end());
      if (tmp.size() > 2) {
        tmp.pop_back();
      }
    }
  }
  dp[x][1] = dp[x][0];
  if (tmp.size() == 2) {
    dp[x][1] = max(dp[x][1], tmp[0] + tmp[1] + kid - 1);
  }
  ans = max(ans, dp[x][1]);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long q;
  cin >> q;
  while (q--) {
    ans = 1;
    long long n;
    cin >> n;
    for (long long i = 0; i < n; i++) {
      visited[i] = 0;
      v[i].clear();
      dp[i][0] = 1;
      dp[i][1] = 1;
    }
    for (long long i = 0; i < n - 1; i++) {
      long long a, b;
      cin >> a >> b;
      a--;
      b--;
      v[a].push_back(b);
      v[b].push_back(a);
    }
    dfs(0);
    cout << ans << endl;
  }
  return 0;
}
