#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
vector<int> G[300005];
int ans;
int dfs(int x, int p) {
  int mx1 = 0, mx2 = 0;
  for (int i : G[x]) {
    if (i != p) {
      int ret = dfs(i, x);
      if (ret > mx1) {
        mx2 = mx1;
        mx1 = ret;
      } else if (ret > mx2)
        mx2 = ret;
    }
  }
  if (mx2 == 0)
    ans = max(ans, mx1 + 1);
  else
    ans = max(ans, mx1 + mx2 + 1 + (int)G[x].size() - 2);
  return mx1 + 1 + (int)G[x].size() - 1 - (mx1 > 0);
}
int main() {
  cin.tie(0), ios::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) G[i].clear();
    for (int i = 1; i < n; i++) {
      int a, b;
      cin >> a >> b;
      G[a - 1].push_back(b - 1);
      G[b - 1].push_back(a - 1);
    }
    ans = 0;
    dfs(0, 0);
    cout << ans << "\n";
  }
  return 0;
}
