#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
long long N;
vector<long long> w;
vector<vector<long long> > G, tmp;
long long solve(long long now, long long pa, long long &ans) {
  vector<long long> ch;
  for (auto &to : G[now]) {
    if (to == pa) continue;
    ch.push_back(solve(to, now, ans) + max(0LL, (long long)G[to].size() - 2));
  }
  sort((ch).rbegin(), (ch).rend());
  if (ch.size() == 0)
    return 1;
  else if (ch.size() == 1)
    chmax(ans, ch[0] + 1);
  else
    chmax(ans, ch[0] + ch[1] + (long long)G[now].size() - 1);
  return ch[0] + 1;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long q;
  cin >> q;
  while (q--) {
    long long ans = 0;
    cin >> N;
    G = tmp;
    G.resize(N);
    for (long long i = 0; i < N - 1; i++) {
      long long a, b;
      cin >> a >> b;
      a--;
      b--;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    solve(0, -1, ans);
    cout << ans << endl;
  }
  return 0;
}
