#include <bits/stdc++.h>
using namespace std;
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> S) {
  os << "{ ";
  for (auto s : S) os << s << " ";
  return os << "}";
}
template <class T>
ostream& operator<<(ostream& os, multiset<T> S) {
  os << "{ ";
  for (auto s : S) os << s << " ";
  return os << "}";
}
template <class L, class R>
ostream& operator<<(ostream& os, map<L, R> M) {
  os << "{ ";
  for (auto m : M) os << "(" << m.F << ":" << m.S << ") ";
  return os << "}";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " |";
  __f(comma + 1, args...);
}
const int N = 3e5;
map<pair<int, int>, int> M;
vector<pair<int, int> > adj[N];
vector<int> dp;
void dfs(int node, int par, int ind) {
  int& ans = dp[ind];
  int mxf = -1;
  for (pair<int, int> child : adj[node]) {
    if (child.first == par) continue;
    if (dp[child.second] == -1) dfs(child.first, node, child.second);
    mxf = max(mxf, dp[child.second]);
  }
  if (mxf == -1)
    ans = 1;
  else
    ans = 1 + adj[node].size() - 2 + mxf;
}
void solve() {
  M.clear();
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    adj[i].clear();
  }
  dp = vector<int>(2 * n - 2, -1);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back({v, i});
    adj[v].push_back({u, i + n - 1});
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int a = -1, b = -1;
    for (pair<int, int> child : adj[i]) {
      if (dp[child.second] == -1) dfs(child.first, i, child.second);
      if (dp[child.second] > a) {
        b = a;
        a = dp[child.second];
      } else if (dp[child.second] > b) {
        b = dp[child.second];
      }
    }
    if (adj[i].size() == 1)
      ans = max(ans, 1 + a);
    else
      ans = max(ans, 1 + (int)adj[i].size() - 2 + a + b);
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1, tc = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
