#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int>> ad(n);
    for (int i = 1; i < n; i++) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      ad[a].push_back(b);
      ad[b].push_back(a);
    }
    int res = 1;
    vector<int> dp(n);
    function<void(int, int)> dfs = [&](int cur, int par) {
      int t = 1;
      if (ad[cur].size() > 2) t = ad[cur].size() - 1;
      dp[cur] = t;
      vector<int> ha;
      for (int ch : ad[cur]) {
        if (ch == par) continue;
        dfs(ch, cur);
        dp[cur] = max(dp[cur], t + dp[ch]);
        ha.push_back(dp[ch]);
      }
      sort(ha.begin(), ha.end());
      reverse(ha.begin(), ha.end());
      if (ha.size() == 1) res = max(res, t + ha[0]);
      if (ha.size() >= 2) res = max(res, t + ha[0] + ha[1]);
    };
    dfs(0, -1);
    cout << res << "\n";
  }
  return 0;
}
