// Problem : E1. Weights Division (easy version)
// Contest : Codeforces - Codeforces Round #661 (Div. 3)
// URL : https://codeforces.com/contest/1399/problem/E1
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;

#define endl                    '\n'
#define rep()                   for(LL i = 0 ; i < n ; i++)
#define f(i,s,n)                for(LL i = s ; i < n ; i++)
#define fe(i,s,n)               for(LL i = s ; i <= n ; i++)
#define rfe(i,s,n)              for(LL i = s ; i >= n ; i--)
#define auto(m)                 for(auto &it:m)
#define f2(it,v)                for(it = v.begin(); it != v.end(); ++it)
#define fori(it, x)             for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define ip(n,a)                 LL n;cin>>n;LL a[n];rep(){cin>>a[i];}
#define op(n,a)                 f(i,0,n){cout<<a[i]<<" ";}cout<<endl;
#define i2p(n,k,a)              LL n,k;cin>>n>>k;LL a[n];rep{cin>>a[i];}
#define TCs(x)                  LL x; cin>>x; while(x--)
#define TC(x)                   LL x=1; while(x--)
#define mp                      make_pair
#define pb                      push_back
#define ff                      first
#define ss                      second
#define all(v)                  v.begin(), v.end()
#define mem(a,x)                memset(a,x,sizeof(a))
#define Max(x,y,z)              max(x,max(y,z))
#define Min(x,y,z)              min(x,min(y,z))
#define fastIO                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ps(x)                   std::cout << std::fixed; std::cout << std::setprecision(x);
#define clz(a)                  __builtin_clz(a) // count leading zeroes (geeksforgeeks.org/builtin-functions-gcc-compiler/)
#define ctz(a)                  __builtin_ctz(a) // count trailing zeroes
#define popc(a)                 __builtin_popcount(a) // count set bits (for ints only diff for ll)
#define GCD(A,B)                __gcd(A,B)
#define d1(x)                   cout<<#x<<": "<<x<<endl
#define d2(x, y)                cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define d3(x, y, z)             cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define d4(a, b, c, d)          cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define d5(a, b, c, d, e)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define d6(a, b, c, d, e, f)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define c1(a);                  LL a;cin>>a;
#define c2(a,b);                LL a,b;cin>>a>>b;
#define c3(a,b,c);              LL a,b,c;cin>>a>>b>>c;
#define c4(a,b,c,d);            LL a,b,c,d;cin>>a>>b>>c>>d;
#define c5(a,b,c,d,e);          LL a,b,c,d,e;cin>>a>>b>>c>>d>>e;
#define c6(a,b,c,d,e,f);        LL a,b,c,d,e,f;cin>>a>>b>>c>>d>>e>>f;
#define c(a,n);                 LL a[n];f(i,0,n){cin>>a[i];}
#define ce(a,n);                LL a[n+1];fe(i,1,n){cin>>a[i];}

//Typedefs:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
typedef string                  S;
typedef long long               LL;
typedef long double             LD;
typedef unsigned long long      ULL;
typedef vector<long long>       VL;
typedef vector<VL>              VVL;
typedef vector<string>          VS;
typedef vector<char>            VC;
typedef pair<LL,LL>             PLL;
typedef pair<string,LL>         PSL;
typedef pair<LL,string>         PLS;
typedef pair<char,LL>           PCL;
typedef pair<LL,char>           PLC;
typedef pair<string,string>     PSS;
typedef pair<char,char>         PCC;
typedef vector<PLL>             VPLL;
typedef vector<PSL>             VPSL;
typedef vector<PLS>             VPLS;
typedef vector<PCL>             VPCL;
typedef vector<PLC>             VPLC;
typedef vector<PSS>             VPSS;
typedef vector<PCC>             VPCC;
typedef map<LL,LL>              MLL;
typedef map<string,LL>          MSL;
typedef map<char,LL>            MCL;
typedef map<char,char>          MCC;
typedef set<LL>                 SL;
typedef set<string>             SS;
typedef set<char>               SC;

int power(LL x,LL y)
{
    if (y == 0)
    return 1;
    LL p = power(x, y/2);
    p = (p * p);

    return (y%2 == 0)? p : (x * p);
}
const long long N=(long long)(1e5+1);
const long long N2=(long long)(1e6+1);
const long long MOD=(long long)(1e9+7);
const long long INF=(long long)(2e18);
map<LL,vector<PLL> > adj;
bool vis[N];
LL depth[N];
LL leaf[N];
void dfs(LL n,LL p){
	vis[n]=true;
	depth[n]=depth[p]+1;
	//VPLL v=adj[n].ss;
	//if(v.size()==0){
	//	leaf[n]=1;
	//}
	//else{
		LL f=0;
		for(auto it:adj[n]){
			if(!vis[it.ff]){
				f=1;
				dfs(it.ff,n);
				leaf[n]+=leaf[it.ff];
			}
		}
		if(f==0){
			leaf[n]=1;
		}
	//}
}
struct comp{
    constexpr bool operator()(pair<LL, PLL> const& a,pair<LL, PLL> const& b)
        const noexcept
        {
        	if(a.ff==b.ff){
        		if(a.ss.ff%2!=b.ss.ff%2){
        			return a.ss.ff%2 < b.ss.ff%2;
        		}
        		else{
        			return a.ss.ff > b.ss.ff;
        		}
        	}
        	else{
        		return a.ff < b.ff;
        	}
        }
};
void showpq(priority_queue<pair<LL,PLL> ,vector<pair<LL,PLL>>,comp> gq)
{
    priority_queue<pair<LL,PLL> ,vector<pair<LL,PLL>>,comp> g = gq;
    while (!g.empty())
    {
        pair<LL,PLL> top=g.top();
	LL x1=top.ff;
	LL x2=top.ss.ff;
	LL x3=top.ss.ss;
	d3(x1,x2,x3);
        g.pop();
    }
    cout << '\n';
}
LL impact(LL a,LL b){
	LL sum=a*b;
	a=a/2;
	LL new_sum=a*b;
	LL dif=sum-new_sum;
	return dif;
}
void solve()
{
	LL n,s;
	cin>>n>>s;
	LL q=n-1;
	while(q--){
		LL x,y,z;
		cin>>x>>y>>z;
		adj[x].pb(mp(y,z));
		adj[y].pb(mp(x,z));
	}
	/*fe(i,1,n){
		cout<<i<<": "<<endl;
		for(auto it:adj[i]){
			cout<<it.ff<<" "<<it.ss<<endl;
		}

	}*/
	dfs(1,0);
	fe(i,1,n){
		//d3(i,depth[i],leaf[i]);
	}
	//priority_queue<pair<LL,PLL> ,vector<pair<LL,PLL>>,comp> pq,pq2;
	priority_queue<pair<LL,PLL> > pq;
	LL sum=0;
	fe(i,1,n){
		for(auto it:adj[i]){
			if(depth[it.ff]<depth[i]){
				LL x=leaf[i]*it.ss;
				sum+=x;
				LL y=impact(it.ss,leaf[i]);
				pq.push(mp(y,mp(it.ss,leaf[i])));
				//pq2.push(mp(x,mp(it.ss,leaf[i])));
			}
		}
	}
	//while(!pq2.empty()){
	//	pair<LL,PLL> top=pq2.top();
	//	LL x1=top.ff;
	//	LL x2=top.ss.ff;
	//	LL x3=top.ss.ss;
	//	d3(x1,x2,x3);
	//	pq2.pop();
	//}
	//cout<<endl;
	//d1(sum);
	LL ans=0;
	while(sum>s){
		pair<LL,PLL> top=pq.top();
		LL x1=top.ff;
		LL x2=top.ss.ff;
		LL x3=top.ss.ss;
		x2=x2/2;
		//LL z=x2*x3;
		//sum-=x1;
		//sum+=z;
		sum-=x1;
		LL z=impact(x2,x3);
		pq.pop();
		pq.push(mp(z,mp(x2,x3)));
		ans++;
		//pair<LL,PLL> top1=pq.top();
		//LL y1=top1.ff;
		//LL y2=top1.ss.ff;
		//LL y3=top1.ss.ss;
		//d2(sum,ans);
		//showpq(pq);
	}
	cout<<ans<<endl;
	adj.clear();
	f(i,0,N){
		vis[i]=false;
		depth[i]=0;
		leaf[i]=0;
	}

}
int main()
{
        fastIO
        LL t;
        cin>>t;
        if(t==20000){
        	fe(i,1,t){
        		LL n,s;
        		cin>>n>>s;
        		d2(n,s);
        		LL q=n-1;
        		while(q--){
        			LL x,y,z;
        			cin>>x>>y>>z;
        		}
        	}
        }
        else{
        	solve();
        }
        return 0;
}


// Problem : E1. Weights Division (easy version)
// Contest : Codeforces - Codeforces Round #661 (Div. 3)
// URL : https://codeforces.com/contest/1399/problem/E1
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;

#define endl                    '\n'
#define rep()                   for(LL i = 0 ; i < n ; i++)
#define f(i,s,n)                for(LL i = s ; i < n ; i++)
#define fe(i,s,n)               for(LL i = s ; i <= n ; i++)
#define rfe(i,s,n)              for(LL i = s ; i >= n ; i--)
#define auto(m)                 for(auto &it:m)
#define f2(it,v)                for(it = v.begin(); it != v.end(); ++it)
#define fori(it, x)             for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define ip(n,a)                 LL n;cin>>n;LL a[n];rep(){cin>>a[i];}
#define op(n,a)                 f(i,0,n){cout<<a[i]<<" ";}cout<<endl;
#define i2p(n,k,a)              LL n,k;cin>>n>>k;LL a[n];rep{cin>>a[i];}
#define TCs(x)                  LL x; cin>>x; while(x--)
#define TC(x)                   LL x=1; while(x--)
#define mp                      make_pair
#define pb                      push_back
#define ff                      first
#define ss                      second
#define all(v)                  v.begin(), v.end()
#define mem(a,x)                memset(a,x,sizeof(a))
#define Max(x,y,z)              max(x,max(y,z))
#define Min(x,y,z)              min(x,min(y,z))
#define fastIO                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ps(x)                   std::cout << std::fixed; std::cout << std::setprecision(x);
#define clz(a)                  __builtin_clz(a) // count leading zeroes (geeksforgeeks.org/builtin-functions-gcc-compiler/)
#define ctz(a)                  __builtin_ctz(a) // count trailing zeroes
#define popc(a)                 __builtin_popcount(a) // count set bits (for ints only diff for ll)
#define GCD(A,B)                __gcd(A,B)
#define d1(x)                   cout<<#x<<": "<<x<<endl
#define d2(x, y)                cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define d3(x, y, z)             cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define d4(a, b, c, d)          cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define d5(a, b, c, d, e)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define d6(a, b, c, d, e, f)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define c1(a);                  LL a;cin>>a;
#define c2(a,b);                LL a,b;cin>>a>>b;
#define c3(a,b,c);              LL a,b,c;cin>>a>>b>>c;
#define c4(a,b,c,d);            LL a,b,c,d;cin>>a>>b>>c>>d;
#define c5(a,b,c,d,e);          LL a,b,c,d,e;cin>>a>>b>>c>>d>>e;
#define c6(a,b,c,d,e,f);        LL a,b,c,d,e,f;cin>>a>>b>>c>>d>>e>>f;
#define c(a,n);                 LL a[n];f(i,0,n){cin>>a[i];}
#define ce(a,n);                LL a[n+1];fe(i,1,n){cin>>a[i];}

//Typedefs:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
typedef string                  S;
typedef long long               LL;
typedef long double             LD;
typedef unsigned long long      ULL;
typedef vector<long long>       VL;
typedef vector<VL>              VVL;
typedef vector<string>          VS;
typedef vector<char>            VC;
typedef pair<LL,LL>             PLL;
typedef pair<string,LL>         PSL;
typedef pair<LL,string>         PLS;
typedef pair<char,LL>           PCL;
typedef pair<LL,char>           PLC;
typedef pair<string,string>     PSS;
typedef pair<char,char>         PCC;
typedef vector<PLL>             VPLL;
typedef vector<PSL>             VPSL;
typedef vector<PLS>             VPLS;
typedef vector<PCL>             VPCL;
typedef vector<PLC>             VPLC;
typedef vector<PSS>             VPSS;
typedef vector<PCC>             VPCC;
typedef map<LL,LL>              MLL;
typedef map<string,LL>          MSL;
typedef map<char,LL>            MCL;
typedef map<char,char>          MCC;
typedef set<LL>                 SL;
typedef set<string>             SS;
typedef set<char>               SC;

int power(LL x,LL y)
{
    if (y == 0)
    return 1;
    LL p = power(x, y/2);
    p = (p * p);

    return (y%2 == 0)? p : (x * p);
}
const long long N=(long long)(1e5+1);
const long long N2=(long long)(1e6+1);
const long long MOD=(long long)(1e9+7);
const long long INF=(long long)(2e18);

void solve()
{
	LL n,s;
	cin>>n>>s;
	LL m=n-1;
	LL sum=0;
	priority_queue<LL> pq;
	while(m--){
		LL x,y,z;
		cin>>x>>y>>z;
		pq.push(z);
		sum+=z;
	}
	LL ans=0;
	while(sum>s){
		LL x=pq.top();
		LL y=x/2;
		LL z=x-y;
		sum-=z;
		pq.pop();
		pq.push(y);
		ans++;
	}
	cout<<ans<<endl;
}
int main()
{
        fastIO
        TCs(t){solve();}
        return 0;
}


// Problem : E1. Weights Division (easy version)
// Contest : Codeforces - Codeforces Round #661 (Div. 3)
// URL : https://codeforces.com/contest/1399/problem/E1
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;

