// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN reverse replace stringstream string::npos length substr front priority_queue
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif
#define endl '\n'
#define ll long long
#define rint register int
#define minpq priority_queue <int, vector<int>, greater<int> >
#define maxpq priority_queue <int>
#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';
using db = long double ;
using pll = pair < ll, ll > ;
#define scan(a, n) for(int i = 0; i < n; i++)cin >> a[i];
#define rep(i,x,n,inc) for(int i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a) (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))
#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007
#define infl 1000000007
#define conutBits(n) __builtin_popcount(n)
#define Foxen(i,s) for (i=s.begin(); i!=s.end(); i++)
#define Fill(s,v) memset(s,v,sizeof(s))
#define cout_p(x, p) cout << fixed << setprecision((p)) << x << endl //print with precision
void test_case( ) {
ll n, m, q;
cin >> n;
vector< pll> v[ n + 1 ] , query[ n + 1 ] ;
for ( int i = 0 ; i < n - 1 ; i++ ) {
ll x, y, w;
cin >> x >> y >> w;
v[ x] .push_back ( { y, w} ) ;
v[ y] .push_back ( { x, w} ) ;
}
vector< ll> ans( n + 1 , 0 ) , sub( n + 1 , 0 ) , pre( n + 1 , 0 ) ;
function< void ( ll, ll) > dfs = [ & ] ( ll s, ll p) {
sub[ s] = 0 ;
for ( auto zx : v[ s] ) {
if ( zx.ff ! = p) {
dfs( zx.ff , s) ;
// debug(s, zx.ss + sub[zx.ff]);
if ( zx.ss + sub[ zx.ff ] >= sub[ s] ) {
pre[ s] = sub[ s] ;
sub[ s] = zx.ss + sub[ zx.ff ] ;
} else if ( zx.ss + sub[ zx.ff ] >= pre[ s] ) {
pre[ s] = zx.ss + sub[ zx.ff ] ;
}
}
}
} ;
function< void ( ll, ll, ll) > bfs = [ & ] ( ll s, ll p, ll up) {
if ( s ! = 1 ) {
debug( s) ;
for ( auto zx : v[ s] ) {
if ( zx.ff ! = p) {
sub[ zx.ff ] = max( sub[ zx.ff ] , zx.ss + up) ;
debug( zx.ff , s, zx.ss + up) ;
bfs( zx.ff , s, zx.ss + up) ;
}
}
} else {
for ( auto zx : v[ s] ) {
if ( zx.ff ! = p) {
if ( zx.ss + sub[ zx.ff ] == sub[ s] ) {
sub[ zx.ff ] = max( sub[ zx.ff ] , zx.ss + pre[ s] ) ;
debug( zx.ff , s, zx.ss + pre[ s] ) ;
bfs( zx.ff , s, zx.ss + pre[ s] ) ;
} else {
sub[ zx.ff ] = max( sub[ zx.ff ] , zx.ss + sub[ s] ) ;
debug( zx.ff , s, zx.ss + sub[ s] ) ;
bfs( zx.ff , s, zx.ss + sub[ s] ) ;
}
}
}
}
// 12 9 7 12
} ;
dfs( 1 , - 1 ) ;
bfs( 1 , - 1 , 0 ) ;
for ( int i = 1 ; i < n + 1 ; i++ )
cout << sub[ i] << ' ' ;
cout << "\n " ;
debug( sub, pre) ;
}
int32_t main( ) {
ios_base:: sync_with_stdio ( false ) ;
cin .tie ( NULL ) ; cout .tie ( NULL ) ;
ll tc;
cin >> tc;
while ( tc-- )
test_case( ) ;
return 0 ;
}
Ly8gZmlyc3Qgc2Vjb25kIHB1c2hfYmFjayB1bm9yZGVyZWQgcmV0dXJuIGNvbnRpbnVlIGJyZWFrIHZlY3RvciB2aXNpdGVkIGNoZWNrIGZsYWcgYm9vbCB3aGlsZSBpdGVyYXRvciBiZWdpbiBlbmQgbG93ZXJfYm91bmQgdXBwZXJfYm91bmQgdGVtcCB0cnVlIGZhbHNlIGxsX01BWCBsbF9NSU4gaW5zZXJ0IGVyYXNlIGNsZWFyIHBvcCBwdXNoIGNvbXBhcmUgbGw2NF9NQVggbGw2NF9N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