#include <bits/stdc++.h>
using namespace std;
pair<int, int> maxres;
vector<int> e[311111];
void dfs(int now, int p, int resnow) {
  maxres = max(maxres, {max(1, (int)e[now].size() - 1) + resnow, now});
  for (int k : e[now])
    if (k != p) dfs(k, now, max(1, (int)e[now].size() - 1) + resnow);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, a, b;
    scanf("%d", &n);
    for (int i = 1; i < n + 1; i++) e[i].clear();
    for (int i = 1; i < n; i++)
      scanf("%d%d", &a, &b), e[a].push_back(b), e[b].push_back(a);
    maxres = {0, 0};
    dfs(1, 1, 0);
    int tmp = maxres.second;
    dfs(tmp, tmp, 0);
    printf("%d\n", maxres.first);
  }
}
