#include <bits/stdc++.h>
using namespace std;
const int N = 3000100;
template <class Ruby>
inline void read(Ruby &x) {
  x = 0;
  char ch = getchar(), w = 0;
  while (!isdigit(ch)) w = (ch == '-'), ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  x = w ? -x : x;
}
int deg[N], n, nxt[N * 2], to[N * 2], hd[N], m, ans, maxx[N], bs, T;
inline void addedg(int qa, int qb) {
  nxt[++bs] = hd[qa], to[bs] = qb, hd[qa] = bs;
}
inline void dp(int u, int father) {
  maxx[u] = 1;
  for (register int i = hd[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == father) continue;
    dp(v, u);
    ans = max(ans, maxx[u] + maxx[v] + deg[u] - 1 - (maxx[u] > 1));
    maxx[u] = max(maxx[u], maxx[v] + 1);
  }
  maxx[u] = maxx[u] + deg[u] - (father > 0) - (maxx[u] > 1);
}
int main() {
  read(T);
  while (T--) {
    read(n);
    ans = bs = 0;
    for (register int i = 1; i <= n * 2; i++)
      hd[i] = maxx[i] = deg[i] = nxt[i] = to[i] = 0;
    for (register int i = 1, qa, qb; i < n; i++)
      read(qa), read(qb), addedg(qa, qb), addedg(qb, qa), deg[qa]++, deg[qb]++;
    dp(1, 0);
    printf("%d\n", ans);
  }
  return 0;
}
