#include <bits/stdc++.h>
using namespace std;
const int N = int(3e5) + 99;
int n;
vector<int> g[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 to : g[v]) {
    if (to == p) continue;
    dfs(to, v);
    dp[v][0] = max(dp[v][0], dp[to][0]);
    if (g[to].size() > 1) {
      d1.push_back(dp[to][1]);
      dp[v][1] = max(dp[v][1], d[v] + dp[to][1] - 1);
    }
  }
  int first_max = 0, second_max = 0;
  int x = d[v] + 1;
  for (auto elem : d1) {
    if (elem >= first_max) {
      second_max = first_max;
      first_max = elem;
    } else if (elem < first_max && elem > second_max)
      second_max = elem;
  }
  dp[v][0] = max(dp[v][0], x + first_max + second_max);
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  for (int qc = 0; qc < q; ++qc) {
    cin >> n;
    for (int i = 0; i < n; ++i) {
      g[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;
      --u, --v;
      g[u].push_back(v), g[v].push_back(u);
    }
    if (n <= 2) {
      cout << n << endl;
      continue;
    }
    for (int v = 0; v < n; ++v) {
      d[v] = g[v].size();
    }
    int r = -1;
    for (int v = 0; v < n; ++v)
      if (g[v].size() != 1) r = v;
    dfs(r, r);
    cout << dp[r][0] << endl;
  }
  return 0;
}