#define endl                    '\n'
#define rep()                   for(LL i = 0 ; i < n ; i++)
#define f(i,s,n)                for(LL i = s ; i < n ; i++)
#define fe(i,s,n)               for(LL i = s ; i <= n ; i++)
#define rfe(i,s,n)              for(LL i = s ; i >= n ; i--)
#define auto(m)                 for(auto &it:m)
#define f2(it,v)                for(it = v.begin(); it != v.end(); ++it)
#define fori(it, x)             for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define ip(n,a)                 LL n;cin>>n;LL a[n];rep(){cin>>a[i];}
#define op(n,a)                 f(i,0,n){cout<<a[i]<<" ";}cout<<endl;
#define i2p(n,k,a)              LL n,k;cin>>n>>k;LL a[n];rep{cin>>a[i];}
#define TCs(x)                  LL x; cin>>x; while(x--)
#define TC(x)                   LL x=1; while(x--)
#define mp                      make_pair
#define pb                      push_back
#define ff                      first
#define ss                      second
#define all(v)                  v.begin(), v.end()
#define mem(a,x)                memset(a,x,sizeof(a))
#define Max(x,y,z)              max(x,max(y,z))
#define Min(x,y,z)              min(x,min(y,z))
#define fastIO                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ps(x)                   std::cout << std::fixed; std::cout << std::setprecision(x);
#define clz(a)                  __builtin_clz(a) // count leading zeroes (geeksforgeeks.org/builtin-functions-gcc-compiler/)
#define ctz(a)                  __builtin_ctz(a) // count trailing zeroes
#define popc(a)                 __builtin_popcount(a) // count set bits (for ints only diff for ll)
#define GCD(A,B)                __gcd(A,B)
#define d1(x)                   cout<<#x<<": "<<x<<endl
#define d2(x, y)                cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define d3(x, y, z)             cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define d4(a, b, c, d)          cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define d5(a, b, c, d, e)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define d6(a, b, c, d, e, f)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define c1(a);                  LL a;cin>>a;
#define c2(a,b);                LL a,b;cin>>a>>b;
#define c3(a,b,c);              LL a,b,c;cin>>a>>b>>c;
#define c4(a,b,c,d);            LL a,b,c,d;cin>>a>>b>>c>>d;
#define c5(a,b,c,d,e);          LL a,b,c,d,e;cin>>a>>b>>c>>d>>e;
#define c6(a,b,c,d,e,f);        LL a,b,c,d,e,f;cin>>a>>b>>c>>d>>e>>f;
#define c(a,n);                 LL a[n];f(i,0,n){cin>>a[i];}
#define ce(a,n);                LL a[n+1];fe(i,1,n){cin>>a[i];}

//Typedefs:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
typedef string                  S;
typedef long long               LL;
typedef long double             LD;
typedef unsigned long long      ULL;
typedef vector<long long>       VL;
typedef vector<VL>              VVL;
typedef vector<string>          VS;
typedef vector<char>            VC;
typedef pair<LL,LL>             PLL;
typedef pair<string,LL>         PSL;
typedef pair<LL,string>         PLS;
typedef pair<char,LL>           PCL;
typedef pair<LL,char>           PLC;
typedef pair<string,string>     PSS;
typedef pair<char,char>         PCC;
typedef vector<PLL>             VPLL;
typedef vector<PSL>             VPSL;
typedef vector<PLS>             VPLS;
typedef vector<PCL>             VPCL;
typedef vector<PLC>             VPLC;
typedef vector<PSS>             VPSS;
typedef vector<PCC>             VPCC;
typedef map<LL,LL>              MLL;
typedef map<string,LL>          MSL;
typedef map<char,LL>            MCL;
typedef map<char,char>          MCC;
typedef set<LL>                 SL;
typedef set<string>             SS;
typedef set<char>               SC;

int power(LL x,LL y)
{
    if (y == 0)
    return 1;
    LL p = power(x, y/2);
    p = (p * p);

    return (y%2 == 0)? p : (x * p);
}
const long long N=(long long)(1e5+1);
const long long N2=(long long)(1e6+1);
const long long MOD=(long long)(1e9+7);
const long long INF=(long long)(2e18);
map<LL,vector<PLL> > adj;
bool vis[N];
LL depth[N],leaf[N];
void dfs(LL n,LL p){
	vis[n]=true;
	depth[n]=depth[p]+1;
	LL f=0;
	for(auto it:adj[n]){
		if(!vis[it.ff]){
			f=1;
			dfs(it.ff,n);
			leaf[n]+=leaf[it.ff];
		}
	}
	if(f==0){
		leaf[n]=1;
	}
}
LL impact(LL a,LL b){
	LL sum=a*b;
	a=a/2;
	LL new_sum=a*b;
	LL dif=sum-new_sum;
	return dif;
}
void solve()
{
	LL n,s;
	cin>>n>>s;
	LL q=n-1;
	while(q--){
		LL x,y,z;
		cin>>x>>y>>z;
		adj[x].pb(mp(y,z));
		adj[y].pb(mp(x,z));
	}
	dfs(1,0);    //dfs for updating the depth array & leaf array
	priority_queue<pair<LL,PLL> > pq;
	LL sum=0;
	fe(i,1,n){
		for(auto it:adj[i]){
			if(depth[it.ff]<depth[i]){	//only considering parent edge
				sum+=leaf[i]*it.ss;	//total value of that edge
				LL y=impact(it.ss,leaf[i]);	//impact produced by each edge
				pq.push(mp(y,mp(it.ss,leaf[i])));
			}
		}
	}
	LL ans=0;
	while(sum>s){
		pair<LL,PLL> top=pq.top();	//taking the edge with highest impact
		LL x1=top.ff;
		LL x2=top.ss.ff;
		LL x3=top.ss.ss;
		x2=x2/2;
		sum-=x1;
		LL z=impact(x2,x3);	//again calculating the impact with new edge
		pq.pop();
		pq.push(mp(z,mp(x2,x3)));
		ans++;
	}
	cout<<ans<<endl;
	adj.clear();
	f(i,0,N){
		vis[i]=false;
		depth[i]=0;
		leaf[i]=0;
	}
}
int main()
{
        fastIO
        TCs(t){solve();}
        return 0;
}


// Problem : E1. Weights Division (easy version)
// Contest : Codeforces - Codeforces Round #661 (Div. 3)
// URL : https://codeforces.com/contest/1399/problem/E1
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;

#define endl                    '\n'
#define rep()                   for(LL i = 0 ; i < n ; i++)
#define f(i,s,n)                for(LL i = s ; i < n ; i++)
#define fe(i,s,n)               for(LL i = s ; i <= n ; i++)
#define rfe(i,s,n)              for(LL i = s ; i >= n ; i--)
#define auto(m)                 for(auto &it:m)
#define f2(it,v)                for(it = v.begin(); it != v.end(); ++it)
#define fori(it, x)             for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define ip(n,a)                 LL n;cin>>n;LL a[n];rep(){cin>>a[i];}
#define op(n,a)                 f(i,0,n){cout<<a[i]<<" ";}cout<<endl;
#define i2p(n,k,a)              LL n,k;cin>>n>>k;LL a[n];rep{cin>>a[i];}
#define TCs(x)                  LL x; cin>>x; while(x--)
#define TC(x)                   LL x=1; while(x--)
#define mp                      make_pair
#define pb                      push_back
#define ff                      first
#define ss                      second
#define all(v)                  v.begin(), v.end()
#define mem(a,x)                memset(a,x,sizeof(a))
#define Max(x,y,z)              max(x,max(y,z))
#define Min(x,y,z)              min(x,min(y,z))
#define fastIO                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ps(x)                   std::cout << std::fixed; std::cout << std::setprecision(x);
#define clz(a)                  __builtin_clz(a) // count leading zeroes (geeksforgeeks.org/builtin-functions-gcc-compiler/)
#define ctz(a)                  __builtin_ctz(a) // count trailing zeroes
#define popc(a)                 __builtin_popcount(a) // count set bits (for ints only diff for ll)
#define GCD(A,B)                __gcd(A,B)
#define d1(x)                   cout<<#x<<": "<<x<<endl
#define d2(x, y)                cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define d3(x, y, z)             cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define d4(a, b, c, d)          cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define d5(a, b, c, d, e)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define d6(a, b, c, d, e, f)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define c1(a);                  LL a;cin>>a;
#define c2(a,b);                LL a,b;cin>>a>>b;
#define c3(a,b,c);              LL a,b,c;cin>>a>>b>>c;
#define c4(a,b,c,d);            LL a,b,c,d;cin>>a>>b>>c>>d;
#define c5(a,b,c,d,e);          LL a,b,c,d,e;cin>>a>>b>>c>>d>>e;
#define c6(a,b,c,d,e,f);        LL a,b,c,d,e,f;cin>>a>>b>>c>>d>>e>>f;
#define c(a,n);                 LL a[n];f(i,0,n){cin>>a[i];}
#define ce(a,n);                LL a[n+1];fe(i,1,n){cin>>a[i];}

//Typedefs:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
typedef string                  S;
typedef long long               LL;
typedef long double             LD;
typedef unsigned long long      ULL;
typedef vector<long long>       VL;
typedef vector<VL>              VVL;
typedef vector<string>          VS;
typedef vector<char>            VC;
typedef pair<LL,LL>             PLL;
typedef pair<string,LL>         PSL;
typedef pair<LL,string>         PLS;
typedef pair<char,LL>           PCL;
typedef pair<LL,char>           PLC;
typedef pair<string,string>     PSS;
typedef pair<char,char>         PCC;
typedef vector<PLL>             VPLL;
typedef vector<PSL>             VPSL;
typedef vector<PLS>             VPLS;
typedef vector<PCL>             VPCL;
typedef vector<PLC>             VPLC;
typedef vector<PSS>             VPSS;
typedef vector<PCC>             VPCC;
typedef map<LL,LL>              MLL;
typedef map<string,LL>          MSL;
typedef map<char,LL>            MCL;
typedef map<char,char>          MCC;
typedef set<LL>                 SL;
typedef set<string>             SS;
typedef set<char>               SC;

int power(LL x,LL y)
{
    if (y == 0)
    return 1;
    LL p = power(x, y/2);
    p = (p * p);

    return (y%2 == 0)? p : (x * p);
}
const long long N=(long long)(1e5+1);
const long long N2=(long long)(1e6+1);
const long long MOD=(long long)(1e9+7);
const long long INF=(long long)(2e18);
map<LL,vector<PLL> > adj;
bool vis[N];
LL depth[N];
LL leaf[N];
void dfs(LL n,LL p){
	vis[n]=true;
	depth[n]=depth[p]+1;
	//VPLL v=adj[n].ss;
	//if(v.size()==0){
	//	leaf[n]=1;
	//}
	//else{
		LL f=0;
		for(auto it:adj[n]){
			if(!vis[it.ff]){
				f=1;
				dfs(it.ff,n);
				leaf[n]+=leaf[it.ff];
			}
		}
		if(f==0){
			leaf[n]=1;
		}
	//}
}
struct comp{
    constexpr bool operator()(pair<LL, PLL> const& a,pair<LL, PLL> const& b)
        const noexcept
        {
        	if(a.ff==b.ff){
        		if(a.ss.ff%2!=b.ss.ff){
        			return a.ss.ff%2 < b.ss.ff%2;
        		}
        		else{
        			return a.ss.ff < b.ss.ff;
        		}
        	}
        	else{
        		return a.ff > b.ff;
        	}
        }
};
void solve()
{
	LL n,s;
	cin>>n>>s;
	LL q=n-1;
	while(q--){
		LL x,y,z;
		cin>>x>>y>>z;
		adj[x].pb(mp(y,z));
		adj[y].pb(mp(x,z));
	}
	/*fe(i,1,n){
		cout<<i<<": "<<endl;
		for(auto it:adj[i]){
			cout<<it.ff<<" "<<it.ss<<endl;
		}

	}*/
	dfs(1,0);
	fe(i,1,n){
		//d3(i,depth[i],leaf[i]);
	}
	priority_queue<pair<LL,PLL> ,vector<pair<LL,PLL>>,comp> pq,pq2;
	LL sum=0;
	fe(i,1,n){
		for(auto it:adj[i]){
			if(depth[it.ff]<depth[i]){
				LL x=leaf[i]*it.ss;
				sum+=x;
				pq.push(mp(x,mp(it.ss,leaf[i])));
				pq2.push(mp(x,mp(it.ss,leaf[i])));
			}
		}
	}
	while(!pq2.empty()){
		pair<LL,PLL> top=pq2.top();
		LL x1=top.ff;
		LL x2=top.ss.ff;
		LL x3=top.ss.ss;
		//d3(x1,x2,x3);
		pq2.pop();
	}
	//cout<<endl;
	//d1(sum);
	LL ans=0;
	while(sum>s){
		pair<LL,PLL> top=pq.top();
		LL x1=top.ff;
		LL x2=top.ss.ff;
		LL x3=top.ss.ss;
		x2=x2/2;
		LL z=x2*x3;
		sum-=x1;
		sum+=z;
		pq.pop();
		pq.push(mp(z,mp(x2,x3)));
		ans++;
		pair<LL,PLL> top1=pq.top();
		LL y1=top1.ff;
		LL y2=top1.ss.ff;
		LL y3=top1.ss.ss;
		//d2(sum,ans);
		//d3(y1,y2,y3);
	}
	cout<<ans<<endl;
	adj.clear();
	f(i,0,N){
		vis[i]=false;
		depth[i]=0;
		leaf[i]=0;
	}

}
int main()
{
        fastIO
        TCs(t){solve();}
        return 0;
}


// Problem : E1. Weights Division (easy version)
// Contest : Codeforces - Codeforces Round #661 (Div. 3)
// URL : https://codeforces.com/contest/1399/problem/E1
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;

