#include <bits/stdc++.h>
using namespace std;
const long long MOD = 16769023;
long long POW(long long a, long long b, long long MMM = MOD) {
  long long ret = 1;
  for (; b; b >>= 1, a = (a * a) % MMM)
    if (b & 1) ret = (ret * a) % MMM;
  return ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) {
  if (a == 0 || b == 0) return a + b;
  return a * (b / gcd(a, b));
}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int ddx[] = {2, 2, -2, -2, 1, 1, -1, -1}, ddy[] = {1, -1, 1, -1, 2, -2, 2, -2};
int ans;
vector<int> v[300001];
int d[300001];
void dfs(int N, int p) {
  d[N] = 1;
  vector<int> vv;
  for (int next : v[N]) {
    if (next == p) continue;
    dfs(next, N);
    d[N]++;
    vv.push_back(d[next]);
  }
  sort(vv.rbegin(), vv.rend());
  if (vv.size() > 1) ans = max(ans, vv[0] + vv[1] + d[N] - 1 - (N == 1));
  if (vv.size()) d[N] += vv[0] - 1;
  ans = max(ans, d[N]);
}
int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    ans = 0;
    int n;
    scanf("%d", &n);
    for (int(i) = (1); (i) <= (n); (i) += (1)) v[i].clear();
    for (int(i) = (1); (i) <= (n - 1); (i) += (1)) {
      int x, y;
      scanf("%d%d", &x, &y);
      v[x].push_back(y);
      v[y].push_back(x);
    }
    dfs(1, -1);
    printf("%d\n", ans);
  }
}
