#include <bits/stdc++.h>
using namespace std;
const int N = 300100;
int cat, head[N] = {0};
struct Edge {
  int p, nt;
  Edge() : p(0), nt(0) {}
  Edge(int _p, int _nt) { p = _p, nt = _nt; }
} lk[N << 1];
inline void Ins(int In_po1, int In_po2) {
  lk[++cat] = Edge(In_po2, head[In_po1]);
  head[In_po1] = cat;
}
int d[N][2];
void dfs(int o, int dad) {
  int siz(0);
  int mx(-1), mxx(-1);
  for (int i = head[o]; i; i = lk[i].nt)
    if (lk[i].p != dad) {
      siz++;
      dfs(lk[i].p, o);
      int tmp = d[lk[i].p][0];
      if (mx < 0)
        mx = tmp;
      else if (tmp > mx)
        mxx = mx, mx = tmp;
      else if (mxx < 0)
        mxx = tmp;
      else if (tmp > mxx)
        mxx = tmp;
    }
  if (mx < 0) {
    d[o][0] = 1;
    d[o][1] = 1 + (!!dad);
  } else if (mxx < 0) {
    d[o][0] = mx + 1;
    d[o][1] = mx + 1 + (!!dad);
  } else {
    d[o][0] = mx + siz;
    d[o][1] = mx + mxx + siz - 1 + (!!dad);
  }
}
int main() {
  int T;
  scanf("%d", &T);
  for (; T--;) {
    int n, po1, po2;
    scanf("%d", &n);
    cat = 0;
    for (int i = 1; i <= n; ++i) head[i] = 0;
    for (int i = 1; i < n; ++i) {
      scanf("%d%d", &po1, &po2);
      Ins(po1, po2), Ins(po2, po1);
    }
    dfs(1, 0);
    int ans(0);
    for (int i = 1; i <= n; ++i) ans = max(ans, d[i][1]);
    printf("%d\n", ans);
  }
  return 0;
}
