#include <bits/stdc++.h>
using namespace std;
class GRAPH {
 public:
  int to[1010000], next[1010000];
  int head[300100];
  int cnt;
  GRAPH() { cnt = 1; }
  void add(int x, int y) {
    next[cnt] = head[x];
    to[cnt] = y;
    head[x] = cnt++;
  }
};
GRAPH g;
int n;
int dp[300100];
int ans = 0;
void init() {
  ans = 0;
  int x, y;
  memset(g.head, 0, sizeof(int) * (n + 5));
  cin >> n;
  g.cnt = 1;
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    g.add(x, y);
    g.add(y, x);
  }
}
void dfs(int pos, int pa) {
  vector<int> v;
  int mx = 0;
  dp[pos] = 1;
  for (register int i = g.head[pos]; i; i = g.next[i])
    if (g.to[i] != pa) {
      dfs(g.to[i], pos);
      v.push_back(dp[g.to[i]]);
      mx = max(mx, dp[g.to[i]]);
    }
  dp[pos] += mx;
  if (v.size() >= 2) dp[pos] += v.size() - 1;
  if (pos != pa) v.push_back(1);
  sort(v.begin(), v.end(), greater<int>());
  ans = max(ans, dp[pos]);
  if (v.size() >= 2) ans = max(ans, v[0] + v[1] - 1 + (int)v.size());
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    init();
    dfs(1, 1);
    cout << ans << endl;
  }
  return ~~(0 ^ 0 ^ 0);
}
