#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  register long long x = 0, f = 1;
  register char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10ll + (ch ^ 48), ch = getchar();
  return x * f;
}
void Write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) Write(x / 10);
  putchar(x % 10 + 48);
}
void writes(long long x) { Write(x), putchar(' '); }
void Writes(long long x) { Write(x), putchar('\n'); }
int n, q, root, ans;
int f2[300101], f3[300101];
vector<int> mp[300101];
vector<int> trans[300101];
bool cmp(const int &x, const int &y) { return x > y; }
void Reset() {
  for (int i = 1; i <= n; ++i) mp[i].clear(), f2[i] = 0, f3[i] = 0;
  root = 0, ans = 0;
}
void Print(int x) { printf("now : %d , f2 = %d , f3 = %d\n", x, f2[x], f3[x]); }
void dfs(int x, int fa) {
  for (int i = 0; i < mp[x].size(); ++i) {
    int y = mp[x][i];
    if (y == fa) continue;
    dfs(y, x);
    trans[x].push_back(f2[y]);
  }
  if (trans[x].empty()) {
    f2[x] = 1, f3[x] = 1;
    return;
  }
  int son = (int)mp[x].size() - (x != root);
  sort(trans[x].begin(), trans[x].end(), cmp);
  f3[x] = f2[x] = 1 + son + (trans[x][0] - 1);
  if (trans[x].size() > 1) f3[x] += (trans[x][1] - 1) + (x != root);
  trans[x].clear();
}
int main() {
  q = read();
  while (q--) {
    Reset();
    n = read();
    for (int i = 1, x, y; i < n; ++i)
      x = read(), y = read(), mp[x].push_back(y), mp[y].push_back(x);
    if (n == 1 or n == 2 or n == 3) {
      Writes(n);
      continue;
    }
    for (int i = 1; i <= n; ++i)
      if (mp[i].size() > 1) {
        root = i;
        break;
      }
    dfs(root, 0);
    for (int i = 1; i <= n; ++i) ans = max(ans, f3[i]);
    Writes(ans);
  }
  return 0;
}
