#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 5;
int n;
vector<int> gph[MAXN];
int parent_[MAXN], depth_[MAXN];
int heavy[MAXN], height_[MAXN];
long long baseVal[MAXN], costNode[MAXN];
int ceil_log2_int(int x){ // ceil(log2 x), x >= 1
if (x <= 1) return 0;
return 32 - __builtin_clz(x - 1); // cho int 32-bit
}
inline long long G(int L){ // g(L)
if (L == 0) return 0;
return 1 + ceil_log2_int(L);
}
void dfs_height(int u, int p){
parent_[u] = p;
heavy[u] = -1;
int bestH = -1, bestChild = -1;
for (int v : gph[u]) if (v != p){
depth_[v] = depth_[u] + 1;
dfs_height(v, u);
if (height_[v] > bestH){
bestH = height_[v];
bestChild = v;
}
}
height_[u] = (bestH == -1 ? 0 : bestH + 1);
heavy[u] = bestChild; // **chọn con có height lớn nhất**
}
void decompose_from_head(int h){
int u = h, d = 0;
while (u != -1){
costNode[u] = baseVal[h] + G(d);
// rẽ qua các cạnh nhẹ
for (int v : gph[u]) if (v != parent_[u] && v != heavy[u]){
baseVal[v] = costNode[u] + 1; // trả 1 cho cạnh nhẹ
decompose_from_head(v); // head mới
}
u = heavy[u]; // tiếp tục xuống con nặng
d++;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
if (!(cin >> T)) return 0;
while (T--){
cin >> n;
for (int i = 1; i <= n; ++i){
gph[i].clear();
baseVal[i] = costNode[i] = 0;
}
for (int i = 0; i < n - 1; ++i){
int u, v; cin >> u >> v;
gph[u].push_back(v);
gph[v].push_back(u);
}
depth_[1] = 0;
dfs_height(1, 0); // chọn heavy = con cao nhất
baseVal[1] = 0;
decompose_from_head(1); // tính cost mọi nút
long long ans = 0;
for (int i = 1; i <= n; ++i) ans = max(ans, costNode[i]);
cout << ans << '\n';
}
return 0;
}