#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n;
vector<int> graph[N];
int d[N];
int dp[N][2];
void dfs(int v, int p) {
  vector<int> d1;
  dp[v][1] = d[v] - 1;
  for (auto u : graph[v]) {
    if (u == p) {
      continue;
    }
    dfs(u, v);
    dp[v][0] = max(dp[v][0], dp[u][0]);
    if (graph[u].size() > 1) {
      d1.push_back(dp[u][1]);
      dp[v][1] = max(dp[v][1], d[v] + dp[u][1] - 1);
    }
  }
  sort(d1.rbegin(), d1.rend());
  int ans = d[v] + 1;
  for (int i = 0; i < 2; ++i) {
    if (i < d1.size()) {
      ans += d1[i];
    }
  }
  dp[v][0] = max(dp[v][0], ans);
}
void solver() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    graph[i].clear();
    d[i] = 0;
    dp[i][0] = dp[i][1] = 0;
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    graph[u - 1].push_back(v - 1);
    graph[v - 1].push_back(u - 1);
  }
  if (n <= 2) {
    cout << n << endl;
    return;
  }
  for (int v = 0; v < n; ++v) {
    d[v] = graph[v].size();
  }
  int notLeaf = -1;
  for (int v = 0; v < n; ++v) {
    if (graph[v].size() != 1) {
      notLeaf = v;
    }
  }
  dfs(notLeaf, notLeaf);
  cout << dp[notLeaf][0] << endl;
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solver();
  }
}
