#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7;
int n;
vector<int> g[N];
int d[N], res;
void go(int x, int z) {
  int s = g[x].size();
  d[x] = s;
  vector<int> v;
  for (int y : g[x]) {
    if (y != z) {
      go(y, x);
      d[x] = max(d[x], d[y] + s - 1);
      v.push_back(d[y]);
    }
  }
  res = max(res, d[x] + (z != -1));
  if (v.size() >= 2) {
    sort(v.rbegin(), v.rend());
    res = max(res, v[0] + v[1] + s - 1);
  }
}
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) g[i].clear();
    for (int i = 0; i < n - 1; ++i) {
      int x, y;
      scanf("%d %d", &x, &y);
      --x;
      --y;
      g[x].push_back(y);
      g[y].push_back(x);
    }
    res = 0;
    go(0, -1);
    printf("%d\n", res);
  }
  return 0;
}