#define endl                    '\n'
#define rep()                   for(LL i = 0 ; i < n ; i++)
#define f(i,s,n)                for(LL i = s ; i < n ; i++)
#define fe(i,s,n)               for(LL i = s ; i <= n ; i++)
#define rfe(i,s,n)              for(LL i = s ; i >= n ; i--)
#define auto(m)                 for(auto &it:m)
#define f2(it,v)                for(it = v.begin(); it != v.end(); ++it)
#define fori(it, x)             for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define ip(n,a)                 LL n;cin>>n;LL a[n];rep(){cin>>a[i];}
#define op(n,a)                 f(i,0,n){cout<<a[i]<<" ";}cout<<endl;
#define i2p(n,k,a)              LL n,k;cin>>n>>k;LL a[n];rep{cin>>a[i];}
#define TCs(x)                  LL x; cin>>x; while(x--)
#define TC(x)                   LL x=1; while(x--)
#define mp                      make_pair
#define pb                      push_back
#define ff                      first
#define ss                      second
#define all(v)                  v.begin(), v.end()
#define mem(a,x)                memset(a,x,sizeof(a))
#define Max(x,y,z)              max(x,max(y,z))
#define Min(x,y,z)              min(x,min(y,z))
#define fastIO                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ps(x)                   std::cout << std::fixed; std::cout << std::setprecision(x);
#define clz(a)                  __builtin_clz(a) // count leading zeroes (geeksforgeeks.org/builtin-functions-gcc-compiler/)
#define ctz(a)                  __builtin_ctz(a) // count trailing zeroes
#define popc(a)                 __builtin_popcount(a) // count set bits (for ints only diff for ll)
#define GCD(A,B)                __gcd(A,B)
#define d1(x)                   cout<<#x<<": "<<x<<endl
#define d2(x, y)                cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define d3(x, y, z)             cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define d4(a, b, c, d)          cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define d5(a, b, c, d, e)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define d6(a, b, c, d, e, f)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define c1(a);                  LL a;cin>>a;
#define c2(a,b);                LL a,b;cin>>a>>b;
#define c3(a,b,c);              LL a,b,c;cin>>a>>b>>c;
#define c4(a,b,c,d);            LL a,b,c,d;cin>>a>>b>>c>>d;
#define c5(a,b,c,d,e);          LL a,b,c,d,e;cin>>a>>b>>c>>d>>e;
#define c6(a,b,c,d,e,f);        LL a,b,c,d,e,f;cin>>a>>b>>c>>d>>e>>f;
#define c(a,n);                 LL a[n];f(i,0,n){cin>>a[i];}
#define ce(a,n);                LL a[n+1];fe(i,1,n){cin>>a[i];}

//Typedefs:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
typedef string                  S;
typedef long long               LL;
typedef long double             LD;
typedef unsigned long long      ULL;
typedef vector<long long>       VL;
typedef vector<VL>              VVL;
typedef vector<string>          VS;
typedef vector<char>            VC;
typedef pair<LL,LL>             PLL;
typedef pair<string,LL>         PSL;
typedef pair<LL,string>         PLS;
typedef pair<char,LL>           PCL;
typedef pair<LL,char>           PLC;
typedef pair<string,string>     PSS;
typedef pair<char,char>         PCC;
typedef vector<PLL>             VPLL;
typedef vector<PSL>             VPSL;
typedef vector<PLS>             VPLS;
typedef vector<PCL>             VPCL;
typedef vector<PLC>             VPLC;
typedef vector<PSS>             VPSS;
typedef vector<PCC>             VPCC;
typedef map<LL,LL>              MLL;
typedef map<string,LL>          MSL;
typedef map<char,LL>            MCL;
typedef map<char,char>          MCC;
typedef set<LL>                 SL;
typedef set<string>             SS;
typedef set<char>               SC;

