#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
const int inf = 0x3f3f3f3f;
vector<int> edge[maxn];
int size[3][maxn];
void upd(int x, int y) {
  int t = size[0][y] + (int)edge[y].size() - 1;
  if (size[0][x] <= t) {
    size[1][x] = size[0][x];
    size[0][x] = t;
  } else if (size[1][x] <= t) {
    size[2][x] = size[1][x];
    size[1][x] = t;
  } else if (size[2][x] < t) {
    size[2][x] = t;
  }
}
void dfs1(int u, int f) {
  size[0][u] = size[1][u] = size[2][u] = 0;
  for (auto i : edge[u]) {
    if (i == f) continue;
    dfs1(i, u);
    upd(u, i);
  }
}
int ans;
void dfs2(int u, int f) {
  int ts[3] = {size[0][u], size[1][u], size[2][u]};
  if (f) upd(u, f);
  ans = max(ans, size[0][u] + size[1][u] + (int)edge[u].size() + 1);
  for (auto i : edge[u]) {
    if (i == f) continue;
    int tts[3] = {size[0][u], size[1][u], size[2][u]};
    int t = size[0][i] + (int)edge[i].size() - 1;
    if (size[0][u] == t) {
      size[0][u] = size[1][u];
      size[1][u] = size[2][u];
    } else if (size[1][u] == t) {
      size[1][u] = size[2][u];
    }
    dfs2(i, u);
    size[0][u] = tts[0];
    size[1][u] = tts[1];
    size[2][u] = tts[2];
  }
  size[0][u] = ts[0];
  size[1][u] = ts[1];
  size[2][u] = ts[2];
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, x, y;
    scanf("%d", &n);
    for (int i = (1); i < (n + 1); i++) edge[i].clear();
    for (int i = (1); i < (n); i++) {
      scanf("%d%d", &x, &y);
      edge[x].push_back(y);
      edge[y].push_back(x);
    }
    ans = 0;
    dfs1(1, 0);
    dfs2(1, 0);
    printf("%d\n", ans);
  }
  return 0;
}
