#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
vector<int> G[300000];
multiset<int> child[300000];
int dp[300000];
int calc(int v, bool root) {
  if (child[v].size() >= 2 && root) {
    int sum = child[v].size() - 1;
    auto itr = child[v].begin();
    sum -= *(itr++);
    sum -= *(itr++);
    return sum;
  }
  if (child[v].size() == 0) return 1;
  return child[v].size() - (*child[v].begin());
}
void dfs(int v, int par) {
  for (int to : G[v]) {
    if (to == par) continue;
    dfs(to, v);
    child[v].insert(-dp[to]);
  }
  dp[v] = calc(v, par == -1);
}
int dfs2(int v, int par) {
  int ret = dp[v];
  for (int to : G[v]) {
    if (to == par) continue;
    child[v].erase(child[v].find(-dp[to]));
    dp[v] = calc(v, 0);
    child[to].insert(-dp[v]);
    dp[to] = calc(to, 1);
    ret = max(ret, dfs2(to, v));
    child[to].erase(child[to].find(-dp[v]));
    dp[to] = calc(to, 0);
    child[v].insert(-dp[to]);
    dp[v] = calc(v, 1);
  }
  return ret;
}
int main() {
  int Q;
  cin >> Q;
  for (int q = 0; q < Q; q++) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      G[i].clear();
      child[i].clear();
    }
    for (int i = 0; i + 1 < n; i++) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      G[x].push_back(y);
      G[y].push_back(x);
    }
    dfs(0, -1);
    cout << dfs2(0, -1) << endl;
  }
}
