#include <bits/stdc++.h>
using namespace std;
const int maxn = 310000;
vector<int> G[maxn];
int d[maxn], ans = 0;
int dp(int x, int fa) {
  vector<int> vec;
  for (auto y : G[x])
    if (y != fa) {
      vec.push_back(dp(y, x));
    }
  sort(vec.begin(), vec.end(), greater<int>());
  if (vec.empty()) {
    d[x] = 1;
  } else {
    d[x] = vec[0] + vec.size();
  }
  ans = max(ans, d[x]);
  if (vec.size() >= 2) {
    ans = max(ans, vec[0] + vec[1] + (int)vec.size() - 1 + (fa > 0));
  }
  return d[x];
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    ans = 0;
    memset(d, 0, sizeof(int) * (n + 10));
    for (int i = 0; i <= n; ++i) G[i].clear();
    for (int i = 0; i < n - 1; ++i) {
      int x, y;
      scanf("%d %d", &x, &y);
      G[x].push_back(y);
      G[y].push_back(x);
    }
    dp(1, 0);
    printf("%d\n", ans);
  }
  return 0;
}