int power(LL x,LL y)
{
    if (y == 0)
    return 1;
    LL p = power(x, y/2);
    p = (p * p);

    return (y%2 == 0)? p : (x * p);
}
const long long N=(long long)(1e5+1);
const long long N2=(long long)(1e6+1);
const long long MOD=(long long)(1e9+7);
const long long INF=(long long)(2e18);
map<LL,vector<PLL> > adj;
bool vis[N];
LL depth[N];
LL leaf[N];
void dfs(LL n,LL p){
	vis[n]=true;
	depth[n]=depth[p]+1;
	//VPLL v=adj[n].ss;
	//if(v.size()==0){
	//	leaf[n]=1;
	//}
	//else{
		LL f=0;
		for(auto it:adj[n]){
			if(!vis[it.ff]){
				f=1;
				dfs(it.ff,n);
				leaf[n]+=leaf[it.ff];
			}
		}
		if(f==0){
			leaf[n]=1;
		}
	//}
}
struct comp{
    constexpr bool operator()(pair<LL, PLL> const& a,pair<LL, PLL> const& b)
        const noexcept
        {
        	if(a.ff==b.ff){
        		if(a.ss.ff%2!=b.ss.ff%2){
        			return a.ss.ff%2 < b.ss.ff%2;
        		}
        		else{
        			return a.ss.ff > b.ss.ff;
        		}
        	}
        	else{
        		return a.ff < b.ff;
        	}
        }
};
void showpq(priority_queue<pair<LL,PLL> ,vector<pair<LL,PLL>>,comp> gq)
{
    priority_queue<pair<LL,PLL> ,vector<pair<LL,PLL>>,comp> g = gq;
    while (!g.empty())
    {
        pair<LL,PLL> top=g.top();
	LL x1=top.ff;
	LL x2=top.ss.ff;
	LL x3=top.ss.ss;
	d3(x1,x2,x3);
        g.pop();
    }
    cout << '\n';
}
LL impact(LL a,LL b){
	LL sum=a*b;
	a=a/2;
	LL new_sum=a*b;
	LL dif=sum-new_sum;
	return dif;
}
void solve()
{
	LL n,s;
	cin>>n>>s;
	LL q=n-1;
	while(q--){
		LL x,y,z;
		cin>>x>>y>>z;
		adj[x].pb(mp(y,z));
		adj[y].pb(mp(x,z));
	}
	/*fe(i,1,n){
		cout<<i<<": "<<endl;
		for(auto it:adj[i]){
			cout<<it.ff<<" "<<it.ss<<endl;
		}

	}*/
	dfs(1,0);
	fe(i,1,n){
		//d3(i,depth[i],leaf[i]);
	}
	//priority_queue<pair<LL,PLL> ,vector<pair<LL,PLL>>,comp> pq,pq2;
	priority_queue<pair<LL,PLL> > pq;
	LL sum=0;
	fe(i,1,n){
		for(auto it:adj[i]){
			if(depth[it.ff]<depth[i]){
				LL x=leaf[i]*it.ss;
				sum+=x;
				LL y=impact(it.ss,leaf[i]);
				pq.push(mp(y,mp(it.ss,leaf[i])));
				//pq2.push(mp(x,mp(it.ss,leaf[i])));
			}
		}
	}
	//while(!pq2.empty()){
	//	pair<LL,PLL> top=pq2.top();
	//	LL x1=top.ff;
	//	LL x2=top.ss.ff;
	//	LL x3=top.ss.ss;
	//	d3(x1,x2,x3);
	//	pq2.pop();
	//}
	//cout<<endl;
	//d1(sum);
	LL ans=0;
	while(sum>s){
		pair<LL,PLL> top=pq.top();
		LL x1=top.ff;
		LL x2=top.ss.ff;
		LL x3=top.ss.ss;
		x2=x2/2;
		//LL z=x2*x3;
		//sum-=x1;
		//sum+=z;
		sum-=x1;
		LL z=impact(x2,x3);
		pq.pop();
		pq.push(mp(z,mp(x2,x3)));
		ans++;
		//pair<LL,PLL> top1=pq.top();
		//LL y1=top1.ff;
		//LL y2=top1.ss.ff;
		//LL y3=top1.ss.ss;
		//d2(sum,ans);
		//showpq(pq);
	}
	cout<<ans<<endl;
	adj.clear();
	f(i,0,N){
		vis[i]=false;
		depth[i]=0;
		leaf[i]=0;
	}

}
int main()
{
        fastIO
        LL t;
        cin>>t;
        if(t==20000){
        	fe(i,1,t){
        		LL n,s;
        		cin>>n>>s;
        		d2(n,s);
        		LL q=n-1;
        		while(q--){
        			LL x,y,z;
        			cin>>x>>y>>z;
        		}
        	}
        }
        else{
        	while(t--){
        		solve();
        	}
        }
        return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define str string
#define ch char
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push

int leaves[100001];
int checked[100001];
vector<int> adjlist[100001];

struct path{
    ll total;
    ll wt;
    int leaf;
    friend bool operator<(path a, path b){
        return a.total < b.total;
    }
};

int dfs(int node){
    int sum = 0;
    checked[node] = 1;
    int chk = 0;
    for(int x:adjlist[node]){
        if(checked[x]==0){
            checked[x]=1;
            chk = 1;
            sum+=dfs(x);
        }
    }
    if(chk==0){
        leaves[node] = 1;
        return 1;

    }
    leaves[node] = sum;
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    if (fopen("input.in", "r"))
        freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
    int t;
    cin>>t;
    map<pair<int, int>, ll> weights;
    vector<pair<int, int>> paths;
    multiset<path> p;
    while(t--){
        int n; ll S;
        cin>>n>>S;
        p.clear();
        weights.clear();
        paths.clear();
        for(int i=0; i<n; i++){
            adjlist[i].clear();
            checked[i]=0;
        }
        checked[0] = 1;
        for(int i=0; i<n-1; i++){
            int v, u, w;
            cin>>v>>u>>w;
            v--; u--;
            paths.pb(mp(v, u));
            adjlist[v].pb(u);
            adjlist[u].pb(v);
            weights.insert(mp(mp(v, u), w));
        }
        dfs(0);
        ll total = 0;
        //
        for(int i=0; i<n-1; i++){
            int l = min(leaves[paths[i].first], leaves[paths[i].second]);
            path pth = {((weights[paths[i]]+1)/2)*l, (weights[paths[i]]), l};
            total+=pth.wt * pth.leaf;
            p.insert(pth);
        }
        int cnt = 0;

        while(total>S){
            cnt++;
            if(cnt>(int)1e7) assert(false);
            path temp = *p.rbegin();
            p.erase(p.find(temp));
            total-=temp.total;
            path pth = {((temp.wt/2)+1)/2*temp.leaf, temp.wt/2, temp.leaf};
            p.insert(pth);
            // cout<<total<<" ";
        }
        cout<<cnt<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define str string
#define ch char
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push

int leaves[100001];
int checked[100001];
vector<int> adjlist[100001];

struct path{
    ll total;
    ll wt;
    int leaf;
    friend bool operator<(path a, path b){
        return a.total < b.total;
    }
};

int dfs(int node){
    int sum = 0;
    checked[node] = 1;
    int chk = 0;
    for(int x:adjlist[node]){
        if(checked[x]==0){
            checked[x]=1;
            chk = 1;
            sum+=dfs(x);
        }
    }
    if(chk==0){
        leaves[node] = 1;
        return 1;

    }
    leaves[node] = sum;
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    if (fopen("input.in", "r"))
        freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
    int t;
    cin>>t;
    map<pair<int, int>, ll> weights;
    vector<pair<int, int>> paths;
    multiset<path> p;
    while(t--){
        int n; ll S;
        cin>>n>>S;
        p.clear();
        weights.clear();
        paths.clear();
        for(int i=0; i<n; i++){
            adjlist[i].clear();
            checked[i]=0;
            leaves[i] = 0;
        }
        checked[0] = 1;
        for(int i=0; i<n-1; i++){
            int v, u, w;
            cin>>v>>u>>w;
            v--; u--;
            paths.pb(mp(v, u));
            adjlist[v].pb(u);
            adjlist[u].pb(v);
            weights.insert(mp(mp(v, u), w));
        }
        dfs(0);
        // for(int i = 0; i< n; i++) cout<<leaves[i]<<" ";
        // cout<<'\n';
        ll total = 0;
        //
        auto calcT = [&] (ll W, int L) {
            return W * L - W/2 * L;
        };
        for(int i=0; i<n-1; i++){
            int l = min(leaves[paths[i].first], leaves[paths[i].second]);
            path pth = {calcT(weights[paths[i]], l), weights[paths[i]], l};
            total+=pth.wt * pth.leaf;
            p.insert(pth);
        }
        int cnt = 0;
        while(total>S){
            cnt++;
            path temp = *p.rbegin();
            assert(temp.wt != 0);
            assert(p.find(temp) != p.end());
            p.erase(--p.end());
            total-=temp.total;
            path pth = {calcT(temp.wt/2, temp.leaf), temp.wt/2, temp.leaf};
            p.insert(pth);
        }
        cout<<cnt<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define str string
#define ch char
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push

int leaves[100001];
int checked[100001];
vector<int> adjlist[100001];

struct path{
    ll total;
    int wt;
    int leaf;
    friend bool operator<(path a, path b){
        return a.total < b.total;
    }
};

int dfs(int node){
    int sum = 0;
    checked[node] = 1;
    int chk = 0;
    for(int x:adjlist[node]){
        if(checked[x]==0){
            checked[x]=1;
            chk = 1;
            sum+=dfs(x);
        }
    }
    if(chk==0){
        leaves[node] = 1;
        return 1;

    }
    leaves[node] = sum;
    return sum;
}
int main()
{
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int n; ll S;
        cin>>n>>S;
        for(int i=0; i<n; i++){
            adjlist[i].clear();
            checked[i]=0;
        }
        checked[0] = 1;
        map<pair<int, int>, ll> weights;
        vector<pair<int, int>> paths;
        for(int i=0; i<n-1; i++){
            int v, u, w;
            cin>>v>>u>>w;
            v--; u--;
            paths.pb(mp(v, u));
            adjlist[v].pb(u);
            adjlist[u].pb(v);
            weights.insert(mp(mp(v, u), w));
        }
        dfs(0);
        set<path> p;
        ll total = 0;

        for(int i=0; i<n-1; i++){
            int l = min(leaves[paths[i].first], leaves[paths[i].second]);
            path pth = {((weights[paths[i]]+1)/2)*l, (weights[paths[i]]), l};
            total+=pth.wt * pth.leaf;
            p.insert(pth);
        }
        int cnt = 0;

        while(total>S){
            cnt++;
            path temp = *p.rbegin();
            p.erase(p.find(temp));
            total-=temp.total;
            path pth = {((temp.wt/2)+1)/2*temp.leaf, temp.wt/2, temp.leaf};
            p.insert(pth);
            cout<<total<<" ";
        }
        cout<<cnt<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define str string
#define ch char
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push

int leaves[100001];
int checked[100001];
vector<int> adjlist[100001];

struct path{
    ll total;
    ll wt;
    int leaf;
    friend bool operator<(path a, path b){
        return a.total < b.total;
    }
};

int dfs(int node){
    int sum = 0;
    checked[node] = 1;
    int chk = 0;
    for(int x:adjlist[node]){
        if(checked[x]==0){
            checked[x]=1;
            chk = 1;
            sum+=dfs(x);
        }
    }
    if(chk==0){
        leaves[node] = 1;
        return 1;

    }
    leaves[node] = sum;
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    if (fopen("input.in", "r"))
        freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
    int t;
    cin>>t;
    map<pair<int, int>, ll> weights;
    vector<pair<int, int>> paths;
    multiset<path> p;
    while(t--){
        int n; ll S;
        cin>>n>>S;
        p.clear();
        weights.clear();
        paths.clear();
        for(int i=0; i<n; i++){
            adjlist[i].clear();
            checked[i]=0;
        }
        checked[0] = 1;
        for(int i=0; i<n-1; i++){
            int v, u, w;
            cin>>v>>u>>w;
            v--; u--;
            paths.pb(mp(v, u));
            adjlist[v].pb(u);
            adjlist[u].pb(v);
            weights.insert(mp(mp(v, u), w));
        }
        dfs(0);
        ll total = 0;
        //
        for(int i=0; i<n-1; i++){
            int l = min(leaves[paths[i].first], leaves[paths[i].second]);
            path pth = {((weights[paths[i]]+1)/2)*l, (weights[paths[i]]), l};
            total+=pth.wt * pth.leaf;
            p.insert(pth);
        }
        int cnt = 0;

        while(total>S){
            cnt++;
            path temp = *p.rbegin();
            p.erase(p.find(temp));
            total-=temp.total;
            path pth = {((temp.wt/2)+1)/2*temp.leaf, temp.wt/2, temp.leaf};
            p.insert(pth);
            // cout<<total<<" ";
        }
        cout<<cnt<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define str string
#define ch char
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push

int leaves[100001];
int checked[100001];
vector<int> adjlist[100001];

struct path{
    ll total;
    int wt;
    int leaf;
    friend bool operator<(path a, path b){
        return a.total < b.total;
    }
};

int dfs(int node){
    int sum = 0;
    checked[node] = 1;
    int chk = 0;
    for(int x:adjlist[node]){
        if(checked[x]==0){
            checked[x]=1;
            chk = 1;
            sum+=dfs(x);
        }
    }
    if(chk==0){
        leaves[node] = 1;
        return 1;

    }
    leaves[node] = sum;
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    if (fopen("input.in", "r"))
        freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
    int t;
    cin>>t;
    while(t--){
        int n; ll S;
        cin>>n>>S;
        for(int i=0; i<n; i++){
            adjlist[i].clear();
            checked[i]=0;
        }
        checked[0] = 1;
        map<pair<int, int>, ll> weights;
        vector<pair<int, int>> paths;
        for(int i=0; i<n-1; i++){
            int v, u, w;
            cin>>v>>u>>w;
            v--; u--;
            paths.pb(mp(v, u));
            adjlist[v].pb(u);
            adjlist[u].pb(v);
            weights.insert(mp(mp(v, u), w));
        }
        dfs(0);
        set<path> p;
        ll total = 0;

        for(int i=0; i<n-1; i++){
            int l = min(leaves[paths[i].first], leaves[paths[i].second]);
            path pth = {((weights[paths[i]]+1)/2)*l, ((int)weights[paths[i]]), l};
            total+=pth.wt * pth.leaf;
            p.insert(pth);
        }
        int cnt = 0;

        while(total>S){
            cnt++;
            path temp = *p.rbegin();
            p.erase(p.find(temp));
            total-=temp.total;
            path pth = {((temp.wt/2)+1)/2*temp.leaf, temp.wt/2, temp.leaf};
            p.insert(pth);
            // cout<<total<<" ";
        }
        cout<<cnt<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define str string
#define ch char
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push

int leaves[100001];
int checked[100001];
vector<int> adjlist[100001];

struct path{
    ll total;
    ll wt;
    int leaf;
    friend bool operator<(path a, path b){
        return a.total < b.total;
    }
};

int dfs(int node){
    int sum = 0;
    checked[node] = 1;
    int chk = 0;
    for(int x:adjlist[node]){
        if(checked[x]==0){
            checked[x]=1;
            chk = 1;
            sum+=dfs(x);
        }
    }
    if(chk==0){
        leaves[node] = 1;
        return 1;

    }
    leaves[node] = sum;
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    if (fopen("input.in", "r"))
        freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
    int t;
    cin>>t;
    map<pair<int, int>, ll> weights;
    vector<pair<int, int>> paths;
    multiset<path> p;
    while(t--){
        int n; ll S;
        cin>>n>>S;
        p.clear();
        weights.clear();
        paths.clear();
        for(int i=0; i<n; i++){
            adjlist[i].clear();
            checked[i]=0;
        }
        checked[0] = 1;
        for(int i=0; i<n-1; i++){
            int v, u, w;
            cin>>v>>u>>w;
            v--; u--;
            paths.pb(mp(v, u));
            adjlist[v].pb(u);
            adjlist[u].pb(v);
            weights.insert(mp(mp(v, u), w));
        }
        dfs(0);
        ll total = 0;
        //
        for(int i=0; i<n-1; i++){
            int l = min(leaves[paths[i].first], leaves[paths[i].second]);
            path pth = {((weights[paths[i]]+1)/2)*l, (weights[paths[i]]), l};
            total+=pth.wt * pth.leaf;
            p.insert(pth);
        }
        int cnt = 0;

        while(total>S){
            cnt++;
            path temp = *p.rbegin();
            assert(temp.wt != 0);
            p.erase(p.find(temp));
            total-=temp.total;
            path pth = {((temp.wt/2)+1)/2*temp.leaf, temp.wt/2, temp.leaf};
            p.insert(pth);
            // cout<<total<<" ";
        }
        cout<<cnt<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define str string
#define ch char
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push

int leaves[100001];
int checked[100001];
vector<int> adjlist[100001];

struct path{
    ll total;
    ll wt;
    int leaf;
    friend bool operator<(path a, path b){
        return a.total < b.total;
    }
};

int dfs(int node){
    int sum = 0;
    checked[node] = 1;
    int chk = 0;
    for(int x:adjlist[node]){
        if(checked[x]==0){
            checked[x]=1;
            chk = 1;
            sum+=dfs(x);
        }
    }
    if(chk==0){
        leaves[node] = 1;
        return 1;

    }
    leaves[node] = sum;
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    if (fopen("input.in", "r"))
        freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
    int t;
    cin>>t;
    map<pair<int, int>, ll> weights;
    vector<pair<int, int>> paths;
    multiset<path> p;
    while(t--){
        int n; ll S;
        cin>>n>>S;
        p.clear();
        weights.clear();
        paths.clear();
        for(int i=0; i<n; i++){
            adjlist[i].clear();
            checked[i]=0;
        }
        checked[0] = 1;
        for(int i=0; i<n-1; i++){
            int v, u, w;
            cin>>v>>u>>w;
            v--; u--;
            paths.pb(mp(v, u));
            adjlist[v].pb(u);
            adjlist[u].pb(v);
            weights.insert(mp(mp(v, u), w));
        }
        dfs(0);
        ll total = 0;
        //
        for(int i=0; i<n-1; i++){
            int l = min(leaves[paths[i].first], leaves[paths[i].second]);
            path pth = {((weights[paths[i]]+1)/2)*l, (weights[paths[i]]), l};
            total+=pth.wt * pth.leaf;
            p.insert(pth);
        }
        int cnt = 0;

        while(total>S){
            cnt++;
            path temp = *p.rbegin();
            assert(temp.wt != 0);
            p.erase(p.find(temp));
            total-=temp.total;
            path pth = {((temp.wt/2)+1)/2*temp.leaf, temp.wt/2, temp.leaf};
            p.insert(pth);
            cout<<total<<" ";
        }
        cout<<cnt<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define str string
#define ch char
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push

int leaves[100001];
int checked[100001];
vector<int> adjlist[100001];

struct path{
    ll total;
    int wt;
    int leaf;
    friend bool operator<(path a, path b){
        return a.total < b.total;
    }
};

int dfs(int node){
    int sum = 0;
    checked[node] = 1;
    int chk = 0;
    for(int x:adjlist[node]){
        if(checked[x]==0){
            checked[x]=1;
            chk = 1;
            sum+=dfs(x);
        }
    }
    if(chk==0){
        leaves[node] = 1;
        return 1;

    }
    leaves[node] = sum;
    return sum;
}
int main()
{
    //ios_base::sync_with_stdio(false); cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int n; ll S;
        cin>>n>>S;
        for(int i=0; i<n; i++){
            adjlist[i].clear();
            checked[i]=0;
        }
        checked[0] = 1;
        map<pair<int, int>, ll> weights;
        vector<pair<int, int>> paths;
        for(int i=0; i<n-1; i++){
            int v, u, w;
            cin>>v>>u>>w;
            v--; u--;
            paths.pb(mp(v, u));
            adjlist[v].pb(u);
            adjlist[u].pb(v);
            weights.insert(mp(mp(v, u), w));
        }
        dfs(0);
        set<path> p;
        ll total = 0;

        for(int i=0; i<n-1; i++){
            int l = min(leaves[paths[i].first], leaves[paths[i].second]);
            path pth = {((weights[paths[i]]+1)/2)*l, (weights[paths[i]]), l};
            total+=pth.wt * pth.leaf;
            p.insert(pth);
        }
        int cnt = 0;

        while(total>S){
            cnt++;
            path temp = *p.rbegin();
            p.erase(p.find(temp));
            total-=temp.total;
            path pth = {((temp.wt/2)+1)/2*temp.leaf, temp.wt/2, temp.leaf};
            p.insert(pth);
            // cout<<total<<" ";
        }
        cout<<cnt<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define str string
#define ch char
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push

int leaves[100001];
int checked[100001];
vector<int> adjlist[100001];

struct path{
    ll total;
    ll wt;
    int leaf;
    int id;
    friend bool operator<(path a, path b){
        if(a.total == b.total) return a.id < b.id;
        return a.total < b.total;
    }
};

int dfs(int node){
    int sum = 0;
    checked[node] = 1;
    int chk = 0;
    for(int x:adjlist[node]){
        if(checked[x]==0){
            checked[x]=1;
            chk = 1;
            sum+=dfs(x);
        }
    }
    if(chk==0){
        leaves[node] = 1;
        return 1;

    }
    leaves[node] = sum;
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    if (fopen("input.in", "r"))
        freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
    int t;
    cin>>t;
    map<pair<int, int>, ll> weights;
    vector<pair<int, int>> paths;
    multiset<path> p;
    while(t--){
        int n; ll S;
        cin>>n>>S;
        p.clear();
        weights.clear();
        paths.clear();
        for(int i=0; i<n; i++){
            adjlist[i].clear();
            checked[i]=0;
            leaves[i] = 0;
        }
        checked[0] = 1;
        for(int i=0; i<n-1; i++){
            int v, u, w;
            cin>>v>>u>>w;
            v--; u--;
            paths.pb(mp(v, u));
            adjlist[v].pb(u);
            adjlist[u].pb(v);
            weights.insert(mp(mp(v, u), w));
        }
        dfs(0);
        // for(int i = 0; i< n; i++) cout<<leaves[i]<<" ";
        // cout<<'\n';
        ll total = 0;
        //
        auto calcT = [&] (ll W, int L) {
            return W * L - W/2 * L;
        };
        for(int i=0; i<n-1; i++){
            int l = min(leaves[paths[i].first], leaves[paths[i].second]);
            path pth = {calcT(weights[paths[i]], l), weights[paths[i]], l, i};
            total+=pth.wt * pth.leaf;
            // cerr<<pth.wt<<" "<<pth.id<<"\n";
            p.insert(pth);
        }
        int cnt = 0;
        // cerr<<"\n";
        // cerr<<total<<" "<<p.size()<<"\n";
        //100 + 10 + 123 + 55
        // 50 5 30 27
        //
        while(total>S){
            cnt++;
        // for(auto i : p) cout<<i.wt<<endl;
            path temp = *p.rbegin();
            assert(temp.wt != 0);
            assert(p.find(temp) != p.end());
            // cout<<"f"<<(*p.find(temp)).total<<"\n";
            p.erase(p.find(temp));
            // cerr<<p.size()<<"\n";
            total-=temp.total;
            path pth = {calcT(temp.wt/2, temp.leaf), temp.wt/2, temp.leaf, temp.id};
            // cout<<temp.id<<" "<<pth.total<<" "<<pth.wt<<endl;
            p.insert(pth);
            // cerr<<temp.wt<<" "<<pth.wt<<" "<<temp.id<<" "<<p.size()<<" "<<total<<endl;
        }
        // cout<<endl<<endl;
        // cerr<<endl<<endl;
        cout<<cnt<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define str string
#define ch char
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push

int leaves[100001];
int checked[100001];
vector<int> adjlist[100001];

struct path{
    ll total;
    ll wt;
    int leaf;
    friend bool operator<(path a, path b){
        return a.total < b.total;
    }
};

int dfs(int node){
    int sum = 0;
    checked[node] = 1;
    int chk = 0;
    for(int x:adjlist[node]){
        if(checked[x]==0){
            checked[x]=1;
            chk = 1;
            sum+=dfs(x);
        }
    }
    if(chk==0){
        leaves[node] = 1;
        return 1;

    }
    leaves[node] = sum;
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    if (fopen("input.in", "r"))
        freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
    int t;
    cin>>t;
    map<pair<int, int>, ll> weights;
    vector<pair<int, int>> paths;
    multiset<path> p;
    while(t--){
        int n; ll S;
        cin>>n>>S;
        p.clear();
        weights.clear();
        paths.clear();
        for(int i=0; i<n; i++){
            adjlist[i].clear();
            checked[i]=0;
            leaves[i] = 0;
        }
        checked[0] = 1;
        for(int i=0; i<n-1; i++){
            int v, u, w;
            cin>>v>>u>>w;
            v--; u--;
            paths.pb(mp(v, u));
            adjlist[v].pb(u);
            adjlist[u].pb(v);
            weights.insert(mp(mp(v, u), w));
        }
        dfs(0);
        ll total = 0;
        //
        for(int i=0; i<n-1; i++){
            int l = min(leaves[paths[i].first], leaves[paths[i].second]);
            path pth = {((weights[paths[i]]+1)/2)*l, (weights[paths[i]]), l};
            total+=pth.wt * pth.leaf;
            p.insert(pth);
        }
        int cnt = 0;

        while(total>S){
            cnt++;
            path temp = *p.rbegin();
            assert(temp.wt != 0);
            p.erase(p.find(temp));
            total-=temp.total;
            path pth = {((temp.wt/2)+1)/2*temp.leaf, temp.wt/2, temp.leaf};
            p.insert(pth);
            // cout<<total<<" ";
        }
        cout<<cnt<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define str string
#define ch char
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push

int leaves[100001];
int checked[100001];
vector<int> adjlist[100001];

struct path{
    ll total;
    ll wt;
    int leaf;
    friend bool operator<(path a, path b){
        return a.total < b.total;
    }
};

int dfs(int node){
    int sum = 0;
    checked[node] = 1;
    int chk = 0;
    for(int x:adjlist[node]){
        if(checked[x]==0){
            checked[x]=1;
            chk = 1;
            sum+=dfs(x);
        }
    }
    if(chk==0){
        leaves[node] = 1;
        return 1;

    }
    leaves[node] = sum;
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    if (fopen("input.in", "r"))
        freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
    int t;
    cin>>t;
    map<pair<int, int>, ll> weights;
    vector<pair<int, int>> paths;
    multiset<path> p;
    while(t--){
        int n; ll S;
        cin>>n>>S;
        p.clear();
        weights.clear();
        paths.clear();
        for(int i=0; i<n; i++){
            adjlist[i].clear();
            checked[i]=0;
            leaves[i] = 0;
        }
        checked[0] = 1;
        for(int i=0; i<n-1; i++){
            int v, u, w;
            cin>>v>>u>>w;
            v--; u--;
            paths.pb(mp(v, u));
            adjlist[v].pb(u);
            adjlist[u].pb(v);
            weights.insert(mp(mp(v, u), w));
        }
        dfs(0);
        // for(int i = 0; i< n; i++) cout<<leaves[i]<<" ";
        // cout<<'\n';
        ll total = 0;
        //
        auto calcT = [&] (ll W, int L) {
            return W * L - W/2 * L;
        };
        for(int i=0; i<n-1; i++){
            int l = min(leaves[paths[i].first], leaves[paths[i].second]);
            path pth = {calcT(weights[paths[i]], l), weights[paths[i]], l};
            total+=pth.wt * pth.leaf;
            p.insert(pth);
        }
        int cnt = 0;

        while(total>S){
            cnt++;
            path temp = *p.rbegin();
            assert(temp.wt != 0);
            p.erase(p.find(temp));
            total-=temp.total;
            path pth = {calcT(temp.wt/2, temp.leaf), temp.wt/2, temp.leaf};
            p.insert(pth);
            // cout<<total<<" ";
        }
        cout<<cnt<<"\n";
    }
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <complex>
#include <algorithm>
#include <functional>
#include <fstream>
#include <numeric>
#include <string>
#include <valarray>
#include <deque>
#define int long long

using namespace std;

typedef pair<int,int> Pair;

template<class t>
ostream & operator << (ostream & tout,const vector<t> &s){
  tout<<'[';
  for (int i=0;i<s.size();i++)
    if (i+1 == s.size())
      tout<<s[i];
    else
      tout<<s[i]<<',';
  tout<<']';
  return(tout);
}

template<class A,class B,class C>
struct tuple{
  A x;
  B y;
  C z;
  bool operator < (const tuple &s)const{
	if (x!=s.x) return(x < s.x);
	if (y!=s.y) return(y < s.y);
	return(z < s.z);
  }
  tuple(A a,B b,C c){
	x=a;
	y=b;
	z=c;
  }
};

template <class Type,class Int=unsigned int,Int End=250000,Type Default=0>
class add_sum_tree{
  class node{
  public:
    Type Itself,Itschilds;
    node *next[2];
    node(){ Itself=0,Itschilds=0; next[0]=next[1]=0;
    }
    node* child(int s){
      if (!next[s]) next[s]=new node();
      return(next[s]);
    }
  }*root;
public:
  add_sum_tree(){ root=new node(); root->Itself=Default;
  }
  void add(Int where,const Type &s){
    Int A=0,B=End;
    node *temp=root;
    Int Mid;
    while (B>where){
      temp->Itschilds+=(where-A+1)*s;
      Mid=(A+B)/2;
      if (Mid >= where){
	temp=temp->child(0);
	B=Mid;
      }else{
	temp->child(0)->Itself+=s;
	temp=temp->child(1);
	A=Mid+1;
      }
    }
    temp->Itself+=s;
  }
  void add(Int a,Int b,const Type &s){
    add(b,s);
    if (a) add(a-1,Type(0)-s);
  }
  Type sum(Int where){
    Int A=0,B=End,Mid;
    Type ans=0;
    node *temp=root;
    while (B>where){
      ans+=temp->Itself*(where-A+1);
      Mid=(A+B)/2;
      if (Mid >= where){
	temp=temp->child(0);
	B=Mid;
      }else{
	if (temp->next[0])
	  ans+=(temp->next[0]->Itself)*(Mid-A+1)+temp->next[0]->Itschilds;
	temp=temp->child(1);
	A=Mid+1;
      }
    }
    ans+=temp->Itschilds+temp->Itself*(B-A+1);
    return(ans);
  }
  Type sum(Int a,Int b){
    if (a) return(sum(b)-sum(a-1));
    return(sum(b));
  }
};

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

vector<int> e[MAX_N],w[MAX_N];
vector<int> mai,cycle,glo;
bool mark[MAX_N],isCycle[MAX_N];
int n,start[MAX_N],end[MAX_N];
vector<triple> add;

void dfs(int s,int t){
  mark[s]=true;
  glo.push_back(s);
  start[s]=glo.size()-1;
  for (int i=0;i<e[s].size();i++)
    if (!mark[e[s][i]] && !isCycle[e[s][i]])
      dfs(e[s][i],w[s][i]);
  end[s]=glo.size()-1;
  if (t){
    add.push_back(triple(0,start[s]-1,t*(end[s]-start[s]+1)));
    add.push_back(triple(end[s]+1,n-1,t*(end[s]-start[s]+1)));
    add.push_back(triple(start[s],end[s],t*(n-(end[s]-start[s]+1))));
  }
}

void find_cycle(int s){
  if (cycle.size())
    return;
  mai.push_back(s);
  mark[s]=true;

  for (int i=0;i<e[s].size();i++){
    if (cycle.size())
      return;
    if (mark[e[s][i]]){
      if (e[s][i] != mai[mai.size()-2]){
	while (mai.size()){
	  cycle.push_back(mai.back());
	  mai.pop_back();
	  if (cycle.back() == e[s][i])
	    break;
	}
	return;
      }
    }else
      find_cycle(e[s][i]);
  }
  mai.pop_back();
}

vector<int> d,zarib;

vector<int> rev(vector<int> s){
  reverse(s.begin(),s.end());
  return(s);
}

int rev(int s){
  return(d.size()-s-1);
}

struct S1{
  vector<int> sum;
  S1(vector<int> a){
    sum.push_back(a[0]);
    for (int i=1;i<a.size();i++)
      sum.push_back(sum.back()+a[i]);
  }
  int get(int s,int t){
    if (s == 0)
      s = sum.size()-1;
    else
      s--;
    if (t == 0)
      t = sum.size()-1;
    else
      t--;
    if (s > t)
      return(sum[t]+sum.back()-sum[s]);
    else
      return(sum[t]-sum[s]);
  }
};

const int N4=400;

struct S2{
  vector<int> A,B;
  vector<int> s4,sum4,Ns4;
  S2(vector<int> a,vector<int> b){
    A=a;
    B=b;
    s4.resize(A.size());
    sum4.resize(A.size());
    Ns4.resize(A.size());
    for (int i=0;i<s4.size();i++){
      int ad=0,pointer=i;
      for (int j=0;j<N4;j++){
	s4[i]+=ad*B[pointer];
	ad+=A[pointer];
	Ns4[i]+=B[pointer];
	pointer=(pointer+1)%A.size();
      }
      sum4[i]=ad;
    }
  }
  int get(int a,int b){
    int ad=0,pointer=a,sum=0;
    while (true){

      if (pointer+N4 <= A.size() && (pointer > b || (pointer < b && pointer+N4 < b))){
	sum+=ad*Ns4[pointer]+s4[pointer];
	ad+=sum4[pointer];
	pointer=(pointer+N4)%A.size();
	continue;
      }
      if (pointer > b && (pointer+N4)%A.size() < b){
	sum+=ad*Ns4[pointer]+s4[pointer];
	ad+=sum4[pointer];
	pointer=(pointer+N4)%A.size();
	continue;
      }

      sum+=ad*B[pointer];
      ad+=A[pointer];
      if (pointer == b)
	break;
      pointer=(pointer+1)%A.size();
    }
    return(sum);
  }
};

vector<int> prev(vector<int> s){
  for (int i=s.size()-1;i>0;i--)
    swap(s[i],s[i-1]);
  return(s);
}

vector<int> cal(){
  S1 s1(d);
  S2 s2(d,zarib);
  S2 s3(rev(prev(d)),rev(zarib));
  vector<int> result;
  int pointer=0;
  int all=accumulate(d.begin(),d.end(),0ll);
  for (int i=0;i<d.size();i++){
    while ((pointer+1)%d.size() != i && s1.get(i,(pointer+1)%d.size()) <= all/2)
      pointer=(pointer+1)%d.size();
    result.push_back(s2.get(i,pointer)+s3.get(rev(i),rev((pointer+1)%d.size())));
//     cout<<i<<' '<<pointer<<' '<<s2.get(i,pointer)<<' '<<s3.get(rev(i),rev((pointer+1)%d.size()))<<endl;
    if (i == pointer)
      pointer=(pointer+1)%d.size();
  }
//   cout<<d<<' '<<zarib<<endl;
//   cout<<result<<endl;
  return(result);
}

#undef int
int main(){
#define int long long
  ios_base::sync_with_stdio(false) ;
  cin>>n;
  for (int i=1;i<=n;i++){
    int a,b,c;
    cin>>a>>b>>c;
    e[a].push_back(b);
    w[a].push_back(c);
    e[b].push_back(a);
    w[b].push_back(c);
  }

  find_cycle(1);
  fill(mark+1,mark+n+1,false);
  for (int i=0;i<cycle.size();i++)
    isCycle[cycle[i]]=true;
  for (int i=0;i<cycle.size();i++)
    dfs(cycle[i],0);

//   cout<<glo<<endl;
//   for (int i=0;i<add.size();i++)
//     cout<<add[i].x<<' '<<add[i].y<<' '<<add[i].z<<endl;
  map<Pair,int> mp;
  for (int i=1;i<=n;i++)
    for (int j=0;j<e[i].size();j++)
      mp[Pair(i,e[i][j])]=w[i][j];

  for (int i=0;i<cycle.size();i++){
    d.push_back(mp[Pair(cycle[i],cycle[(i+1)%cycle.size()])]);
    zarib.push_back(end[cycle[i]]-start[cycle[i]]+1);
  }
  vector<int> result=cal();
  for (int i=0;i<result.size();i++)
    add.push_back(triple(start[cycle[i]],end[cycle[i]],result[i]));
//   for (int i=0;i<add.size();i++)
//     cout<<add[i].x<<' '<<add[i].y<<' '<<add[i].z<<endl;

  add_sum_tree<int> mai;
  for (int i=0;i<add.size();i++)
    mai.add(add[i].x,add[i].y,add[i].z);
  vector<int> ret(n+1);
  for (int i=0;i<glo.size();i++)
    ret[glo[i]]=mai.sum(i,i);
  for (int i=1;i<=n;i++)
    cout<<ret[i]<<' ';
  cout<<endl;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <complex>
#include <algorithm>
#include <functional>
#include <fstream>
#include <numeric>
#include <string>
#include <valarray>
#include <deque>
#define int long long

using namespace std;

typedef pair<int,int> Pair;

template<class t>
ostream & operator << (ostream & tout,const vector<t> &s){
  tout<<'[';
  for (int i=0;i<s.size();i++)
    if (i+1 == s.size())
      tout<<s[i];
    else
      tout<<s[i]<<',';
  tout<<']';
  return(tout);
}

template<class A,class B,class C>
struct tuple{
  A x;
  B y;
  C z;
  bool operator < (const tuple &s)const{
	if (x!=s.x) return(x < s.x);
	if (y!=s.y) return(y < s.y);
	return(z < s.z);
  }
  tuple(A a,B b,C c){
	x=a;
	y=b;
	z=c;
  }
};

template <class Type,class Int=unsigned int,Int End=500000,Type Default=0>
class add_sum_tree{
  class node{
  public:
    Type Itself,Itschilds;
    node *next[2];
    node(){ Itself=0,Itschilds=0; next[0]=next[1]=0;
    }
    node* child(int s){
      if (!next[s]) next[s]=new node();
      return(next[s]);
    }
  }*root;
public:
  add_sum_tree(){ root=new node(); root->Itself=Default;
  }
  void add(Int where,const Type &s){
    Int A=0,B=End;
    node *temp=root;
    Int Mid;
    while (B>where){
      temp->Itschilds+=(where-A+1)*s;
      Mid=(A+B)/2;
      if (Mid >= where){
	temp=temp->child(0);
	B=Mid;
      }else{
	temp->child(0)->Itself+=s;
	temp=temp->child(1);
	A=Mid+1;
      }
    }
    temp->Itself+=s;
  }
  void add(Int a,Int b,const Type &s){
    add(b,s);
    if (a) add(a-1,Type(0)-s);
  }
  Type sum(Int where){
    Int A=0,B=End,Mid;
    Type ans=0;
    node *temp=root;
    while (B>where){
      ans+=temp->Itself*(where-A+1);
      Mid=(A+B)/2;
      if (Mid >= where){
	temp=temp->child(0);
	B=Mid;
      }else{
	if (temp->next[0])
	  ans+=(temp->next[0]->Itself)*(Mid-A+1)+temp->next[0]->Itschilds;
	temp=temp->child(1);
	A=Mid+1;
      }
    }
    ans+=temp->Itschilds+temp->Itself*(B-A+1);
    return(ans);
  }
  Type sum(Int a,Int b){
    if (a) return(sum(b)-sum(a-1));
    return(sum(b));
  }
};

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

vector<int> e[MAX_N],w[MAX_N];
vector<int> mai,cycle,glo;
bool mark[MAX_N],isCycle[MAX_N];
int n,start[MAX_N],end[MAX_N];
vector<triple> add;

void dfs(int s,int t){
  mark[s]=true;
  glo.push_back(s);
  start[s]=glo.size()-1;
  for (int i=0;i<e[s].size();i++)
    if (!mark[e[s][i]] && !isCycle[e[s][i]])
      dfs(e[s][i],w[s][i]);
  end[s]=glo.size()-1;
  if (t){
    add.push_back(triple(0,start[s]-1,t*(end[s]-start[s]+1)));
    add.push_back(triple(end[s]+1,n-1,t*(end[s]-start[s]+1)));
    add.push_back(triple(start[s],end[s],t*(n-(end[s]-start[s]+1))));
  }
}

void find_cycle(int s){
  if (cycle.size())
    return;
  mai.push_back(s);
  mark[s]=true;

  for (int i=0;i<e[s].size();i++){
    if (cycle.size())
      return;
    if (mark[e[s][i]]){
      if (e[s][i] != mai[mai.size()-2]){
	while (mai.size()){
	  cycle.push_back(mai.back());
	  mai.pop_back();
	  if (cycle.back() == e[s][i])
	    break;
	}
	return;
      }
    }else
      find_cycle(e[s][i]);
  }
  mai.pop_back();
}

vector<int> d,zarib;

vector<int> rev(vector<int> s){
  reverse(s.begin(),s.end());
  return(s);
}

int rev(int s){
  return(d.size()-s-1);
}

struct S1{
  vector<int> sum;
  S1(vector<int> a){
    sum.push_back(a[0]);
    for (int i=1;i<a.size();i++)
      sum.push_back(sum.back()+a[i]);
  }
  int get(int s,int t){
    if (s == 0)
      s = sum.size()-1;
    else
      s--;
    if (t == 0)
      t = sum.size()-1;
    else
      t--;
    if (s > t)
      return(sum[t]+sum.back()-sum[s]);
    else
      return(sum[t]-sum[s]);
  }
};

const int N4=400;

struct S2{
  vector<int> A,B;
  vector<int> s4,sum4,Ns4;
  S2(vector<int> a,vector<int> b){
    A=a;
    B=b;
    s4.resize(A.size());
    sum4.resize(A.size());
    Ns4.resize(A.size());
    for (int i=0;i<s4.size();i++)
      if (i+N4 <= a.size()){
	int ad=0;
	for (int j=0;j<N4;j++){
	  s4[i]+=ad*B[i+j];
	  ad+=A[i+j];
	  Ns4[i]+=B[i+j];
	}
	sum4[i]=ad;
      }
  }
  int get(int a,int b){
    int ad=0,pointer=a,sum=0;
    while (true){

      if (pointer+N4 <= A.size() && (pointer > b || (pointer < b && pointer+N4 < b))){
	sum+=ad*Ns4[pointer]+s4[pointer];
	ad+=sum4[pointer];
	pointer=(pointer+N4)%A.size();
	continue;
      }

      sum+=ad*B[pointer];
      ad+=A[pointer];
      if (pointer == b)
	break;
      pointer=(pointer+1)%A.size();
    }
    return(sum);
  }
};

vector<int> prev(vector<int> s){
  for (int i=s.size()-1;i>0;i--)
    swap(s[i],s[i-1]);
  return(s);
}

vector<int> cal(){
  S1 s1(d);
  S2 s2(d,zarib);
  S2 s3(rev(prev(d)),rev(zarib));
  vector<int> result;
  int pointer=0;
  int all=accumulate(d.begin(),d.end(),0ll);
  for (int i=0;i<d.size();i++){
    while ((pointer+1)%d.size() != i && s1.get(i,(pointer+1)%d.size()) <= all/2)
      pointer=(pointer+1)%d.size();
    result.push_back(s2.get(i,pointer)+s3.get(rev(i),rev((pointer+1)%d.size())));
//     cout<<i<<' '<<pointer<<' '<<s2.get(i,pointer)<<' '<<s3.get(rev(i),rev((pointer+1)%d.size()))<<endl;
    if (i == pointer)
      pointer=(pointer+1)%d.size();
  }
//   cout<<d<<' '<<zarib<<endl;
//   cout<<result<<endl;
  return(result);
}

#undef int
int main(){
#define int long long
  ios_base::sync_with_stdio(false) ;
  cin>>n;
  for (int i=1;i<=n;i++){
    int a,b,c;
    cin>>a>>b>>c;
    e[a].push_back(b);
    w[a].push_back(c);
    e[b].push_back(a);
    w[b].push_back(c);
  }

  find_cycle(1);
  fill(mark+1,mark+n+1,false);
  for (int i=0;i<cycle.size();i++)
    isCycle[cycle[i]]=true;
  for (int i=0;i<cycle.size();i++)
    dfs(cycle[i],0);

//   cout<<glo<<endl;
//   for (int i=0;i<add.size();i++)
//     cout<<add[i].x<<' '<<add[i].y<<' '<<add[i].z<<endl;
  map<Pair,int> mp;
  for (int i=1;i<=n;i++)
    for (int j=0;j<e[i].size();j++)
      mp[Pair(i,e[i][j])]=w[i][j];

  for (int i=0;i<cycle.size();i++){
    d.push_back(mp[Pair(cycle[i],cycle[(i+1)%cycle.size()])]);
    zarib.push_back(end[cycle[i]]-start[cycle[i]]+1);
  }
  vector<int> result=cal();
  for (int i=0;i<result.size();i++)
    add.push_back(triple(start[cycle[i]],end[cycle[i]],result[i]));
//   for (int i=0;i<add.size();i++)
//     cout<<add[i].x<<' '<<add[i].y<<' '<<add[i].z<<endl;

  add_sum_tree<int> mai;
  for (int i=0;i<add.size();i++)
    mai.add(add[i].x,add[i].y,add[i].z);
  vector<int> ret(n+1);
  for (int i=0;i<glo.size();i++)
    ret[glo[i]]=mai.sum(i,i);
  for (int i=1;i<=n;i++)
    cout<<ret[i]<<' ';
  cout<<endl;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <complex>
#include <algorithm>
#include <functional>
#include <fstream>
#include <numeric>
#include <string>
#include <valarray>
#include <deque>
#define int long long

using namespace std;

typedef pair<int,int> Pair;

template<class t>
ostream & operator << (ostream & tout,const vector<t> &s){
  tout<<'[';
  for (int i=0;i<s.size();i++)
    if (i+1 == s.size())
      tout<<s[i];
    else
      tout<<s[i]<<',';
  tout<<']';
  return(tout);
}

template<class A,class B,class C>
struct tuple{
  A x;
  B y;
  C z;
  bool operator < (const tuple &s)const{
	if (x!=s.x) return(x < s.x);
	if (y!=s.y) return(y < s.y);
	return(z < s.z);
  }
  tuple(A a,B b,C c){
	x=a;
	y=b;
	z=c;
  }
};

template <class Type,class Int=unsigned int,Int End=500000,Type Default=0>
class add_sum_tree{
  class node{
  public:
    Type Itself,Itschilds;
    node *next[2];
    node(){ Itself=0,Itschilds=0; next[0]=next[1]=0;
    }
    node* child(int s){
      if (!next[s]) next[s]=new node();
      return(next[s]);
    }
  }*root;
public:
  add_sum_tree(){ root=new node(); root->Itself=Default;
  }
  void add(Int where,const Type &s){
    Int A=0,B=End;
    node *temp=root;
    Int Mid;
    while (B>where){
      temp->Itschilds+=(where-A+1)*s;
      Mid=(A+B)/2;
      if (Mid >= where){
	temp=temp->child(0);
	B=Mid;
      }else{
	temp->child(0)->Itself+=s;
	temp=temp->child(1);
	A=Mid+1;
      }
    }
    temp->Itself+=s;
  }
  void add(Int a,Int b,const Type &s){
    add(b,s);
    if (a) add(a-1,Type(0)-s);
  }
  Type sum(Int where){
    Int A=0,B=End,Mid;
    Type ans=0;
    node *temp=root;
    while (B>where){
      ans+=temp->Itself*(where-A+1);
      Mid=(A+B)/2;
      if (Mid >= where){
	temp=temp->child(0);
	B=Mid;
      }else{
	if (temp->next[0])
	  ans+=(temp->next[0]->Itself)*(Mid-A+1)+temp->next[0]->Itschilds;
	temp=temp->child(1);
	A=Mid+1;
      }
    }
    ans+=temp->Itschilds+temp->Itself*(B-A+1);
    return(ans);
  }
  Type sum(Int a,Int b){
    if (a) return(sum(b)-sum(a-1));
    return(sum(b));
  }
};

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

vector<int> e[MAX_N],w[MAX_N];
vector<int> mai,cycle,glo;
bool mark[MAX_N],isCycle[MAX_N];
int n,start[MAX_N],end[MAX_N];
vector<triple> add;

void dfs(int s,int t){
  mark[s]=true;
  glo.push_back(s);
  start[s]=glo.size()-1;
  for (int i=0;i<e[s].size();i++)
    if (!mark[e[s][i]] && !isCycle[e[s][i]])
      dfs(e[s][i],w[s][i]);
  end[s]=glo.size()-1;
  if (t){
    add.push_back(triple(0,start[s]-1,t*(end[s]-start[s]+1)));
    add.push_back(triple(end[s]+1,n-1,t*(end[s]-start[s]+1)));
    add.push_back(triple(start[s],end[s],t*(n-(end[s]-start[s]+1))));
  }
}

void find_cycle(int s){
  if (cycle.size())
    return;
  mai.push_back(s);
  mark[s]=true;

  for (int i=0;i<e[s].size();i++){
    if (cycle.size())
      return;
    if (mark[e[s][i]]){
      if (e[s][i] != mai[mai.size()-2]){
	while (mai.size()){
	  cycle.push_back(mai.back());
	  mai.pop_back();
	  if (cycle.back() == e[s][i])
	    break;
	}
	return;
      }
    }else
      find_cycle(e[s][i]);
  }
  mai.pop_back();
}

vector<int> d,zarib;

vector<int> rev(vector<int> s){
  reverse(s.begin(),s.end());
  return(s);
}

int rev(int s){
  return(d.size()-s-1);
}

struct S1{
  vector<int> sum;
  S1(vector<int> a){
    sum.push_back(a[0]);
    for (int i=1;i<a.size();i++)
      sum.push_back(sum.back()+a[i]);
  }
  int get(int s,int t){
    if (s == 0)
      s = sum.size()-1;
    else
      s--;
    if (t == 0)
      t = sum.size()-1;
    else
      t--;
    if (s > t)
      return(sum[t]+sum.back()-sum[s]);
    else
      return(sum[t]-sum[s]);
  }
};

const int N4=400;

struct S2{
  vector<int> A,B;
  vector<int> s4,sum4;
  S2(vector<int> a,vector<int> b){
    A=a;
    B=b;
    s4.resize(A.size());
    sum4.resize(A.size());
    for (int i=0;i<s4.size();i++)
      if (i+N4 <= a.size()){
	int ad=0;
	for (int j=0;j<N4;j++){
	  s4[i]+=ad*B[i+j];
	  ad+=A[i+j];
	}
	sum4[i]=ad;
      }
  }
  int get(int a,int b){
    int ad=0,pointer=a,sum=0;
    while (true){

//       if (pointer+N4 <= A.size() && (pointer > b || (pointer < b && pointer+N4 < b))){
// 	sum+=ad*N4+s4[pointer];
// 	ad+=sum4[pointer];
// 	pointer=(pointer+N4)%A.size();
// 	continue;
//       }

      sum+=ad*B[pointer];
      ad+=A[pointer];
      if (pointer == b)
	break;
      pointer=(pointer+1)%A.size();
    }
    return(sum);
  }
};

vector<int> prev(vector<int> s){
  for (int i=s.size()-1;i>0;i--)
    swap(s[i],s[i-1]);
  return(s);
}

vector<int> cal(){
  S1 s1(d);
  S2 s2(d,zarib);
  S2 s3(rev(prev(d)),rev(zarib));
  vector<int> result;
  int pointer=0;
  int all=accumulate(d.begin(),d.end(),0ll);
  for (int i=0;i<d.size();i++){
    while ((pointer+1)%d.size() != i && s1.get(i,(pointer+1)%d.size()) <= all/2)
      pointer=(pointer+1)%d.size();
    result.push_back(s2.get(i,pointer)+s3.get(rev(i),rev((pointer+1)%d.size())));
//     cout<<i<<' '<<pointer<<' '<<s2.get(i,pointer)<<' '<<s3.get(rev(i),rev((pointer+1)%d.size()))<<endl;
    if (i == pointer)
      pointer=(pointer+1)%d.size();
  }
//   cout<<d<<' '<<zarib<<endl;
//   cout<<result<<endl;
  return(result);
}

#undef int
int main(){
#define int long long
  ios_base::sync_with_stdio(false) ;
  cin>>n;
  for (int i=1;i<=n;i++){
    int a,b,c;
    cin>>a>>b>>c;
    e[a].push_back(b);
    w[a].push_back(c);
    e[b].push_back(a);
    w[b].push_back(c);
  }

  find_cycle(1);
  fill(mark+1,mark+n+1,false);
  for (int i=0;i<cycle.size();i++)
    isCycle[cycle[i]]=true;
  for (int i=0;i<cycle.size();i++)
    dfs(cycle[i],0);

//   cout<<glo<<endl;
//   for (int i=0;i<add.size();i++)
//     cout<<add[i].x<<' '<<add[i].y<<' '<<add[i].z<<endl;
  map<Pair,int> mp;
  for (int i=1;i<=n;i++)
    for (int j=0;j<e[i].size();j++)
      mp[Pair(i,e[i][j])]=w[i][j];

  for (int i=0;i<cycle.size();i++){
    d.push_back(mp[Pair(cycle[i],cycle[(i+1)%cycle.size()])]);
    zarib.push_back(end[cycle[i]]-start[cycle[i]]+1);
  }
  vector<int> result=cal();
  for (int i=0;i<result.size();i++)
    add.push_back(triple(start[cycle[i]],end[cycle[i]],result[i]));
//   for (int i=0;i<add.size();i++)
//     cout<<add[i].x<<' '<<add[i].y<<' '<<add[i].z<<endl;

  add_sum_tree<int> mai;
  for (int i=0;i<add.size();i++)
    mai.add(add[i].x,add[i].y,add[i].z);
  vector<int> ret(n+1);
  for (int i=0;i<glo.size();i++)
    ret[glo[i]]=mai.sum(i,i);
  for (int i=1;i<=n;i++)
    cout<<ret[i]<<' ';
  cout<<endl;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <complex>
#include <algorithm>
#include <functional>
#include <fstream>
#include <numeric>
#include <string>
#include <valarray>
#include <deque>
#define int long long

using namespace std;

typedef pair<int,int> Pair;

template<class t>
ostream & operator << (ostream & tout,const vector<t> &s){
  tout<<'[';
  for (int i=0;i<s.size();i++)
    if (i+1 == s.size())
      tout<<s[i];
    else
      tout<<s[i]<<',';
  tout<<']';
  return(tout);
}

template<class A,class B,class C>
struct tuple{
  A x;
  B y;
  C z;
  bool operator < (const tuple &s)const{
	if (x!=s.x) return(x < s.x);
	if (y!=s.y) return(y < s.y);
	return(z < s.z);
  }
  tuple(A a,B b,C c){
	x=a;
	y=b;
	z=c;
  }
};

template <class Type,class Int=unsigned int,Int End=500000,Type Default=0>
class add_sum_tree{
  class node{
  public:
    Type Itself,Itschilds;
    node *next[2];
    node(){ Itself=0,Itschilds=0; next[0]=next[1]=0;
    }
    node* child(int s){
      if (!next[s]) next[s]=new node();
      return(next[s]);
    }
  }*root;
public:
  add_sum_tree(){ root=new node(); root->Itself=Default;
  }
  void add(Int where,const Type &s){
    Int A=0,B=End;
    node *temp=root;
    Int Mid;
    while (B>where){
      temp->Itschilds+=(where-A+1)*s;
      Mid=(A+B)/2;
      if (Mid >= where){
	temp=temp->child(0);
	B=Mid;
      }else{
	temp->child(0)->Itself+=s;
	temp=temp->child(1);
	A=Mid+1;
      }
    }
    temp->Itself+=s;
  }
  void add(Int a,Int b,const Type &s){
    add(b,s);
    if (a) add(a-1,Type(0)-s);
  }
  Type sum(Int where){
    Int A=0,B=End,Mid;
    Type ans=0;
    node *temp=root;
    while (B>where){
      ans+=temp->Itself*(where-A+1);
      Mid=(A+B)/2;
      if (Mid >= where){
	temp=temp->child(0);
	B=Mid;
      }else{
	if (temp->next[0])
	  ans+=(temp->next[0]->Itself)*(Mid-A+1)+temp->next[0]->Itschilds;
	temp=temp->child(1);
	A=Mid+1;
      }
    }
    ans+=temp->Itschilds+temp->Itself*(B-A+1);
    return(ans);
  }
  Type sum(Int a,Int b){
    if (a) return(sum(b)-sum(a-1));
    return(sum(b));
  }
};

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

vector<int> e[MAX_N],w[MAX_N];
vector<int> mai,cycle,glo;
bool mark[MAX_N],isCycle[MAX_N];
int n,start[MAX_N],end[MAX_N];
vector<triple> add;

void dfs(int s,int t){
  mark[s]=true;
  glo.push_back(s);
  start[s]=glo.size()-1;
  for (int i=0;i<e[s].size();i++)
    if (!mark[e[s][i]] && !isCycle[e[s][i]])
      dfs(e[s][i],w[s][i]);
  end[s]=glo.size()-1;
  if (t){
    add.push_back(triple(0,start[s]-1,t*(end[s]-start[s]+1)));
    add.push_back(triple(end[s]+1,n-1,t*(end[s]-start[s]+1)));
    add.push_back(triple(start[s],end[s],t*(n-(end[s]-start[s]+1))));
  }
}

void find_cycle(int s){
  if (cycle.size())
    return;
  mai.push_back(s);
  mark[s]=true;

  for (int i=0;i<e[s].size();i++){
    if (cycle.size())
      return;
    if (mark[e[s][i]]){
      if (e[s][i] != mai[mai.size()-2]){
	while (mai.size()){
	  cycle.push_back(mai.back());
	  mai.pop_back();
	  if (cycle.back() == e[s][i])
	    break;
	}
	return;
      }
    }else
      find_cycle(e[s][i]);
  }
  mai.pop_back();
}

deque<int> d,zarib;

void next(deque<int> &s){
  int temp=s.front();
  s.pop_front();
  s.push_back(temp);
}

vector<int> cal(){
  vector<int> result;
  int all=0;
  for (int i=0;i<d.size();i++)
    all+=d[i];
  for (int count=0;count<d.size();count++){
    int sum=0,ad=0;
    for (int i=0;i<d.size();i++){
      sum+=min(ad,all-ad)*zarib[i];
      ad+=d[i];
    }
    result.push_back(sum);
    next(d);
    next(zarib);
  }
  return(result);
}

#undef int
int main(){
#define int long long
  ios_base::sync_with_stdio(false) ;
  cin>>n;
  for (int i=1;i<=n;i++){
    int a,b,c;
    cin>>a>>b>>c;
    e[a].push_back(b);
    w[a].push_back(c);
    e[b].push_back(a);
    w[b].push_back(c);
  }

  find_cycle(1);
  fill(mark+1,mark+n+1,false);
  for (int i=0;i<cycle.size();i++)
    isCycle[cycle[i]]=true;
  for (int i=0;i<cycle.size();i++)
    dfs(cycle[i],0);

//   cout<<glo<<endl;
//   for (int i=0;i<add.size();i++)
//     cout<<add[i].x<<' '<<add[i].y<<' '<<add[i].z<<endl;
  map<Pair,int> mp;
  for (int i=1;i<=n;i++)
    for (int j=0;j<e[i].size();j++)
      mp[Pair(i,e[i][j])]=w[i][j];

  for (int i=0;i<cycle.size();i++){
    d.push_back(mp[Pair(cycle[i],cycle[(i+1)%cycle.size()])]);
    zarib.push_back(end[cycle[i]]-start[cycle[i]]+1);
  }
  vector<int> result=cal();
  for (int i=0;i<result.size();i++)
    add.push_back(triple(start[glo[i]],end[glo[i]],result[i]));

//   for (int i=0;i<add.size();i++)
//     cout<<add[i].x<<' '<<add[i].y<<' '<<add[i].z<<endl;

  add_sum_tree<int> mai;
  for (int i=0;i<add.size();i++)
    mai.add(add[i].x,add[i].y,add[i].z);
  vector<int> ret(n+1);
  for (int i=0;i<glo.size();i++)
    ret[glo[i]]=mai.sum(i,i);
  for (int i=1;i<=n;i++)
    cout<<ret[i]<<' ';
  cout<<endl;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <complex>
#include <algorithm>
#include <functional>
#include <fstream>
#include <numeric>
#include <string>
#include <valarray>
#include <deque>
#define int long long

using namespace std;

typedef pair<int,int> Pair;

template<class t>
ostream & operator << (ostream & tout,const vector<t> &s){
  tout<<'[';
  for (int i=0;i<s.size();i++)
    if (i+1 == s.size())
      tout<<s[i];
    else
      tout<<s[i]<<',';
  tout<<']';
  return(tout);
}

template<class A,class B,class C>
struct tuple{
  A x;
  B y;
  C z;
  bool operator < (const tuple &s)const{
	if (x!=s.x) return(x < s.x);
	if (y!=s.y) return(y < s.y);
	return(z < s.z);
  }
  tuple(A a,B b,C c){
	x=a;
	y=b;
	z=c;
  }
};

template <class Type,class Int=unsigned int,Int End=250000,Type Default=0>
class add_sum_tree{
  class node{
  public:
    Type Itself,Itschilds;
    node *next[2];
    node(){ Itself=0,Itschilds=0; next[0]=next[1]=0;
    }
    node* child(int s){
      if (!next[s]) next[s]=new node();
      return(next[s]);
    }
  }*root;
public:
  add_sum_tree(){ root=new node(); root->Itself=Default;
  }
  void add(Int where,const Type &s){
    Int A=0,B=End;
    node *temp=root;
    Int Mid;
    while (B>where){
      temp->Itschilds+=(where-A+1)*s;
      Mid=(A+B)/2;
      if (Mid >= where){
	temp=temp->child(0);
	B=Mid;
      }else{
	temp->child(0)->Itself+=s;
	temp=temp->child(1);
	A=Mid+1;
      }
    }
    temp->Itself+=s;
  }
  void add(Int a,Int b,const Type &s){
    add(b,s);
    if (a) add(a-1,Type(0)-s);
  }
  Type sum(Int where){
    Int A=0,B=End,Mid;
    Type ans=0;
    node *temp=root;
    while (B>where){
      ans+=temp->Itself*(where-A+1);
      Mid=(A+B)/2;
      if (Mid >= where){
	temp=temp->child(0);
	B=Mid;
      }else{
	if (temp->next[0])
	  ans+=(temp->next[0]->Itself)*(Mid-A+1)+temp->next[0]->Itschilds;
	temp=temp->child(1);
	A=Mid+1;
      }
    }
    ans+=temp->Itschilds+temp->Itself*(B-A+1);
    return(ans);
  }
  Type sum(Int a,Int b){
    if (a) return(sum(b)-sum(a-1));
    return(sum(b));
  }
};

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

vector<int> e[MAX_N],w[MAX_N];
vector<int> mai,cycle,glo;
bool mark[MAX_N],isCycle[MAX_N];
int n,start[MAX_N],end[MAX_N];
vector<triple> add;

void dfs(int s,int t){
  mark[s]=true;
  glo.push_back(s);
  start[s]=glo.size()-1;
  for (int i=0;i<e[s].size();i++)
    if (!mark[e[s][i]] && !isCycle[e[s][i]])
      dfs(e[s][i],w[s][i]);
  end[s]=glo.size()-1;
  if (t){
    add.push_back(triple(0,start[s]-1,t*(end[s]-start[s]+1)));
    add.push_back(triple(end[s]+1,n-1,t*(end[s]-start[s]+1)));
    add.push_back(triple(start[s],end[s],t*(n-(end[s]-start[s]+1))));
  }
}

void find_cycle(int s){
  if (cycle.size())
    return;
  mai.push_back(s);
  mark[s]=true;

  for (int i=0;i<e[s].size();i++){
    if (cycle.size())
      return;
    if (mark[e[s][i]]){
      if (e[s][i] != mai[mai.size()-2]){
	while (mai.size()){
	  cycle.push_back(mai.back());
	  mai.pop_back();
	  if (cycle.back() == e[s][i])
	    break;
	}
	return;
      }
    }else
      find_cycle(e[s][i]);
  }
  mai.pop_back();
}

vector<int> d,zarib;

vector<int> rev(vector<int> s){
  reverse(s.begin(),s.end());
  return(s);
}

int rev(int s){
  return(d.size()-s-1);
}

struct S1{
  vector<int> sum;
  S1(vector<int> a){
    sum.push_back(a[0]);
    for (int i=1;i<a.size();i++)
      sum.push_back(sum.back()+a[i]);
  }
  int get(int s,int t){
    if (s == 0)
      s = sum.size()-1;
    else
      s--;
    if (t == 0)
      t = sum.size()-1;
    else
      t--;
    if (s > t)
      return(sum[t]+sum.back()-sum[s]);
    else
      return(sum[t]-sum[s]);
  }
};

const int N4=400;

int getSum(const vector<int> &a,int x,int y){
  if (x == 0)
    return(a[y]);
  else
    return(a[y]-a[x-1]);
}

struct S2{
  vector<int> A,B;
  vector<int> g,p,q;
  S2(vector<int> a,vector<int> b){
    A=a;
    B=b;
    p.push_back(B[0]);
    for (int i=1;i<b.size();i++)
      p.push_back(p.back()+B[i]);
    q.push_back(A[0]);
    for (int i=1;i<A.size();i++)
      q.push_back(q.back()+A[i]);
    int ad=0;
    g.push_back(0);
    for (int i=1;i<A.size();i++)
      g.push_back(g.back()+q[i-1]*b[i]);
  }
  int get(int a,int b){
    if (a == 0)
      return(g[b]);
    if (a <= b)
      return(getSum(g,a,b)-q[a-1]*getSum(p,a,b));
    return(get(a,A.size()-1)+get(0,b)+getSum(q,a,B.size()-1)*getSum(p,0,b));
  }
};

vector<int> prev(vector<int> s){
  for (int i=s.size()-1;i>0;i--)
    swap(s[i],s[i-1]);
  return(s);
}

vector<int> cal(){
  S1 s1(d);
  S2 s2(d,zarib);
  S2 s3(rev(prev(d)),rev(zarib));
  vector<int> result;
  int pointer=0;
  int all=accumulate(d.begin(),d.end(),0ll);
  for (int i=0;i<d.size();i++){
    while ((pointer+1)%d.size() != i && s1.get(i,(pointer+1)%d.size()) <= all/2)
      pointer=(pointer+1)%d.size();
    result.push_back(s2.get(i,pointer)+s3.get(rev(i),rev((pointer+1)%d.size())));
//     cout<<i<<' '<<pointer<<' '<<s2.get(i,pointer)<<' '<<s3.get(rev(i),rev((pointer+1)%d.size()))<<endl;
    if (i == pointer)
      pointer=(pointer+1)%d.size();
  }
//   cout<<d<<' '<<zarib<<endl;
//   cout<<result<<endl;
  return(result);
}

#undef int
int main(){
#define int long long
  ios_base::sync_with_stdio(false) ;
  cin>>n;
  for (int i=1;i<=n;i++){
    int a,b,c;
    cin>>a>>b>>c;
    e[a].push_back(b);
    w[a].push_back(c);
    e[b].push_back(a);
    w[b].push_back(c);
  }

  find_cycle(1);
  fill(mark+1,mark+n+1,false);
  for (int i=0;i<cycle.size();i++)
    isCycle[cycle[i]]=true;
  for (int i=0;i<cycle.size();i++)
    dfs(cycle[i],0);

//   cout<<glo<<endl;
//   for (int i=0;i<add.size();i++)
//     cout<<add[i].x<<' '<<add[i].y<<' '<<add[i].z<<endl;
  map<Pair,int> mp;
  for (int i=1;i<=n;i++)
    for (int j=0;j<e[i].size();j++)
      mp[Pair(i,e[i][j])]=w[i][j];

  for (int i=0;i<cycle.size();i++){
    d.push_back(mp[Pair(cycle[i],cycle[(i+1)%cycle.size()])]);
    zarib.push_back(end[cycle[i]]-start[cycle[i]]+1);
  }
  vector<int> result=cal();
  for (int i=0;i<result.size();i++)
    add.push_back(triple(start[cycle[i]],end[cycle[i]],result[i]));
//   for (int i=0;i<add.size();i++)
//     cout<<add[i].x<<' '<<add[i].y<<' '<<add[i].z<<endl;

  add_sum_tree<int> mai;
  for (int i=0;i<add.size();i++)
    mai.add(add[i].x,add[i].y,add[i].z);
  vector<int> ret(n+1);
  for (int i=0;i<glo.size();i++)
    ret[glo[i]]=mai.sum(i,i);
  for (int i=1;i<=n;i++)
    cout<<ret[i]<<' ';
  cout<<endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,n) for (int i=1;i<=n;++i)
typedef long long ll;
const int N=600005;
int n,m,E,x,y,z,son[N],ed[N],next[N],data[N],fa[N],a[N],c[N],d[N],p[N],q[N];
ll s[N],f[N],g[N];
bool circle(int x)
{
	for (int p=son[x];p;p=next[p]){
		int y=ed[p];
		if (p!=fa[x]){
			if (fa[y]){
				d[++m]=data[p];
				for (int i=x;i!=y;i=ed[fa[i]]) a[m]=i,d[++m]=data[fa[i]];
				a[m]=y; return 1;
			}else fa[y]=p^1; if (circle(y)) return 1;
		}
	}
	return 0;
}
void dp(int x)
{
	c[x]=1,s[x]=0;
	for (int p=son[x];p;p=next[p]){
		int y=ed[p];
		if (!fa[y]) fa[y]=x,dp(y),c[x]+=c[y],s[x]+=s[y]+c[y]*data[p];
	}
}
void dfs(int x)
{
	for (int p=son[x];p;p=next[p]){
		int y=ed[p];
		if (fa[y]>0 && fa[x]!=y)
			s[y]=s[x]+(n-c[y]*2)*data[p],dfs(y);
	}
}
int main()
{
	scanf("%d",&n),E=1;
	rep(i,n) scanf("%d%d%d",&x,&y,&z),
		ed[++E]=y,data[E]=z,next[E]=son[x],son[x]=E,
		ed[++E]=x,data[E]=z,next[E]=son[y],son[y]=E;
	fa[1]=-1,circle(1); rep(i,n) fa[i]=0; rep(i,m) fa[a[i]]=-1;
	rep(i,m) a[m+m+i]=a[m+i]=a[i],d[m+m+i]=d[m+i]=d[i],dp(a[i]);
	for (int i=m*3;i;--i) q[i]=q[i+1]+c[a[i]],g[i]=g[i+1]+s[a[i]]+q[i+1]*d[i+1];
	rep(i,m*3) p[i]=p[i-1]+c[a[i]],f[i]=f[i-1]+s[a[i]]+p[i-1]*d[i],d[i]+=d[i-1];
	int w=1;
	for (int i=m;i<m+m;++i){
		while (d[i]-d[w]>d[w+m]-d[i]) ++w; int l=w-1,r=w+m;
		s[a[i]]=f[i]-f[l]-1LL*p[l]*(d[i]-d[l])+g[i]-g[r]-1LL*q[r]*(d[r]-d[i])-s[a[i]];
	}
	rep(i,m) dfs(a[i]); rep(i,n) printf("%I64d ",s[i]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define IN(x,y) ((y).find((x))!=(y).end())
#define DBG(vari) cout<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

#define N 200007
vector<pii> adj[N];
bool seen[N];
ll res[N];
vector<pii> cycle;
int end, n;

int dfs (int v, int p) {
   if (seen[v]++) {
      end = v;
      return 1;
   } else {
      FOREACH(x,adj[v]) if (x->fi != p) {
         if (dfs(x->fi,v)) {
            if (cycle.empty() || cycle.back().fi != end) cycle.pb(mp(v,x->se));
            return 1;
         }
      }
   }
   return 0;
}

ll size[N], totalCost[N], distToRoot[N], root[N];

void dig (int v, int p1, int p2, int dist, int rt) {
   size[v] = 1;
   totalCost[v] = 0;
   distToRoot[v] = dist;
   root[v] = rt;
   FOREACH(x,adj[v]) if (x->fi != p1 && x->fi != p2) {
      dig(x->fi,v,-1,dist + x->se,rt);
      size[v] += size[x->fi];
      totalCost[v] += size[x->fi] * x->se + totalCost[x->fi];
   }
}
void compute (int v, int p1, int p2) {
   FOREACH(x,adj[v]) if (x->fi != p1 && x->fi != p2) {
      int w = x->fi;
      res[w] = res[v] + (n - 2*size[w]) * x->se;
      compute(w,v,-1);
   }
}

int main () {
   scanf("%d", &n);
   FOR(i,n) {
      wez3(a,b,c)
      adj[a].pb(mp(b,c));
      adj[b].pb(mp(a,c));
   }
   dfs(1,-1);
   reverse(ALL(cycle));
   int m = SZ(cycle);
   ll baseCost = 0;
   FOR(u,m) {
      dig(cycle[u].fi,cycle[(u+1)%m].fi,cycle[(u+m-1)%m].fi,0,cycle[u].fi);
      baseCost += totalCost[cycle[u].fi];
   }

   // oblicz dla r
   int r = cycle[0].fi;
   int cycleLen = 0;
   FOREACH(x,cycle) cycleLen += x->se;
   res[r] = baseCost;
   ll straightCost = 0; // tymczasowa
   ll numOfStraights = 1;
   ll sumOfSizesOverStraights = size[r];
   ll distToLastStraight = 0;
   REP(u,1,m-1) {
      straightCost += cycle[u-1].se;
      #define backwardCost (cycleLen - straightCost)
      if (backwardCost >= straightCost) {
         numOfStraights++;
         sumOfSizesOverStraights += size[cycle[u].fi];
         distToLastStraight = straightCost;
      }
      res[r] += size[cycle[u].fi] * min(straightCost, backwardCost);
   }

   // oblicz dla innych rootów
   REP(u,1,m-1) {
      res[cycle[u].fi] = res[cycle[u-1].fi];
      numOfStraights--; // u-1 wypada
      distToLastStraight -= cycle[u-1].se;
      sumOfSizesOverStraights -= size[cycle[u-1].fi];
      res[cycle[u].fi] -= sumOfSizesOverStraights * cycle[u-1].se;
      res[cycle[u].fi] += (n - sumOfSizesOverStraights) * cycle[u-1].se;
      straightCost = distToLastStraight;
      while (numOfStraights < m) {
         int cand = (u + numOfStraights) % m;
         straightCost += cycle[(cand-1+m)%m].se;
         if (backwardCost >= straightCost) {
            numOfStraights++;
            sumOfSizesOverStraights += size[cycle[cand].fi];
            distToLastStraight = straightCost;
            res[cycle[u].fi] += (straightCost - backwardCost) * size[cycle[cand].fi];
         } else {
            break;
         }
      }
   }

   // oblicz dla reszty
   FOR(u,m) compute(cycle[u].fi,cycle[(u+1)%m].fi,cycle[(u+m-1)%m].fi);

   FORI(i,n) printf("%I64d ", res[i]);
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 200010

using namespace std;

typedef long long ll;

int st[maxn],aim[maxn<<1],nxt[maxn<<1],ln;
int w[maxn<<1];
int root[maxn];
int n;
void in_edge(int x,int y,int z){
	aim[ln]=y;
	nxt[ln]=st[x];
	w[ln]=z;
	st[x]=ln++;
}
int stack[maxn],next[maxn];
void dfs(int R){
	int stn=1;
	stack[1]=R;
	next[R]=st[R];
	root[R]=0;
	while (stn){
		int k=stack[stn],i;
		for (i=next[k];i!=-1;i=nxt[i]){
			int v=aim[i];
			if (stn>1 && v==stack[stn-1]) continue;
			if (root[v]==-1){
				next[k]=nxt[i];
				next[v]=st[v];
				root[v]=0;
				stack[++stn]=v;
				break;
			}else
			if (root[v]==0){
				for (int j=stn;j>0 && stack[j]!=v;j--){
					root[stack[j]]=1;
				}
				root[v]=1;
			}
		}
		if (i==-1){
			--stn;
		}
	}
}
int q[maxn];
int tag[maxn];
int tot[maxn];
ll f[maxn];
void bfs(int sp){
	int qn=1;
	tag[sp]=1;
	q[0]=sp;
	for (int p=0;p<qn;p++)
		for (int i=st[q[p]];i!=-1;i=nxt[i])
			if (!tag[aim[i]] && !root[aim[i]])
				tag[q[qn++]=aim[i]]=1;
	for (int p=qn-1;p>=0;p--){
		int k=q[p];
		f[k]=0;
		tag[k]=0;
		tot[k]=1;
		for (int i=st[k];i!=-1;i=nxt[i])
			if (!tag[aim[i]] && !root[aim[i]]){
				int v=aim[i];
				tot[k]+=tot[v];
				f[k]+=f[v];
				f[k]+=(ll)tot[v]*w[i];
			}
	}
}
int a[maxn<<1];
int aw[maxn<<1];
ll ts[maxn<<1],ss[maxn<<1],ws[maxn<<1];
ll ts2[maxn<<1],ss2[maxn<<1],ws2[maxn<<1];
void Make_cir(int x){
	int an=1;
	a[1]=x;
	int p=x;
	while (1){
		for (int i=st[p];i!=-1;i=nxt[i])
			if (root[aim[i]] && (an==1 || aim[i]!=a[an-1])){
				aw[an]=w[i];
				a[++an]=aim[i];
				p=aim[i];
				break;
			}
		if (p==x){
			--an;
			break;
		}
	}
	for (int i=1;i<=an;i++){
		a[an+i]=a[i];
		aw[an+i]=aw[i];
	}
	ll tf=0;
	for (int i=1;i<=an;i++) tf+=f[a[i]];

	ss[0]=0;
	for (int i=1;i<=(an<<1);i++) ss[i]=ss[i-1]+aw[i];
	ts[0]=0;
	for (int i=1;i<=(an<<1);i++) ts[i]=ts[i-1]+tot[a[i]];

	ss2[(an<<1)+1]=0;
	for (int i=(an<<1);i>0;i--) ss2[i]=ss2[i+1]+aw[i];
	ts2[(an<<1)+1]=0;
	for (int i=(an<<1);i>0;i--) ts2[i]=ts2[i+1]+tot[a[i]];

	ws[0]=0;
	for (int i=1;i<=(an<<1);i++) ws[i]=ws[i-1]+ts2[i+1]*aw[i];
	ws2[(an<<1)+1]=0;
	for (int i=(an<<1);i>0;i--) ws2[i]=ws2[i+1]+ts[i]*aw[i];

	for (int i=1,p=1;i<=an;i++){
		while (p<i+an && ss[p]-ss[i-1]<=ss2[p+1]-ss2[i+an]) ++p;
		int x=a[i];
		f[x]+=tf-f[x];
		if (p>i){
			f[x]+=ws[p-1]-ws[i-1]-ts2[p+1]*(ss[p-1]-ss[i-1]);
		}
		if (p<i+an-1){
			f[x]+=ws2[p+1]-ws2[i+an]-ts[p]*(ss2[p+1]-ss2[i+an]);
		}
	}
//	for (int i=0;i<n;i++) printf("i=%d f=%lld\n",i,f[i]);
}
void bfs2(int sp){
	int qn=1;
	q[0]=sp;
	tag[sp]=1;
	for (int p=0;p<qn;p++)
		for (int i=st[q[p]];i!=-1;i=nxt[i])
			if (!tag[aim[i]] && !root[aim[i]])
				tag[q[qn++]=aim[i]]=1;
	for (int p=0;p<qn;p++){
		int k=q[p];
		tag[k]=0;
		for (int i=st[k];i!=-1;i=nxt[i])
			if (tag[aim[i]] && !root[aim[i]]){
				int v=aim[i];
				ll tmp=f[k]-f[v]-(ll)w[i]*tot[v];
				f[v]+=tmp+(ll)w[i]*(n-tot[v]);
			}
	}
}
int main(){
	scanf("%d",&n);
	memset(st,-1,sizeof(st));
	ln=0;
	for (int i=0;i<n;i++){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		--u,--v;
		in_edge(u,v,w);
		in_edge(v,u,w);
	}
	memset(root,-1,sizeof(root));
	for (int i=0;i<n;i++)
		if (root[i]==-1) dfs(i);
	memset(tag,0,sizeof(tag));
	for (int i=0;i<n;i++)
		if (root[i]) bfs(i);
	for (int i=0;i<n;i++)
		if (root[i]){
			Make_cir(i);
			break;
		}
	for (int i=0;i<n;i++)
		if (root[i]) bfs2(i);
	for (int i=0;i<n;i++) printf("%lld ",f[i]);
	puts("");
	return 0;
}