#include <bits/stdc++.h>
using namespace std;
template <class T>
void selfmax(T& a, const T& b) {
  a = max(a, b);
}
template <class T>
void selfmin(T& a, const T& b) {
  a = min(a, b);
}
template <typename T>
inline T gcd(T a, T b) {
  while (b != 0) swap(b, a %= b);
  return a;
}
void __print(long long x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V>& x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T& x) {
  long long f = 0;
  cerr << '{';
  for (auto& i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
template <class A>
void read(vector<A>& x) {
  for (auto& a : x) read(a);
}
template <class A>
void read(vector<pair<A, A> >& x) {
  for (auto& a : x) read(a.first, a.second);
}
template <typename... T>
void read(vector<long long>& a) {
  long long n = a.size();
  for (long long i = 0; i < n; i++) cin >> a[i];
}
template <class A>
void print(vector<A>& a) {
  for (auto& x : a) cout << x << " ";
  cout << endl;
}
long long prod(long long a, long long b, long long m) {
  long long res = 0;
  while (b) {
    if (b & 1) res = (res + a) % m;
    a = (a + a) % m;
    b >>= 1;
  }
  return res;
}
long long power(long long a, long long b, long long m) {
  long long res = 1;
  while (b) {
    if (b & 1) res = prod(res, a, m);
    a = prod(a, a, m);
    b >>= 1;
  }
  return res;
}
long long dx[] = {1, 0, -1, 0};
long long dy[] = {0, 1, 0, -1};
const long long MOD = 1e9 + 7;
const long long INF = 1000000000;
const long long BIGINF = 1e12;
template <typename I, typename P>
I binarysearch(const P& predicate, I l, I r) {
  l--;
  while (r - l > 1) {
    auto mid = l + (r - l) / 2;
    if (predicate(mid))
      r = mid;
    else
      l = mid;
  }
  return r;
}
vector<long long> graph[300008];
long long ans = 0;
long long dfs(long long root, long long par) {
  vector<long long> v;
  long long sum = 0;
  for (auto node : graph[root]) {
    if (node == par) {
      continue;
    } else {
      long long xx = dfs(node, root);
      v.push_back(xx);
      sum += xx;
    }
  }
  sort(v.begin(), v.end());
  if (v.size() >= 2) {
    long long temp1 =
        v[v.size() - 1] + v[v.size() - 2] + v.size() - 1LL + (par != -1);
    ans = max(ans, temp1);
  }
  if (v.size()) {
    long long temp2 = v[v.size() - 1] + 1 + v.size() - 1LL;
    ans = max(ans, temp2);
  }
  if (par == -1) {
    if (v.size() <= 2) {
      ans = max(ans, sum + 1LL);
      return sum + 1;
    } else {
      long long temp = v[v.size() - 1] + v[v.size() - 2] + v.size() - 1LL;
      ans = max(ans, temp);
      return v[v.size() - 1] + v[v.size() - 2] + v.size() - 2 + 1;
    }
  } else if (v.size() >= 1) {
    long long temp = v[v.size() - 1] + 1 + v.size() - 1LL;
    ans = max(ans, temp);
    return v[v.size() - 1] + 1 + v.size() - 1;
  } else {
    ans = max(ans, 2LL);
    return 1;
  }
}
void solve(long long tt) {
  long long n, i, j, k, x, y;
  cin >> n;
  ans = 0;
  for (i = 0; i <= n; i++) {
    graph[i].clear();
  }
  for (i = 0; i < n - 1; i++) {
    cin >> x >> y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  long long mx = 0, id = 1;
  for (i = 0; i <= n; i++) {
    if (graph[i].size() > mx) {
      mx = graph[i].size();
      id = i;
    }
  }
  dfs(1, -1);
  cout << ans << '\n';
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  cin >> t;
  for (long long tt = 1; tt <= t; tt++) {
    solve(tt);
  }
}
