#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-all-loops,unroll-loops")
#pragma GCC target("abm,mmx,sse,sse2,sse3,ssse3,sse4a,popcnt,tune=native")
using namespace std;
const int MAXN = 5e5;
vector<int> gr[MAXN];
vector<int> dp[MAXN];
int maxdp[MAXN];
int res = 0;
void dfs(int v, int pr) {
  maxdp[v] = 0;
  for (int i = 0; i < (int)gr[v].size(); i++) {
    int x = gr[v][i];
    if (x != pr) {
      dfs(x, v);
      dp[v][i] = maxdp[x] + (int)gr[v].size() - 1;
      maxdp[v] = max(maxdp[v], dp[v][i]);
    }
  }
  if (gr[v].size() <= 2 && pr != -1) return;
  int max1 = -1, max2 = -1;
  for (int i = 0; i < (int)gr[v].size(); i++) {
    if (gr[v][i] != pr) {
      int curval = dp[v][i];
      if (curval > max1)
        max2 = max1, max1 = curval;
      else if (curval > max2)
        max2 = curval;
    }
  }
  assert(max1 != -1);
  assert(max2 != -1);
  res = max(res, max1 + max2 + 1 - (int)gr[v].size());
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  int q;
  cin >> q;
  for (int t = 0; t < q; t++) {
    int n;
    cin >> n;
    res = 0;
    for (int i = 0; i < n; i++) gr[i].clear();
    for (int i = 0; i < n - 1; i++) {
      int x, y;
      cin >> x >> y;
      x--, y--;
      gr[x].push_back(y);
      gr[y].push_back(x);
    }
    for (int i = 0; i < n; i++) dp[i].resize((int)gr[i].size());
    if (n == 2) {
      cout << 2 << endl;
      continue;
    }
    int root = -1;
    for (int i = 0; i < n; i++)
      if (gr[i].size() >= 2) root = i;
    assert(root != -1);
    dfs(root, -1);
    cout << res + 2 << endl;
  }
  return 0;
}
