#include <iostream>
#include <string>
#include <queue>

using namespace std;

vector<vector<int> > adjList; // my adjacency list
int seen[50000];              // my BFS seen array
int dist[50000];              // my BFS dist array
int n;                        // number of nodes
int u, v;

int main() {
  int nc;
  cin >> nc;
  while(nc--) {
    adjList.clear();
    cin >> n;
    adjList.resize(n);
    for(int i = 1; i < n; i++) {
      cin >> u >> v;
      adjList[u].push_back(v);
      adjList[v].push_back(u);
    }
    memset(seen, 0, sizeof(seen));
    // now run two BFS, first one starts at 0
    // remember the final node;

    int final;
    queue<int> q;
    q.push(0);
    while(!q.empty()) {
      int curr = q.front();
      q.pop();

      seen[curr] = true;

      for(int i = 0; i < adjList[curr].size(); i++)
        if (!seen[adjList[curr][i]])
          q.push(adjList[curr][i]);

      if (q.empty()) final = curr;
    }

    // second bfs starts at the final node of the first one
    // this time we keep track of distance
    q.push(final);
    dist[final] = 0;
    int longestDist = -1;
    memset(seen, 0, sizeof(seen));

    while(!q.empty()) {
      int curr = q.front();
      q.pop();

      longestDist = dist[curr];
      seen[curr] = true;
      int neighbor;

      for(int i = 0; i < adjList[curr].size(); i++) {
        neighbor = adjList[curr][i];
        if (!seen[neighbor]) {
          q.push(neighbor);
          dist[neighbor] = dist[curr]+1;
        }
      }
    }
    cout << longestDist << endl;
  }
  return 0;
}
