#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 1;
vector<int> g[N];
set<pair<int, int> > st[N];
int n, w[N];
void calc(int node, bool root) {
  w[node] = st[node].size() + 1;
  auto it = st[node].end();
  if (st[node].size()) it--, w[node] += it->first - 1;
  if (st[node].size() > 1 && root) it--, w[node] += it->first - 1;
}
void DFS(int node, int parent = -1) {
  for (auto child : g[node])
    if (child != parent) {
      DFS(child, node);
      st[node].insert({w[child], child});
    }
  calc(node, parent == -1);
}
int ans = 0;
void dfs(int node, int parent = -1) {
  ans = max(ans, w[node]);
  for (auto child : g[node])
    if (child != parent) {
      int old_child = w[child];
      st[node].erase({w[child], child});
      calc(node, 0);
      int old_node = w[node];
      st[child].insert({w[node], node});
      calc(child, 1);
      dfs(child, node);
      st[child].erase({old_node, node});
      calc(child, 0);
      st[node].insert({old_child, child});
      calc(node, 1);
    }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    ans = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) g[i].clear(), st[i].clear();
    for (int i = 1; i < n; i++) {
      int x, y;
      cin >> x >> y;
      g[x].push_back(y);
      g[y].push_back(x);
    }
    DFS(1);
    dfs(1);
    cout << ans << '\n';
  }
  return 0;
}
