#include <bits/stdc++.h>
using namespace std;
namespace zyt {
template <typename T>
inline bool read(T &x) {
  char c;
  bool f = false;
  x = 0;
  do c = getchar();
  while (c != EOF && c != '-' && !isdigit(c));
  if (c == EOF) return false;
  if (c == '-') f = true, c = getchar();
  do x = x * 10 + c - '0', c = getchar();
  while (isdigit(c));
  if (f) x = -x;
  return true;
}
template <typename T>
inline void write(T x) {
  static char buf[20];
  char *pos = buf;
  if (x < 0) putchar('-'), x = -x;
  do *pos++ = x % 10 + '0';
  while (x /= 10);
  while (pos > buf) putchar(*--pos);
}
const int N = 3e5 + 10;
int head[N], n, ecnt, dp[N][2];
struct edge {
  int to, next;
} e[N << 1];
void add(const int a, const int b) {
  e[ecnt] = (edge){b, head[a]}, head[a] = ecnt++;
}
void addtw(const int a, const int b) { add(a, b), add(b, a); }
void dfs(const int u, const int f) {
  int son = 0, mx = 0, mx2 = 0;
  dp[u][0] = dp[u][1] = 1;
  for (int i = head[u]; ~i; i = e[i].next) {
    int v = e[i].to;
    if (v == f) continue;
    ++son;
    dfs(v, u);
    if (dp[v][0] >= mx)
      mx2 = mx, mx = dp[v][0];
    else if (dp[v][0] > mx2)
      mx2 = dp[v][0];
  }
  if (!son) return;
  dp[u][0] = mx + son;
  dp[u][1] = mx + mx2 + son - 1 + bool(f);
}
int work() {
  int T;
  read(T);
  while (T--) {
    read(n);
    ecnt = 0;
    memset(head, -1, sizeof(int[n + 1]));
    for (int i = 1; i < n; i++) {
      int a, b;
      read(a), read(b);
      addtw(a, b);
    }
    dfs(1, 0);
    int ans = 0;
    for (int i = 1; i <= n; i++) ans = max(ans, max(dp[i][0], dp[i][1]));
    write(ans), putchar('\n');
  }
  return 0;
}
}  // namespace zyt
int main() { return zyt::work(); }
