#include using namespace std; using ll = long long; using pll = pair; #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; template struct edge{ int from, to; T cost; int id; edge(){} edge(int to, T cost=1) : from(-1), to(to), cost(cost){} edge(int to, T cost, int id) : from(-1), to(to), cost(cost), id(id){} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){} }; template struct redge{ int from, to; T cap, cost; int rev; redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){} redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){} }; template using Edges = vector>; template using weighted_graph = vector>; template using tree = vector>; using unweighted_graph = vector>; template using residual_graph = vector>>; template struct lowest_common_ancestor{ int root = 0; int n; int log_n = 1; vector> par; vector depth; lowest_common_ancestor(tree &T, int root=0) : root(root){ n = (int)T.size(); while((1 << log_n) < n) log_n++; par.resize(log_n, vector(n, -1)); depth.resize(n, 0); init(T); } void init(tree &T){ function dfs = [&](int v, int p){ par[0][v] = p; for(edge e : T[v]) if(e.to!=p){ depth[e.to] = depth[v]+1; dfs(e.to, v); } }; dfs(root, -1); for(int k=0; k+1> k & 1) u = par[k][u]; if(u == v) return u; for(int k=log_n-1; k>=0; k--){ if(par[k][u] != par[k][v]){ u = par[k][u]; v = par[k][v]; } } return par[0][u]; } }; void solve(){ int n; cin >> n; vector a(n); for(int i=0; i> a[i]; { vector c(n); for(int i=0; i T(n); for(int i=0; i> u >> v; u--; v--; T[u].pb(edge(v)); T[v].pb(edge(u)); } lowest_common_ancestor lca(T); vector depth(n, 0); function dfs = [&](int v, int p){ for(edge e : T[v]) if(e.to!=p){ depth[e.to] = depth[v]+1; dfs(e.to, v); } }; dfs(0, -1); function dist = [&](int u, int v){ return depth[u] + depth[v] -2*depth[lca.query(u, v)]; }; vector> b(n); for(int i=0; i ans(n, 0); for(int i=n-1; i>=0; i--){ //追加 for(int v : b[i]){ if(s==-1){ s = v; t = v; diam = 0; }else{ int s_dist = dist(s, v); int t_dist = dist(t, v); if(diam < max(s_dist, t_dist)){ if(s_dist>=t_dist){ t = v; diam = s_dist; }else{ s = v; diam = t_dist; } } } } for(int v : b[i]){ ans[v] = max(dist(s, v), dist(t, v)); } } for(int i=0; i> T; while(T--) solve(); }