#include <bits/stdc++.h>
using namespace std;
inline int setBit(int mask, int pos) { return (mask | (1 << pos)); }
inline int resetBit(int mask, int pos) { return (mask & ~(1 << pos)); }
inline bool checkBit(int mask, int pos) { return (bool)(mask & (1 << pos)); }
int n;
vector<int> vec[300005];
int ans;
int DFS(int s, int p) {
  int mx1 = 0, mx2 = 0, tot = 0, child = 0;
  for (int x : vec[s]) {
    if (x == p) continue;
    child++;
    int tmp = DFS(x, s);
    if (mx1 < tmp)
      mx2 = mx1, mx1 = tmp;
    else if (mx2 < tmp)
      mx2 = tmp;
  }
  tot = mx1 + mx2 + max(0, child - 2) + 1;
  if (p != -1) tot++;
  ans = max(ans, tot);
  tot = mx1 + max(0, child - 1) + 1;
  return tot;
}
void clr() {
  for (int i = 1; i <= n; i++) vec[i].clear();
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t, x, y;
  cin >> t;
  while (t--) {
    cin >> n;
    clr();
    for (int i = 1; i < n; i++) {
      cin >> x >> y;
      vec[x].push_back(y);
      vec[y].push_back(x);
    }
    ans = 0;
    DFS(1, -1);
    cout << ans << endl;
  }
  return 0;
}
