#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<vector<int>> g;
int ans = 0;
int dfs(int u, int p) {
  int mx = 0, c = 0;
  vector<int> v;
  for (int i = 0; i < g[u].size(); ++i) {
    if (g[u][i] != p) {
      v.push_back(dfs(g[u][i], u));
      c++;
    }
  }
  sort(v.begin(), v.end());
  if (c == 1) {
    mx = v.back();
  } else if (c > 1) {
    ans = max(ans, 1 + v[v.size() - 1] + v[v.size() - 2] + c - 2 + (p != -1));
    mx = v.back();
  }
  ans = max(ans, 1 + mx + max(c - 1, 0));
  return 1 + mx + max(c - 1, 0);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    g.clear();
    ans = 0;
    g.resize(n + 1);
    for (int i = 0; i < n - 1; ++i) {
      int x, y;
      cin >> x >> y;
      g[--x].push_back(--y);
      g[y].push_back(x);
    }
    dfs(0, -1);
    cout << ans << endl;
  }
  return 0;
}
