#include <bits/stdc++.h>
using namespace std;
vector<long long> graph[301010];
vector<bool> visited(301010, false);
vector<long long> parent(301010, -1);
vector<pair<long long, long long>> ans(301010, {0, 0});
void dfs(long long node) {
  visited[node] = true;
  for (auto x : graph[node]) {
    if (!visited[x]) {
      parent[x] = node;
      dfs(x);
    }
  }
  if (true) {
    long long sz = graph[node].size();
    ans[node].second = sz - 1;
    ;
    pair<long long, long long> mxx = {0, 0};
    for (auto x : graph[node]) {
      if (x != parent[node]) {
        ans[node].first = max(ans[node].first, ans[x].first);
        if (ans[x].second >= mxx.first) {
          mxx.second = mxx.first;
          mxx.first = ans[x].second;
        } else if (ans[x].second >= mxx.second) {
          mxx.second = ans[x].second;
        }
        ans[node].second = max(ans[node].second, sz + ans[x].second - 1);
      }
    }
    ans[node].first = max(ans[node].first, sz + 1 + mxx.first + mxx.second);
  }
}
void clr(long long n) {
  for (long long i = 0; i < n; i++) {
    graph[i].clear();
    visited[i] = false;
    ans[i].first = 0;
    ans[i].second = 0;
    parent[i] = -1;
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, u, v;
    cin >> n;
    clr(n + 10);
    for (long long i = 1; i < n; i++) {
      cin >> u >> v;
      u--;
      v--;
      graph[u].push_back(v);
      graph[v].push_back(u);
    }
    long long root = 0;
    for (long long i = 0; i < n; i++) {
      long long sz = graph[i].size();
      if (sz != 1) {
        root = i;
        break;
      }
    }
    dfs(root);
    cout << ans[root].first << "\n";
  }
}
