#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("%I64d ",f[i]);
	puts("");
	return 0;
}


#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;
	memset(tag,0,sizeof(tag));
	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;
	memset(tag,0,sizeof(tag));
	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=qn-1;p>=0;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]-tot[v]*w[i];
				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);
	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;
}


#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <iostream>
#include <algorithm>
#define LL long long
#define pi 3.1415926535897932384626433
#define sqr(a) ((a)*(a))

using namespace std;

const int maxn = 501010;

vector<int> q;
vector< pair<int, LL> > adj[maxn];
//
int u[maxn], d[maxn];
//
LL size[maxn], D[maxn], TreeAns[maxn];
//
int Cycle[maxn];
LL Dis[maxn], Len = 0LL;
//
LL SumSize[maxn], Sum[maxn], SumDis[maxn];
//
LL ans[maxn], Candy;
int n, tot, Cur;  //tot -> number of cycle nodes

void dfs(int x)
{
    u[x] = 1;
    size[x] = 1LL; TreeAns[x] = 0LL;
    for (int i = 0; i < adj[x].size(); i ++)
    {
        int j = adj[x][i].first;
        if (! u[j] && d[j] == 1)
        {
            D[j] = D[x] + adj[x][i].second;
            dfs(j);
            size[x] += size[j];
            TreeAns[x] += adj[x][i].second * size[j] + TreeAns[j];
        }
    }
}

void dp(int x, int Rt)
{
    u[x] = 1;
    for (int i = 0; i < adj[x].size(); i ++)
    {
        int j = adj[x][i].first;
        if (! u[j] && d[j] == 1)
        {
            TreeAns[j] = TreeAns[x];
            TreeAns[j] += (size[Rt] - size[j]) * adj[x][i].second;
            TreeAns[j] -= size[j] * adj[x][i].second;
            ans[j] = TreeAns[j] + (ans[Rt] - TreeAns[Rt]);
            ans[j] += ((LL) n - size[Rt]) * D[j];
            dp(j, Rt);
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
#endif
    scanf("%d", &n);
    //init d[] & adj[]
    for (int i = 1; i <= n; i ++)
        adj[i].clear();
    memset(d, 0, sizeof(d));
    //input edges
    for (int i = 1; i <= n; i ++)
    {
        int x, y; LL w;
        scanf("%d%d%I64d", &x, &y, &w);
        adj[x].push_back(make_pair(y, w));
        adj[y].push_back(make_pair(x, w));
        d[x] ++; d[y] ++;
    }
    //top - sort
    q.clear();
    for (int i = 1; i <= n; i ++)
        if (d[i] == 1) q.push_back(i);
    for (int f = 0; f < q.size(); f ++)
    {
        int x = q[f];
        for (int i = 0; i < adj[x].size(); i ++)
        {
            if (d[adj[x][i].first] == 1) continue;
            d[adj[x][i].first] --;
            if (d[adj[x][i].first] == 1)
                q.push_back(adj[x][i].first);
        }
    }
    //make the cycle
    for (int i = 1; i <= n; i ++)
        if (d[i] > 1)
        {
            tot = 1; Cycle[tot] = i;
            break;
        }
    for (int i = 0; i < adj[Cycle[1]].size(); i ++)
        if (d[adj[Cycle[1]][i].first] > 1)
        {
            Dis[tot] = adj[Cycle[1]][i].second;
            Cycle[++ tot] = adj[Cycle[1]][i].first;
            break;
        }
    for ( ; ; )
    {
        int cur = Cycle[tot], next;
        for (int i = 0; i < adj[cur].size(); i ++)
        {
            int j = adj[cur][i].first;
            if (d[j] > 1 && j != Cycle[tot - 1])
            {
                Dis[tot] = adj[cur][i].second;
                next = j;
                break;
            }
        }
        if (next == Cycle[1]) break;
        Cycle[++ tot] = next;
    }
    for (int i = tot + 1; i < tot * 2; i ++)
        Cycle[i] = Cycle[i - tot], Dis[i] = Dis[i - tot];
    tot *= 2; tot --;
    //make size[], D[], TreeAns[]
    memset(u, 0, sizeof(u));
    for (int i = 1; i <= tot / 2 + 1; i ++)
        D[Cycle[i]] = 0, dfs(Cycle[i]);
    //Length of the cycle
    for (int i = 1; i <= tot / 2 + 1; i ++)
        Len += Dis[i];
    memset(ans, 0, sizeof(ans));
    //1 -> tot
    memset(SumSize, 0, sizeof(SumSize));
    for (int i = tot; i; i --)
        SumSize[i] = SumSize[i + 1] + size[Cycle[i]];
    memset(SumDis, 0, sizeof(SumDis));
    for (int i = tot - 1; i; i --)
        SumDis[i] = SumDis[i + 1] + Dis[i];
    memset(Sum, 0, sizeof(Sum));
    for (int i = tot - 1; i; i --)
        Sum[i] = Sum[i + 1] + Dis[i] * SumSize[i + 1];
    //go
    Cur = 1;
    for (int i = 1; i <= tot / 2 + 1; i ++)
    {
        while (Cur < tot && (SumDis[i] - SumDis[Cur + 1]) * 2LL < Len)
            Cur ++;
        ans[Cycle[i]] += Sum[i] - Sum[Cur];
        ans[Cycle[i]] -= (SumDis[i] - SumDis[Cur]) * SumSize[Cur + 1];
    }
    //tot -> 1
    memset(SumSize, 0, sizeof(SumSize));
    for (int i = 1; i <= tot; i ++)
        SumSize[i] = SumSize[i - 1] + size[Cycle[i]];
    memset(SumDis, 0, sizeof(SumDis));
    for (int i = 2; i <= tot; i ++)
        SumDis[i] = SumDis[i - 1] + Dis[i - 1];
    memset(Sum, 0, sizeof(Sum));
    for (int i = 2; i <= tot; i ++)
        Sum[i] = Sum[i - 1] + Dis[i - 1] * SumSize[i - 1];
    //go
    Cur = tot;
    for (int i = tot; i > tot / 2; i --)
    {
        while (Cur > 1 && (SumDis[i] - SumDis[Cur - 1]) * 2LL <= Len)
            Cur --;
        ans[Cycle[i]] += Sum[i] - Sum[Cur];
        ans[Cycle[i]] -= (SumDis[i] - SumDis[Cur]) * SumSize[Cur - 1];
    }
    //Sum of TreeAns
    Candy = 0LL;
    for (int i = 1; i <= tot / 2 + 1; i ++)
        Candy += TreeAns[Cycle[i]];
    for (int i = 1; i <= tot / 2 + 1; i ++)
        ans[Cycle[i]] += Candy;
    //others
    memset(u, 0, sizeof(u));
    for (int i = 1; i <= tot / 2 + 1; i ++)
        dp(Cycle[i], Cycle[i]);
    //print
    for (int i = 1; i <= n; i ++)
    {
        if (i > 1) printf(" ");
        printf("%I64d", ans[i]);
    }
    printf("\n");
    return 0;
}


#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <iostream>
#include <algorithm>
#define LL long long
#define pi 3.1415926535897932384626433
#define sqr(a) ((a)*(a))

using namespace std;

const int maxn = 501010;

vector<int> q;
vector< pair<int, LL> > adj[maxn];
//
int u[maxn], d[maxn];
//
LL size[maxn], D[maxn], TreeAns[maxn];
//
int Cycle[maxn];
LL Dis[maxn], Len = 0LL;
//
LL SumSize[maxn], Sum[maxn], SumDis[maxn];
//
LL ans[maxn];
int n, tot, Cur, Candy;  //tot -> number of cycle nodes

void dfs(int x)
{
    u[x] = 1;
    size[x] = 1LL; TreeAns[x] = 0LL;
    for (int i = 0; i < adj[x].size(); i ++)
    {
        int j = adj[x][i].first;
        if (! u[j] && d[j] == 1)
        {
            D[j] = D[x] + adj[x][i].second;
            dfs(j);
            size[x] += size[j];
            TreeAns[x] += adj[x][i].second * size[j] + TreeAns[j];
        }
    }
}

void dp(int x, int Rt)
{
    u[x] = 1;
    for (int i = 0; i < adj[x].size(); i ++)
    {
        int j = adj[x][i].first;
        if (! u[j] && d[j] == 1)
        {
            TreeAns[j] = TreeAns[x];
            TreeAns[j] += (size[Rt] - size[j]) * adj[x][i].second;
            TreeAns[j] -= size[j] * adj[x][i].second;
            ans[j] = TreeAns[j] + (ans[Rt] - TreeAns[Rt]);
            ans[j] += ((LL) n - size[Rt]) * D[j];
            dp(j, Rt);
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
#endif
    scanf("%d", &n);
    //init d[] & adj[]
    for (int i = 1; i <= n; i ++)
        adj[i].clear();
    memset(d, 0, sizeof(d));
    //input edges
    for (int i = 1; i <= n; i ++)
    {
        int x, y; LL w;
        scanf("%d%d%I64d", &x, &y, &w);
        adj[x].push_back(make_pair(y, w));
        adj[y].push_back(make_pair(x, w));
        d[x] ++; d[y] ++;
    }
    //top - sort
    q.clear();
    for (int i = 1; i <= n; i ++)
        if (d[i] == 1) q.push_back(i);
    for (int f = 0; f < q.size(); f ++)
    {
        int x = q[f];
        for (int i = 0; i < adj[x].size(); i ++)
        {
            if (d[adj[x][i].first] == 1) continue;
            d[adj[x][i].first] --;
            if (d[adj[x][i].first] == 1)
                q.push_back(adj[x][i].first);
        }
    }
    //make the cycle
    for (int i = 1; i <= n; i ++)
        if (d[i] > 1)
        {
            tot = 1; Cycle[tot] = i;
            break;
        }
    for (int i = 0; i < adj[Cycle[1]].size(); i ++)
        if (d[adj[Cycle[1]][i].first] > 1)
        {
            Dis[tot] = adj[Cycle[1]][i].second;
            Cycle[++ tot] = adj[Cycle[1]][i].first;
            break;
        }
    for ( ; ; )
    {
        int cur = Cycle[tot], next;
        for (int i = 0; i < adj[cur].size(); i ++)
        {
            int j = adj[cur][i].first;
            if (d[j] > 1 && j != Cycle[tot - 1])
            {
                Dis[tot] = adj[cur][i].second;
                next = j;
                break;
            }
        }
        if (next == Cycle[1]) break;
        Cycle[++ tot] = next;
    }
    for (int i = tot + 1; i < tot * 2; i ++)
        Cycle[i] = Cycle[i - tot], Dis[i] = Dis[i - tot];
    tot *= 2; tot --;
    //make size[], D[], TreeAns[]
    memset(u, 0, sizeof(u));
    for (int i = 1; i <= tot / 2 + 1; i ++)
        D[Cycle[i]] = 0, dfs(Cycle[i]);
    //Length of the cycle
    for (int i = 1; i <= tot / 2 + 1; i ++)
        Len += Dis[i];
    memset(ans, 0, sizeof(ans));
    //1 -> tot
    memset(SumSize, 0, sizeof(SumSize));
    for (int i = tot; i; i --)
        SumSize[i] = SumSize[i + 1] + size[Cycle[i]];
    memset(SumDis, 0, sizeof(SumDis));
    for (int i = tot - 1; i; i --)
        SumDis[i] = SumDis[i + 1] + Dis[i];
    memset(Sum, 0, sizeof(Sum));
    for (int i = tot - 1; i; i --)
        Sum[i] = Sum[i + 1] + Dis[i] * SumSize[i + 1];
    //go
    Cur = 1;
    for (int i = 1; i <= tot / 2 + 1; i ++)
    {
        while (Cur < tot && (SumDis[i] - SumDis[Cur + 1]) * 2LL < Len)
            Cur ++;
        ans[Cycle[i]] += Sum[i] - Sum[Cur];
        ans[Cycle[i]] -= (SumDis[i] - SumDis[Cur]) * SumSize[Cur + 1];
    }
    //tot -> 1
    memset(SumSize, 0, sizeof(SumSize));
    for (int i = 1; i <= tot; i ++)
        SumSize[i] = SumSize[i - 1] + size[Cycle[i]];
    memset(SumDis, 0, sizeof(SumDis));
    for (int i = 2; i <= tot; i ++)
        SumDis[i] = SumDis[i - 1] + Dis[i - 1];
    memset(Sum, 0, sizeof(Sum));
    for (int i = 2; i <= tot; i ++)
        Sum[i] = Sum[i - 1] + Dis[i - 1] * SumSize[i - 1];
    //go
    Cur = tot;
    for (int i = tot; i > tot / 2; i --)
    {
        while (Cur > 1 && (SumDis[i] - SumDis[Cur - 1]) * 2LL <= Len)
            Cur --;
        ans[Cycle[i]] += Sum[i] - Sum[Cur];
        ans[Cycle[i]] -= (SumDis[i] - SumDis[Cur]) * SumSize[Cur - 1];
    }
    //Sum of TreeAns
    Candy = 0LL;
    for (int i = 1; i <= tot / 2 + 1; i ++)
        Candy += TreeAns[Cycle[i]];
    for (int i = 1; i <= tot / 2 + 1; i ++)
        ans[Cycle[i]] += Candy;
    //others
    memset(u, 0, sizeof(u));
    for (int i = 1; i <= tot / 2 + 1; i ++)
        dp(Cycle[i], Cycle[i]);
    //print
    for (int i = 1; i <= n; i ++)
    {
        if (i > 1) printf(" ");
        printf("%d", ans[i]);
    }
    printf("\n");
    return 0;
}


#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <iostream>
#include <algorithm>
#define LL long long
#define pi 3.1415926535897932384626433
#define sqr(a) ((a)*(a))

using namespace std;

const int maxn = 501010;

vector<int> q;
vector< pair<int, LL> > adj[maxn];
//
int u[maxn], d[maxn];
//
LL size[maxn], D[maxn], TreeAns[maxn];
//
int Cycle[maxn];
LL Dis[maxn], Len = 0LL;
//
LL SumSize[maxn], Sum[maxn], SumDis[maxn];
//
LL ans[maxn], Candy;
int n, tot, Cur;  //tot -> number of cycle nodes

void dfs(int x)
{
    u[x] = 1;
    size[x] = 1LL; TreeAns[x] = 0LL;
    for (int i = 0; i < adj[x].size(); i ++)
    {
        int j = adj[x][i].first;
        if (! u[j] && d[j] == 1)
        {
            D[j] = D[x] + adj[x][i].second;
            dfs(j);
            size[x] += size[j];
            TreeAns[x] += adj[x][i].second * size[j] + TreeAns[j];
        }
    }
}

void dp(int x, int Rt)
{
    u[x] = 1;
    for (int i = 0; i < adj[x].size(); i ++)
    {
        int j = adj[x][i].first;
        if (! u[j] && d[j] == 1)
        {
            TreeAns[j] = TreeAns[x];
            TreeAns[j] += (size[Rt] - size[j]) * adj[x][i].second;
            TreeAns[j] -= size[j] * adj[x][i].second;
            ans[j] = TreeAns[j] + (ans[Rt] - TreeAns[Rt]);
            ans[j] += ((LL) n - size[Rt]) * D[j];
            dp(j, Rt);
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
#endif
    scanf("%d", &n);
    //init d[] & adj[]
    for (int i = 1; i <= n; i ++)
        adj[i].clear();
    memset(d, 0, sizeof(d));
    //input edges
    for (int i = 1; i <= n; i ++)
    {
        int x, y; LL w;
        scanf("%d%d%I64d", &x, &y, &w);
        adj[x].push_back(make_pair(y, w));
        adj[y].push_back(make_pair(x, w));
        d[x] ++; d[y] ++;
    }
    //top - sort
    q.clear();
    for (int i = 1; i <= n; i ++)
        if (d[i] == 1) q.push_back(i);
    for (int f = 0; f < q.size(); f ++)
    {
        int x = q[f];
        for (int i = 0; i < adj[x].size(); i ++)
        {
            if (d[adj[x][i].first] == 1) continue;
            d[adj[x][i].first] --;
            if (d[adj[x][i].first] == 1)
                q.push_back(adj[x][i].first);
        }
    }
    //make the cycle
    for (int i = 1; i <= n; i ++)
        if (d[i] > 1)
        {
            tot = 1; Cycle[tot] = i;
            break;
        }
    for (int i = 0; i < adj[Cycle[1]].size(); i ++)
        if (d[adj[Cycle[1]][i].first] > 1)
        {
            Dis[tot] = adj[Cycle[1]][i].second;
            Cycle[++ tot] = adj[Cycle[1]][i].first;
            break;
        }
    for ( ; ; )
    {
        int cur = Cycle[tot], next;
        for (int i = 0; i < adj[cur].size(); i ++)
        {
            int j = adj[cur][i].first;
            if (d[j] > 1 && j != Cycle[tot - 1])
            {
                Dis[tot] = adj[cur][i].second;
                next = j;
                break;
            }
        }
        if (next == Cycle[1]) break;
        Cycle[++ tot] = next;
    }
    for (int i = tot + 1; i < tot * 2; i ++)
        Cycle[i] = Cycle[i - tot], Dis[i] = Dis[i - tot];
    tot *= 2; tot --;
    //make size[], D[], TreeAns[]
    memset(u, 0, sizeof(u));
    for (int i = 1; i <= tot / 2 + 1; i ++)
        D[Cycle[i]] = 0, dfs(Cycle[i]);
    //Length of the cycle
    for (int i = 1; i <= tot / 2 + 1; i ++)
        Len += Dis[i];
    memset(ans, 0, sizeof(ans));
    //1 -> tot
    memset(SumSize, 0, sizeof(SumSize));
    for (int i = tot; i; i --)
        SumSize[i] = SumSize[i + 1] + size[Cycle[i]];
    memset(SumDis, 0, sizeof(SumDis));
    for (int i = tot - 1; i; i --)
        SumDis[i] = SumDis[i + 1] + Dis[i];
    memset(Sum, 0, sizeof(Sum));
    for (int i = tot - 1; i; i --)
        Sum[i] = Sum[i + 1] + Dis[i] * SumSize[i + 1];
    //go
    Cur = 1;
    for (int i = 1; i <= tot / 2 + 1; i ++)
    {
        while (Cur < tot && (SumDis[i] - SumDis[Cur + 1]) * 2LL < Len)
            Cur ++;
        ans[Cycle[i]] += Sum[i] - Sum[Cur];
        ans[Cycle[i]] -= (SumDis[i] - SumDis[Cur]) * SumSize[Cur + 1];
    }
    //tot -> 1
    memset(SumSize, 0, sizeof(SumSize));
    for (int i = 1; i <= tot; i ++)
        SumSize[i] = SumSize[i - 1] + size[Cycle[i]];
    memset(SumDis, 0, sizeof(SumDis));
    for (int i = 2; i <= tot; i ++)
        SumDis[i] = SumDis[i - 1] + Dis[i - 1];
    memset(Sum, 0, sizeof(Sum));
    for (int i = 2; i <= tot; i ++)
        Sum[i] = Sum[i - 1] + Dis[i - 1] * SumSize[i - 1];
    //go
    Cur = tot;
    for (int i = tot; i > tot / 2; i --)
    {
        while (Cur > 1 && (SumDis[i] - SumDis[Cur - 1]) * 2LL <= Len)
            Cur --;
        ans[Cycle[i]] += Sum[i] - Sum[Cur];
        ans[Cycle[i]] -= (SumDis[i] - SumDis[Cur]) * SumSize[Cur - 1];
    }
    //Sum of TreeAns
    Candy = 0LL;
    for (int i = 1; i <= tot / 2 + 1; i ++)
        Candy += TreeAns[Cycle[i]];
    for (int i = 1; i <= tot / 2 + 1; i ++)
        ans[Cycle[i]] += Candy;
    //others
    memset(u, 0, sizeof(u));
    for (int i = 1; i <= tot / 2 + 1; i ++)
        dp(Cycle[i], Cycle[i]);
    //print
    for (int i = 1; i <= n; i ++)
    {
        if (i > 1) printf(" ");
        printf("%d", ans[i]);
    }
    printf("\n");
    return 0;
}


#include <stdio.h>
typedef long long ll;
const int nmax = 100000, mmax = nmax << 1;

int ind[nmax + 18], fst[nmax + 18], nxt[mmax + 18], pnt[mmax + 18], cst[mmax + 18];
int q[nmax + 18], qh = 1, qt, sze[nmax + 18], fa[nmax + 18], l[nmax + 18], r[nmax + 18], ssze[nmax + 18], wsize, w[nmax + 18];
ll tans[nmax + 18], whole, nowl, ans[nmax + 18], sd[nmax + 18], nowr, ts;
int n, tot;

void add(int s, int t, int c){pnt[++tot] = t, nxt[tot] = fst[s], fst[s] = tot, cst[tot] = c, ++ind[t];}

void dfs(int i, int f)
{
    ssze[i] += sze[i];
    for (int j = fst[i]; j; j = nxt[j])
	if (pnt[j] != f && ind[pnt[j]] == 2)
	{
	    w[l[r[i] = pnt[j]] = i] = cst[j];
	    whole += cst[j];
	    if (!r[pnt[j]])
	    {
		sd[pnt[j]] = sd[i] + cst[j];
		ssze[pnt[j]] += ssze[i];
		dfs(pnt[j], i);
	    }
	    return;
	}
}

void work(int i)
{
    wsize = n;
    int j, k;ll __e__ = whole >> 1;
    for (ts = tans[j = i]; (j = r[j]) != i; ) ts += tans[j];
    for (j = i; l[j] != i && (sd[i] - sd[l[j]] + whole) % whole <= __e__; )
	j = l[j];
    for (k = j; j != i; j = r[j])
	nowl += ((ssze[j] + wsize - ssze[l[k]]) % wsize) * w[j];
    for (j = l[k]; j != i; j = l[j])
	nowr += ((ssze[l[k]] + wsize - ssze[l[j]]) % wsize) * w[l[j]];
    for (j = k, ans[k = i] += nowl + nowr + ts; (i = r[i]) != k;)
    {
	if (i == j) nowl = nowl - (whole - w[l[i]]) * sze[i] + w[l[i]] * (wsize - sze[i]),nowr = 0, j = r[j];
	else
	    nowl += ((ssze[l[i]] + wsize - ssze[l[j]]) % wsize) * w[l[i]],nowr -= ((ssze[l[j]] - ssze[l[i]] + wsize) % wsize) * w[l[i]];
	while (j != i && (sd[j] - sd[i] + whole) % whole <= __e__)
	    nowl -= tans[j] + (/*i == j ? whole : */(sd[i] - sd[j] + whole) % whole) * sze[j], nowr += tans[j] + (/*i == j ? whole : */(sd[j] - sd[i] + whole) % whole) * sze[j], j = r[j];
	ans[i] = nowl + nowr + ts;
    }
}

void dfs2(int i, int f)
{
    for (int j = fst[i]; j; j = nxt[j])
	if (pnt[j] != f && ind[pnt[j]] == 1)
	    ans[pnt[j]]  = ans[i] - cst[j]*1LL*sze[pnt[j]] + cst[j]*1LL*(n - sze[pnt[j]]), dfs2(pnt[j], i);
}

int main()
{
    scanf("%d", &n);
    for (int i = 1, ss, tt, cc; i <= n; ++i)
	scanf("%d%d%d", &ss, &tt, &cc), add(ss, tt, cc), add(tt, ss, cc), sze[i] = 1;
    for (int i = 1; i <= n; ++i)
	if (ind[i] == 1)
	{
//	    ++sze[fa[i] = pnt[fst[i]]];
//	    tans[fa[i]] += cst[fst[i]];
	    /* if (--ind[fa[i]] == 1)*/ q[++qt] = i;
	}
    for (int i; qh <= qt; ++qh)
	for (int j = fst[i = q[qh]]; j; j = nxt[j])
	    if (ind[pnt[j]] > 1)
	    {
		sze[fa[i] = pnt[j]] += sze[q[qh]];
		tans[fa[i]] += cst[j]*1LL*sze[i] + tans[i];
		if (--ind[fa[i]] == 1) q[++qt] = fa[i];
		break;
	    }
    for (int i = 1; i <= n; ++i)
	if (ind[i] == 2)
	{
	    dfs(i, 0);
	    work(i);
	    break;
	}
    for (int i = 1; i <= n; ++i)
	if (ind[i] == 2)
	    dfs2(i, 0);
    for (int i = 1; i <= n; ++i)
	printf("%I64d ", ans[i]);
    return 0;
}


#include <stdio.h>
typedef long long ll;
const int nmax = 100000, mmax = nmax << 1;

int ind[nmax + 18], fst[nmax + 18], nxt[mmax + 18], pnt[mmax + 18], cst[mmax + 18];
int q[nmax + 18], qh = 1, qt, sze[nmax + 18], fa[nmax + 18], l[nmax + 18], r[nmax + 18], ssze[nmax + 18], wsize, w[nmax + 18];
ll tans[nmax + 18], whole, nowl, ans[nmax + 18], sd[nmax + 18], nowr, ts;
int n, tot;

void add(int s, int t, int c){pnt[++tot] = t, nxt[tot] = fst[s], fst[s] = tot, cst[tot] = c, ++ind[t];}

void dfs(int i, int f)
{
    ssze[i] += sze[i];
    for (int j = fst[i]; j; j = nxt[j])
    if (pnt[j] != f && ind[pnt[j]] == 2)
    {
        w[l[r[i] = pnt[j]] = i] = cst[j];
        whole += cst[j];
        if (!r[pnt[j]])
        {
        sd[pnt[j]] = sd[i] + cst[j];
        ssze[pnt[j]] += ssze[i];
        dfs(pnt[j], i);
        }
        return;
    }
}

void work(int i)
{
    wsize = n;
    int j, k;ll __e__ = whole >> 1;
    for (ts = tans[j = i]; (j = r[j]) != i; ) ts += tans[j];
    for (j = i; l[j] != i && (sd[i] - sd[l[j]] + whole) % whole <= __e__; )
    j = l[j];
    for (k = j; j != i; j = r[j])
    nowl += ((ssze[j] + wsize - ssze[l[k]]) % wsize) * w[j];
    for (j = l[k]; j != i; j = l[j])
    nowr += ((ssze[l[k]] + wsize - ssze[l[j]]) % wsize) * w[l[j]];
    for (j = k, ans[k = i] += nowl + nowr + ts; (i = r[i]) != k;)
    {
    if (i == j) nowl = nowl - (whole - w[l[i]]) * sze[i] + w[l[i]] * (wsize - sze[i]),nowr = 0, j = r[j];
    else
        nowl += ((ssze[l[i]] + wsize - ssze[l[j]]) % wsize) * w[l[i]],nowr -= ((ssze[l[j]] - ssze[l[i]] + wsize) % wsize) * w[l[i]];
    while (j != i && (sd[j] - sd[i] + whole) % whole <= __e__)
        nowl -= tans[j] + (/*i == j ? whole : */(sd[i] - sd[j] + whole) % whole) * sze[j], nowr += tans[j] + (/*i == j ? whole : */(sd[j] - sd[i] + whole) % whole) * sze[j], j = r[j];
    ans[i] = nowl + nowr + ts;
    }
}

void dfs2(int i, int f)
{
    for (int j = fst[i]; j; j = nxt[j])
    if (pnt[j] != f && ind[pnt[j]] == 1)
        ans[pnt[j]]  = ans[i] - cst[j]*1LL*sze[pnt[j]] + cst[j]*1LL*(n - sze[pnt[j]]), dfs2(pnt[j], i);
}

int main()
{
    scanf("%d", &n);
    for (int i = 1, ss, tt, cc; i <= n; ++i)
    scanf("%d%d%d", &ss, &tt, &cc), add(ss, tt, cc), add(tt, ss, cc), sze[i] = 1;
    for (int i = 1; i <= n; ++i)
    if (ind[i] == 1)
    {
        ++sze[fa[i] = pnt[fst[i]]];
        tans[fa[i]] += cst[fst[i]];
        if (--ind[fa[i]] == 1) q[++qt] = fa[i];
    }
    for (int i; qh <= qt; ++qh)
    {
    sze[fa[i = q[qh]] = pnt[fst[q[qh]]]] += sze[q[qh]];
    tans[fa[i]] += cst[fst[i]]*1LL*sze[i] + tans[i];
    if (--ind[fa[i]] == 1) q[++qt] = fa[i];
    }
    for (int i = 1; i <= n; ++i)
    if (ind[i] == 2)
    {
        dfs(i, 0);
        work(i);
        break;
    }
    for (int i = 1; i <= n; ++i)
    if (ind[i] == 2)
        dfs2(i, 0);
    for (int i = 1; i <= n; ++i)
    printf("%I64d ", ans[i]);
    return 0;
}


#include <stdio.h>
typedef long long ll;
const int nmax = 100000, mmax = nmax << 1;

int ind[nmax + 18], fst[nmax + 18], nxt[mmax + 18], pnt[mmax + 18], cst[mmax + 18];
int q[nmax + 18], qh = 1, qt, sze[nmax + 18], fa[nmax + 18], l[nmax + 18], r[nmax + 18], ssze[nmax + 18], wsize, w[nmax + 18];
ll tans[nmax + 18], whole, nowl, ans[nmax + 18], sd[nmax + 18], nowr;
int n, tot;

void add(int s, int t, int c){pnt[++tot] = t, nxt[tot] = fst[s], fst[s] = tot, cst[tot] = c, ++ind[t];}

void dfs(int i, int f)
{
    ssze[i] += sze[i];
    for (int j = fst[i]; j; j = nxt[j])
	if (pnt[j] != f && ind[pnt[j]] == 2)
	{
	    w[l[r[i] = pnt[j]] = i] = cst[j];
	    whole += cst[j];
	    wsize += sze[i];
	    if (!r[pnt[j]])
	    {
		sd[pnt[j]] = sd[i] + cst[j];
		ssze[pnt[j]] += ssze[i];
		dfs(pnt[j], i);
	    }
	    return;
	}
}

void work(int i)
{
    int j, k;ll __e__ = whole >> 1;
    for (j = i; l[j] != i && (sd[i] - sd[l[j]] + whole) % whole <= __e__; )
	j = l[j];
    for (k = j; j != i; j = r[j])
	nowl += ((ssze[j] + wsize - ssze[l[k]]) % wsize) * w[j] + tans[j];
    for (j = l[k]; j != i; j = l[j])
	nowr += ((ssze[l[k]] + wsize - ssze[l[j]]) % wsize) * w[l[j]] + tans[j];
    for (j = k, ans[k = i] += nowl + nowr + tans[i]; (i = r[i]) != k;)
    {
	if (i == j) nowl = nowl - (whole - w[l[i]]) * sze[i] + w[l[i]] * (wsize - sze[i]),nowr = 0, j = r[j];
	else
	    nowl += ((ssze[l[i]] + wsize - ssze[l[j]]) % wsize) * w[l[i]],nowr -= ((ssze[l[j]] - ssze[l[i]] + wsize) % wsize) * w[l[i]];
	while (r[j] != i && (sd[j] - sd[i] + whole) % whole <= __e__)
	    nowl -= tans[j] + (/*i == j ? whole : */(sd[i] - sd[j] + whole) % whole) * sze[j], nowr += tans[j] + (/*i == j ? whole : */(sd[j] - sd[i] + whole) % whole) * sze[j], j = r[j];
	ans[i] = nowl + nowr + tans[i];
    }
}

void dfs2(int i, int f)
{
    for (int j = fst[i]; j; j = nxt[j])
	if (pnt[j] != f && ind[pnt[j]] == 1)
	    ans[pnt[j]]  = ans[i] - cst[j]*1LL*sze[pnt[j]] + cst[j]*1LL*(n - sze[pnt[j]]), dfs2(pnt[j], i);
}

int main()
{
    scanf("%d", &n);
    for (int i = 1, ss, tt, cc; i <= n; ++i)
	scanf("%d%d%d", &ss, &tt, &cc), add(ss, tt, cc), add(tt, ss, cc), sze[i] = 1;
    for (int i = 1; i <= n; ++i)
	if (ind[i] == 1)
	{
	    ++sze[fa[i] = pnt[fst[i]]];
	    tans[fa[i]] += cst[fst[i]];
	    if (--ind[fa[i]] == 1) q[++qt] = fa[i];
	}
    for (int i; qh <= qt; ++qh)
    {
	sze[fa[i = q[qh]] = pnt[fst[q[qh]]]] += sze[q[qh]];
	tans[fa[i]] += cst[fst[i]]*1LL*sze[i] + tans[i];
	if (--ind[fa[i]] == 1) q[++qt] = fa[i];
    }
    for (int i = 1; i <= n; ++i)
	if (ind[i] == 2)
	{
	    dfs(i, 0);
	    work(i);
	    break;
	}
    for (int i = 1; i <= n; ++i)
	if (ind[i] == 2)
	    dfs2(i, 0);
    for (int i = 1; i <= n; ++i)
	printf("%I64d ", ans[i]);
    return 0;
}


#include <stdio.h>
typedef long long ll;
const int nmax = 200000, mmax = nmax << 1;

int ind[nmax + 18], fst[nmax + 18], nxt[mmax + 18], pnt[mmax + 18], cst[mmax + 18];
int q[nmax + 18], qh = 1, qt, sze[nmax + 18], fa[nmax + 18], l[nmax + 18], r[nmax + 18], ssze[nmax + 18], wsize, w[nmax + 18];
ll tans[nmax + 18], whole, nowl, ans[nmax + 18], sd[nmax + 18], nowr, ts;
int n, tot;

void add(int s, int t, int c){pnt[++tot] = t, nxt[tot] = fst[s], fst[s] = tot, cst[tot] = c, ++ind[t];}

void dfs(int i, int f)
{
    ssze[i] += sze[i];
    for (int j = fst[i]; j; j = nxt[j])
    if (pnt[j] != f && ind[pnt[j]] == 2)
    {
        w[l[r[i] = pnt[j]] = i] = cst[j];
        whole += cst[j];
        if (!r[pnt[j]])
        {
        sd[pnt[j]] = sd[i] + cst[j];
        ssze[pnt[j]] += ssze[i];
        dfs(pnt[j], i);
        }
        return;
    }
}

void work(int i)
{
    wsize = n;
    int j, k;ll __e__ = whole >> 1;
    for (ts = tans[j = i]; (j = r[j]) != i; ) ts += tans[j];
    for (j = i; l[j] != i && (sd[i] - sd[l[j]] + whole) % whole <= __e__; )
    j = l[j];
    for (k = j; j != i; j = r[j])
    nowl += ((ssze[j] + wsize - ssze[l[k]]) % wsize) * w[j];
    for (j = l[k]; j != i; j = l[j])
    nowr += ((ssze[l[k]] + wsize - ssze[l[j]]) % wsize) * w[l[j]];
    for (j = k, ans[k = i] += nowl + nowr + ts; (i = r[i]) != k;)
    {
    if (i == j) nowl = nowl - (whole - w[l[i]]) * sze[i] + w[l[i]] * (wsize - sze[i]),nowr = 0, j = r[j];
    else
        nowl += ((ssze[l[i]] + wsize - ssze[l[j]]) % wsize) * w[l[i]],nowr -= ((ssze[l[j]] - ssze[l[i]] + wsize) % wsize) * w[l[i]];
    while (j != i && (sd[j] - sd[i] + whole) % whole <= __e__)
        nowl -= tans[j] + (/*i == j ? whole : */(sd[i] - sd[j] + whole) % whole) * sze[j], nowr += tans[j] + (/*i == j ? whole : */(sd[j] - sd[i] + whole) % whole) * sze[j], j = r[j];
    ans[i] = nowl + nowr + ts;
    }
}

void dfs2(int i, int f)
{
    for (int j = fst[i]; j; j = nxt[j])
    if (pnt[j] != f && ind[pnt[j]] == 1)
        ans[pnt[j]]  = ans[i] - cst[j]*1LL*sze[pnt[j]] + cst[j]*1LL*(n - sze[pnt[j]]), dfs2(pnt[j], i);
}

int main()
{
    scanf("%d", &n);
    for (int i = 1, ss, tt, cc; i <= n; ++i)
    scanf("%d%d%d", &ss, &tt, &cc), add(ss, tt, cc), add(tt, ss, cc), sze[i] = 1;
    for (int i = 1; i <= n; ++i)
    if (ind[i] == 1)
    {
//      ++sze[fa[i] = pnt[fst[i]]];
//      tans[fa[i]] += cst[fst[i]];
        /* if (--ind[fa[i]] == 1)*/ q[++qt] = i;
    }
    for (int i; qh <= qt; ++qh)
    for (int j = fst[i = q[qh]]; j; j = nxt[j])
        if (ind[pnt[j]] > 1)
        {
        sze[fa[i] = pnt[j]] += sze[q[qh]];
        tans[fa[i]] += cst[j]*1LL*sze[i] + tans[i];
        if (--ind[fa[i]] == 1) q[++qt] = fa[i];
        break;
        }
    for (int i = 1; i <= n; ++i)
    if (ind[i] == 2)
    {
        dfs(i, 0);
        work(i);
        break;
    }
    for (int i = 1; i <= n; ++i)
    if (ind[i] == 2)
        dfs2(i, 0);
    for (int i = 1; i <= n; ++i)
    printf("%I64d ", ans[i]);
    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cmath>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <utility>
#include <numeric>
#include <memory>
#include <stack>
#include <deque>
#include <queue>
#include <list>
#include <functional>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <bitset>

#define X first
#define Y second
#define MAX(x, y) x = x > (y) ? x : (y)
#define MIN(x, y) x = x > (y) ? (y) : x
#define LOW(x) ((x) & -(x))
#define SQR(x) ((x) * (x))
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rFOR(i, b, a) for (int i = (b); i > (a); --i)
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(x, p) memset(x, p, sizeof(x))
#define COPY(x, y) memcpy(x, y, sizeof(x))
#define ALL(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<string> VS;

const int INF = 0x3fffffff;
const ll _INF = 0x7ffffffffffffffll;
const double EPS = 1e-9;
const double PI = acos(-1.0);

template<class T, class INT>
inline T pow(T x, INT y)
{
	T res(1);
	for (T tt(x); y; y /= 2)
	{
		if (y & 1) res *= tt;
		tt *= tt;
	}
	return res;
}

template<class T, class INT>
inline T pow(T x, INT y, T p)
{
	T res(1);
	for (T tt(x); y; y /= 2)
	{
		if (y & 1) res = res * tt % p;
		tt = tt * tt % p;
	}
	return res;
}

int toInt(string s)
{
	int x = 0;
	istringstream sin(s);
	sin >> x;
	return x;
}

template<class T>
inline string toString(T x)
{
	ostringstream sout;
	sout << x;
	return sout.str();
}

template<class INT>
inline INT gcd(INT x, INT y)
{
	return y ? gcd(y, x % y) : x;
}

template<class INT>
inline int calc_bits(INT x)
{
	return x ? (x & 1) + calc_bits(x >> 1) : 0;
}

const int MAXN = 200000 + 10;
const int MAXM = 400000 + 10;

int n, tot, tt, u, v, c, sum, it, tmpl, tmpr, zf[MAXN], t;
int len[MAXM], slen[MAXM], prt[MAXM], head[MAXN], next[MAXM], vis[MAXM], cost[MAXM];
ll cnt[MAXN], ans[MAXN], _ans, f[MAXN];
bool check[MAXN];

int root(int x)
{
	return zf[x] ? zf[x] = root(zf[x]) : x;
}

inline void add (int u, int v, int c)
{
	next[++tot] = head[u];
	vis[head[u] = tot] = v;
	cost[tot] = c;
}

inline bool dfs(int x, int fat)
{
	check[x] = true;
	for (int i = head[x], k; i; i = next[i])
		if (!check[k = vis[i]] && dfs(k, x) || k == t && fat != t)
		{
			len[tt] = cost[i];
			prt[tt++] = k;
			return true;
		}
	return false;
}

ll calc(int x)
{
	ll res = 0;
	cnt[x] = 1;
	check[x] = true;
	for (int i = head[x], k; i; i = next[i])
		if (!check[k = vis[i]]) res += calc(k) + cost[i] * cnt[k], cnt[x] += cnt[k];
	return f[x] = res;
}

void _calc(int x, ll S, int fat, int path)
{
	ans[x] += f[x];
	if (fat) ans[x] = ans[fat] - (2 * cnt[x] - n) * cost[path];
	check[x] = true;
	for (int i = head[x], k; i; i = next[i])
		if (!check[k = vis[i]]) _calc(k, S, x, i);
}

int main(int argc, char* argv[])
{
	cin >> n;
	REP(i, n) cin >> u >> v >> c, add(u, v, c), add(v, u, c);
	FOR(i, 1, n + 1)
		for (int j = head[i]; j; j = next[j])
		{
			u = root(i), v = root(vis[j]);
			if (u == v) t = i;
			else zf[u] = v;
		}
	dfs(t, 0);
	CLEAR(check);
	REP(i, tt) check[prt[i]] = true;
	REP(i, tt) calc(prt[i]);
	memcpy(prt + tt, prt, tt * sizeof(*prt));
	memcpy(len + tt, len, tt * sizeof(*len));
	partial_sum(len, len + tt + tt, slen);
	sum = slen[tt - 1];
	it = tt;
	rFOR(i, tt - 1, 0)
	{
		if (slen[i - 1] > sum - slen[i - 1]) it = i, tmpr += cnt[prt[i]];
		else tmpl += cnt[prt[i]];
		_ans += cnt[prt[i]] * min(slen[i - 1], sum - slen[i - 1]);
	}
	tmpr += cnt[prt[0]];
	ans[prt[0]] = _ans;
	FOR(i, 1, tt)
	{
		_ans += (tmpr - tmpl) * len[i - 1];
		while (it < tt + i && 2 * (slen[it - 1] - slen[i - 1]) < sum)
		{
			_ans -= (sum - 2 * (slen[it - 1] - slen[i - 1])) * cnt[prt[it]];
			tmpl += cnt[prt[it]], tmpr -= cnt[prt[it++]];
		}
		tmpl -= cnt[prt[i]], tmpr += cnt[prt[i]];
		ans[prt[i]] = _ans;
	}
	CLEAR(check);
	REP(i, tt) check[prt[i]] = true;
	REP(i, tt) _calc(prt[i], ans[prt[i]], 0, 0);
	FOR(i, 1, n + 1) cout << ans[i] << " ";
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cmath>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <utility>
#include <numeric>
#include <memory>
#include <stack>
#include <deque>
#include <queue>
#include <list>
#include <functional>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <bitset>

#define X first
#define Y second
#define MAX(x, y) x = x > (y) ? x : (y)
#define MIN(x, y) x = x > (y) ? (y) : x
#define LOW(x) ((x) & -(x))
#define SQR(x) ((x) * (x))
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rFOR(i, b, a) for (int i = (b); i > (a); --i)
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(x, p) memset(x, p, sizeof(x))
#define COPY(x, y) memcpy(x, y, sizeof(x))
#define ALL(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<string> VS;

const int INF = 0x3fffffff;
const ll _INF = 0x7ffffffffffffffll;
const double EPS = 1e-9;
const double PI = acos(-1.0);

template<class T, class INT>
inline T pow(T x, INT y)
{
	T res(1);
	for (T tt(x); y; y /= 2)
	{
		if (y & 1) res *= tt;
		tt *= tt;
	}
	return res;
}

template<class T, class INT>
inline T pow(T x, INT y, T p)
{
	T res(1);
	for (T tt(x); y; y /= 2)
	{
		if (y & 1) res = res * tt % p;
		tt = tt * tt % p;
	}
	return res;
}

int toInt(string s)
{
	int x = 0;
	istringstream sin(s);
	sin >> x;
	return x;
}

template<class T>
inline string toString(T x)
{
	ostringstream sout;
	sout << x;
	return sout.str();
}

template<class INT>
inline INT gcd(INT x, INT y)
{
	return y ? gcd(y, x % y) : x;
}

template<class INT>
inline int calc_bits(INT x)
{
	return x ? (x & 1) + calc_bits(x >> 1) : 0;
}

const int MAXN = 200000 + 10;
const int MAXM = 400000 + 10;

int n, tot, tt, u, v, c, sum, it, tmpl, tmpr, zf[MAXN], t;
int len[MAXM], slen[MAXM], prt[MAXM], head[MAXN], next[MAXM], vis[MAXM], cost[MAXM];
ll cnt[MAXN], ans[MAXN], _ans, f[MAXN];
bool check[MAXN];

int root(int x)
{
	return zf[x] ? zf[x] = root(zf[x]) : x;
}

inline void add (int u, int v, int c)
{
	next[++tot] = head[u];
	vis[head[u] = tot] = v;
	cost[tot] = c;
}

inline bool dfs(int x, int fat)
{
	check[x] = true;
	for (int i = head[x], k; i; i = next[i])
		if (!check[k = vis[i]] && dfs(k, x) || k == t && fat != t)
		{
			len[tt] = cost[i];
			prt[tt++] = k;
			return true;
		}
	return false;
}

ll calc(int x)
{
	ll res = 0;
	cnt[x] = 1;
	check[x] = true;
	for (int i = head[x], k; i; i = next[i])
		if (!check[k = vis[i]]) res += calc(k) + cost[i] * cnt[k], cnt[x] += cnt[k];
	return f[x] = res;
}

void _calc(int x, ll S, int fat, int path)
{
	ans[x] += f[x];
	if (fat) ans[x] = ans[fat] - (2 * cnt[x] - n) * cost[path];
	check[x] = true;
	for (int i = head[x], k; i; i = next[i])
		if (!check[k = vis[i]]) _calc(k, S, x, i);
}

int main(int argc, char* argv[])
{
	cin >> n;
	REP(i, n) cin >> u >> v >> c, add(u, v, c), add(v, u, c);
	FOR(i, 1, n + 1)
		for (int j = head[i]; j; j = next[j])
		{
			if (vis[j] > i) continue;
			u = root(i), v = root(vis[j]);
			if (u == v) t = i;
			else zf[u] = v;
		}
	dfs(t, 0);
	CLEAR(check);
	REP(i, tt) check[prt[i]] = true;
	REP(i, tt) calc(prt[i]);
	memcpy(prt + tt, prt, tt * sizeof(*prt));
	memcpy(len + tt, len, tt * sizeof(*len));
	partial_sum(len, len + tt + tt, slen);
	sum = slen[tt - 1];
	it = tt;
	rFOR(i, tt - 1, 0)
	{
		if (slen[i - 1] > sum - slen[i - 1]) it = i, tmpr += cnt[prt[i]];
		else tmpl += cnt[prt[i]];
		_ans += cnt[prt[i]] * min(slen[i - 1], sum - slen[i - 1]);
	}
	tmpr += cnt[prt[0]];
	ans[prt[0]] = _ans;
	FOR(i, 1, tt)
	{
		_ans += (tmpr - tmpl) * len[i - 1];
		while (it < tt + i && 2 * (slen[it - 1] - slen[i - 1]) < sum)
		{
			_ans -= (sum - 2 * (slen[it - 1] - slen[i - 1])) * cnt[prt[it]];
			tmpl += cnt[prt[it]], tmpr -= cnt[prt[it++]];
		}
		tmpl -= cnt[prt[i]], tmpr += cnt[prt[i]];
		ans[prt[i]] = _ans;
	}
	sum = 0;
	REP(i, tt) sum += f[prt[i]];
	REP(i, tt) ans[prt[i]] += sum - f[prt[i]];
	CLEAR(check);
	REP(i, tt) check[prt[i]] = true;
	REP(i, tt) _calc(prt[i], ans[prt[i]], 0, 0);
	FOR(i, 1, n + 1) cout << ans[i] << " ";
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cmath>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <utility>
#include <numeric>
#include <memory>
#include <stack>
#include <deque>
#include <queue>
#include <list>
#include <functional>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <bitset>

#define X first
#define Y second
#define MAX(x, y) x = x > (y) ? x : (y)
#define MIN(x, y) x = x > (y) ? (y) : x
#define LOW(x) ((x) & -(x))
#define SQR(x) ((x) * (x))
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rFOR(i, b, a) for (int i = (b); i > (a); --i)
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(x, p) memset(x, p, sizeof(x))
#define COPY(x, y) memcpy(x, y, sizeof(x))
#define ALL(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<string> VS;

const int INF = 0x3fffffff;
const ll _INF = 0x7ffffffffffffffll;
const double EPS = 1e-9;
const double PI = acos(-1.0);

template<class T, class INT>
inline T pow(T x, INT y)
{
	T res(1);
	for (T tt(x); y; y /= 2)
	{
		if (y & 1) res *= tt;
		tt *= tt;
	}
	return res;
}

template<class T, class INT>
inline T pow(T x, INT y, T p)
{
	T res(1);
	for (T tt(x); y; y /= 2)
	{
		if (y & 1) res = res * tt % p;
		tt = tt * tt % p;
	}
	return res;
}

int toInt(string s)
{
	int x = 0;
	istringstream sin(s);
	sin >> x;
	return x;
}

template<class T>
inline string toString(T x)
{
	ostringstream sout;
	sout << x;
	return sout.str();
}

template<class INT>
inline INT gcd(INT x, INT y)
{
	return y ? gcd(y, x % y) : x;
}

template<class INT>
inline int calc_bits(INT x)
{
	return x ? (x & 1) + calc_bits(x >> 1) : 0;
}

const int MAXN = 200000 + 10;
const int MAXM = 400000 + 10;

int n, tot, tt, u, v, c, sum, it, tmpl, tmpr, zf[MAXN], t;
int len[MAXM], slen[MAXM], prt[MAXM], head[MAXN], next[MAXM], vis[MAXM], cost[MAXM];
ll cnt[MAXN], ans[MAXN], _ans, f[MAXN];
bool check[MAXN];

int root(int x)
{
	return zf[x] ? zf[x] = root(zf[x]) : x;
}

inline void add (int u, int v, int c)
{
	next[++tot] = head[u];
	vis[head[u] = tot] = v;
	cost[tot] = c;
}

inline bool dfs(int x, int fat)
{
	check[x] = true;
	for (int i = head[x], k; i; i = next[i])
		if (!check[k = vis[i]] && dfs(k, x) || k == t && fat != t)
		{
			len[tt] = cost[i];
			prt[tt++] = k;
			return true;
		}
	return false;
}

ll calc(int x)
{
	ll res = 0;
	cnt[x] = 1;
	check[x] = true;
	for (int i = head[x], k; i; i = next[i])
		if (!check[k = vis[i]]) res += calc(k) + cost[i] * cnt[k], cnt[x] += cnt[k];
	return f[x] = res;
}

void _calc(int x, ll S, int fat, int path)
{
	ans[x] += f[x];
	if (fat) ans[x] = ans[fat] - (2 * cnt[x] - n) * cost[path];
	check[x] = true;
	for (int i = head[x], k; i; i = next[i])
		if (!check[k = vis[i]]) _calc(k, S, x, i);
}

int main(int argc, char* argv[])
{
	cin >> n;
	REP(i, n) cin >> u >> v >> c, add(u, v, c), add(v, u, c);
	FOR(i, 1, n + 1)
		for (int j = head[i]; j; j = next[j])
		{
			u = root(i), v = root(vis[j]);
			if (u == v) t = i;
			else zf[u] = v;
		}
	dfs(t, 0);
	CLEAR(check);
	REP(i, tt) check[prt[i]] = true;
	REP(i, tt) calc(prt[i]);
	memcpy(prt + tt, prt, tt * sizeof(*prt));
	memcpy(len + tt, len, tt * sizeof(*len));
	partial_sum(len, len + tt + tt, slen);
	sum = slen[tt - 1];
	it = tt;
	rFOR(i, tt - 1, 0)
	{
		if (slen[i - 1] > sum - slen[i - 1]) it = i, tmpr += cnt[prt[i]];
		else tmpl += cnt[prt[i]];
		_ans += cnt[prt[i]] * min(slen[i - 1], sum - slen[i - 1]);
	}
	tmpr += cnt[prt[0]];
	ans[prt[0]] = _ans;
	FOR(i, 1, tt)
	{
		_ans += (tmpr - tmpl) * len[i - 1];
		while (it < tt + i && 2 * (slen[it - 1] - slen[i - 1]) < sum)
		{
			_ans -= (sum - 2 * (slen[it - 1] - slen[i - 1])) * cnt[prt[it]];
			tmpl += cnt[prt[it]], tmpr -= cnt[prt[it++]];
		}
		tmpl -= cnt[prt[i]], tmpr += cnt[prt[i]];
		ans[prt[i]] = _ans;
	}
	sum = 0;
	REP(i, tt) sum += f[prt[i]];
	REP(i, tt) ans[prt[i]] += sum - f[prt[i]];
	CLEAR(check);
	REP(i, tt) check[prt[i]] = true;
	REP(i, tt) _calc(prt[i], ans[prt[i]], 0, 0);
	FOR(i, 1, n + 1) cout << ans[i] << " ";
}


#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<fstream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<cctype>
#include<cassert>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<iomanip>
#include<utility>
using namespace std;
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define rg register
#define il inline
#define lch(x) ((x)<<1)
#define rch(x) ((x)<<1^1)
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
#define rep0(i,n) for(register int i=0;i<(n);++i)
#define per0(i,n) for(register int i=(n)-1;i>=0;--i)
#define rep(i,st,ed) for(register int i=(st);i<=(ed);++i)
#define per(i,ed,st) for(register int i=(ed);i>=(st);--i)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double dbl;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> il T qmin(const T &a,const T &b){return a<b?a:b;}
template<typename T> il T qmax(const T &a,const T &b){return a>b?a:b;}
template<typename T> il void getmin(T &a,const T &b){if(a>b) a=b;}
template<typename T> il void getmax(T &a,const T &b){if(a<b) a=b;}
il void fileio(string s){
	freopen((s+".in").c_str(),"r",stdin);
	freopen((s+".out").c_str(),"w",stdout);
}

const int inf=(int)1e9+7;
const ll linf=(ll)1e17+7;
const int N=4e5+7,M=N<<1;

int v[M],w[M],nxt[M];
int head[N],id[N],sz[N],cnt,node[N],val[N];
ll sum[N],dt[N],dep[N],ans[N];
int n;

il void addedge(const int &p,const int &x,const int &y,const int &z){
	v[p]=y,w[p]=z;
	nxt[p]=head[x],head[x]=p;
}

bool iscut[M],used[M];
int dfn[N],low[N],dfsclock;
il void dfs(int u){
	dfn[u]=low[u]=++dfsclock;
	for(int e=head[u];e;e=nxt[e]){
		if(used[e>>1]) continue;
		used[e>>1]=true;
		if(!dfn[v[e]]){
			dfs(v[e]);
			getmin(low[u],low[v[e]]);
			if(low[v[e]]>dfn[u]) iscut[e>>1]=true;
		}else getmin(low[u],low[v[e]]);
	}
}

il void getall(int u,int p){
	node[id[u]=++cnt]=u;
	for(int e=head[u];e;e=nxt[e]){
		if(iscut[e>>1]||v[e]==p) continue;
		val[u]=w[e];
		if(!id[v[e]]) getall(v[e],u);
		break;
	}
}

il void dfs(int u,int p){
	sz[u]=1;
	for(int e=head[u];e;e=nxt[e]){
		if(v[e]==p||!iscut[e>>1]) continue;
		dfs(v[e],u);
		sum[u]+=sum[v[e]]+(ll)sz[v[e]]*w[e];
		sz[u]+=sz[v[e]];
	}
}

il void getans(int u,int p,ll tot){
	ans[u]=sum[u]+dep[u]*(n-sz[u])+tot;
	int tmp;
	for(int e=head[u];e;e=nxt[e]){
		if(!iscut[e>>1]) continue;
		if(v[e]==p){
			tmp=w[e];
		}else{
			dep[v[e]]=dep[u]+w[e];
			sum[u]-=sum[v[e]]+sz[v[e]]*w[e];
			sum[v[e]]+=(ll)(sz[u]-sz[v[e]])*w[e]+sum[u];
			sz[u]-=sz[v[e]];
			sz[v[e]]+=sz[u];
			getans(v[e],u,tot);
		}
	}
	if(p){
		sum[u]-=sum[p]+sz[p]*tmp;
		sum[p]+=(ll)(sz[u]-sz[p])*tmp+sum[u];
		sz[u]-=sz[p];
		sz[p]+=sz[u];
	}
}

ll len[N];
ll sumsz[N],sumlen[N],sumans[N];
ll sumsz1[N],sumlen1[N],sumans1[N];
il void init(){
	dfs(1);
	int rt;
	rep(i,1,n){
		if(iscut[i]) continue;
		rt=v[lch(i)];
		break;
	}
	getall(rt,0);
	rep(i,1,cnt){
		int u=node[i];
	//	cerr<<u<<" : "<<val[u]<<endl;
		dfs(u,0);
	}
//	cerr<<endl;
	ll totlen=0;
	rep(i,1,cnt) totlen+=val[node[i]];
	rep(i,cnt+1,cnt<<1) node[i]=node[i-cnt];
	rep(i,1,cnt<<1){
		len[i]=val[node[i-1]]+len[i-1];
		sumsz[i]=sumsz[i-1]+sz[node[i]];
		sumlen[i]=sumlen[i-1]+len[i];
		sumans[i]=sumans[i-1]+len[i]*sz[node[i]]+sum[node[i]];
	//	cerr<<len[i]<<" "<<sumsz[i]<<" "<<sumlen[i]<<" "<<sumans[i]<<endl;
	}
	int cur=1;
	rep(i,1,cnt){
		while(cur<(cnt<<1)&&len[cur+1]-len[i]<=totlen-len[cur+1]+len[i]) ++cur;
	//	cerr<<node[i]<<" to "<<node[cur]<<endl;
		dt[node[i]]+=sumans[cur]-sumans[i]-len[i]*(sumsz[cur]-sumsz[i]);
	}
//	rep(i,1,cnt) cerr<<node[i]<<" "<<dt[node[i]]<<endl;
	reverse(node+2,node+(cnt<<1)+1);
	rep(i,1,cnt<<1){
		len[i]=val[node[i]]+len[i-1];
		sumsz[i]=sumsz[i-1]+sz[node[i]];
		sumlen[i]=sumlen[i-1]+len[i];
		sumans[i]=sumans[i-1]+len[i]*sz[node[i]]+sum[node[i]];
	}
	cur=1;
	rep(i,1,cnt){
		while(cur<(cnt<<1)&&len[cur+1]-len[i]<totlen-len[cur+1]+len[i]) ++cur;
		dt[node[i]]+=sumans[cur]-sumans[i]-len[i]*(sumsz[cur]-sumsz[i]);
	}
//	cerr<<"end1"<<endl;
	rep(i,1,cnt) getans(node[i],0,dt[node[i]]);
}

int main(){
//	fileio("test");
	scanf("%d",&n);
	rep(i,1,n){
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		addedge(lch(i),x,y,z);
		addedge(rch(i),y,x,z);
	}
	init();
	rep(i,1,n) printf("%lld ",ans[i]);
	puts("");
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

const int MAXN = 200010, MAXM = 400010;
int n;
int head[MAXN], next[MAXM], b[MAXM], c[MAXM], bj[MAXM], l;
int p[MAXN], q[MAXN], p2[MAXN], q2[MAXN];
bool chk[MAXN], use[MAXM];
int s[MAXN], d[MAXN], f[MAXN], fa[MAXN];
int d1, d2, divid, s1, s2, f1, f2;

void makeedge(int a0, int b0, int c0)
{
	b[++l] = b0; c[l] = c0;
	next[l] = head[a0];
	head[a0] = l;
}

bool flag;

void dfs(int v, int dep)
{
	if (flag) return;
	chk[v] = true;
	p2[dep] = v;
	for (int i = head[v]; i; i = next[i])
		if (!use[i])
		{
			use[i] = use[bj[i]] = true;
			if (!chk[b[i]])
			{
				q2[dep + 1] = c[i];
				dfs(b[i], dep + 1);
			} else
			{
				p[++p[0]] = b[i]; q[p[0]] = c[i];
				for (; p2[dep] != b[i]; --dep) p[++p[0]] = p2[dep], q[p[0]] = q2[dep];
				flag = true;
				return;
			}
		}
}

void dp(int v)
{
	s[v] = 1;
	d[v] = 0;
	for (int i = head[v]; i; i = next[i])
		if ((!chk[b[i]]) && b[i] != fa[v])
		{
			fa[b[i]] = v;
			dp(b[i]);
			s[v] += s[b[i]];
			d[v] += d[b[i]] + c[i] * s[b[i]];
		}
}

void dfs_tr(int v)
{
	for (int i = head[v]; i; i = next[i])
		if ((!chk[b[i]]) && b[i] != fa[v])
		{
			f[b[i]] = d[b[i]] + (f[v] - d[b[i]] - s[b[i]] * c[i]) + (c[i] * (n - s[b[i]]));
			dfs_tr(b[i]);
		}
}

int main()
{
	// freopen("g.in","r",stdin);
	scanf("%d",&n);
	memset(head,0,sizeof(head));
	memset(next,0,sizeof(next));
	l = 0;
	int a0, b0, c0;
	for (int i = 1; i <= n; ++i)
	{
		scanf("%d%d%d",&a0,&b0,&c0);
		makeedge(a0, b0, c0);
		makeedge(b0, a0, c0);
		bj[l] = l - 1; bj[l - 1] = l;
	}

	memset(chk,0,sizeof(chk));
	memset(use,0,sizeof(use));
	p[0] = 0; p2[0] = 0;
	flag = false;
	dfs(1, 1);

	memset(s,0,sizeof(s));
	memset(d,0,sizeof(d));
	memset(fa,0,sizeof(fa));
	memset(chk,0,sizeof(chk));
	for (int i = 1; i <= p[0]; ++i) chk[p[i]] = true;
	for (int i = 1; i <= p[0]; ++i) dp(p[i]);

	divid = 2;
	d1 = q[1];
	s1 = 0;
	f1 = 0;
	d2 = f2 = s2 = 0;
	for (int i = 2; i <= p[0]; ++i)
	{
		d2 += q[i];
		s2 += s[p[i]];
		f2 += q[i] * s2;
	}

	for (; d1 < d2 && divid <= p[0]; ++divid)
	{
		s1 += s[p[divid]];
		s2 -= s[p[divid]];
		f1 += s[p[divid]] * d1;
		f2 -= s[p[divid]] * d2;
		d1 += q[divid];
		d2 -= q[divid];
	}

	int tmp = 0;
	for (int i = 1; i <= p[0]; ++i) tmp += d[p[i]];
	f[p[1]] = f1 + f2 + tmp;

	for (int i = 2; i <= p[0]; ++i)
	{
		f1 -= s1 * q[i - 1];
		s1 -= s[p[i]];
		s2 += s[p[i - 1]];
		f2 += s2 * q[i - 1];
		d1 -= q[i - 1];
		d2 += q[i - 1];
		for (; d1 < d2; divid = (divid % p[0]) + 1)
		{
			s1 += s[p[divid]];
			s2 -= s[p[divid]];
			f1 += s[p[divid]] * d1;
			f2 -= s[p[divid]] * d2;
			d1 += q[divid];
			d2 -= q[divid];
		}
		f[p[i]] = f1 + f2 + tmp;
		// printf("div:%d\n",divid);
		// printf("d1:%d d2:%d s1:%d s2:%d f1:%d f2:%d\n",d1, d2, s1, s2, f1, f2);
	}

	for (int i = 1; i <= p[0]; ++i) dfs_tr(p[i]);

	for (int i = 1; i < n; ++i) printf("%d ",f[i]); printf("%d\n",f[n]);
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

const int MAXN = 200010, MAXM = 400010;
int n;
int head[MAXN], next[MAXM], b[MAXM], bj[MAXM], l;
long long c[MAXM];
int p[MAXN], p2[MAXN];
long long q[MAXN], q2[MAXN];
bool chk[MAXN], use[MAXM];
int s[MAXN], fa[MAXN];
long long d[MAXN], f[MAXN];
long long d1, d2, f1, f2;
int divid, s1, s2;

void makeedge(int a0, int b0, long long c0)
{
	b[++l] = b0; c[l] = c0;
	next[l] = head[a0];
	head[a0] = l;
}

bool flag;

void dfs(int v, int dep)
{
	if (flag) return;
	chk[v] = true;
	p2[dep] = v;
	for (int i = head[v]; i; i = next[i])
		if (!use[i])
		{
			use[i] = use[bj[i]] = true;
			if (!chk[b[i]])
			{
				q2[dep + 1] = c[i];
				dfs(b[i], dep + 1);
			} else
			{
				p[++p[0]] = b[i]; q[p[0]] = c[i];
				for (; p2[dep] != b[i]; --dep) p[++p[0]] = p2[dep], q[p[0]] = q2[dep];
				flag = true;
				return;
			}
		}
}

void dp(int v)
{
	s[v] = 1;
	d[v] = 0;
	for (int i = head[v]; i; i = next[i])
		if ((!chk[b[i]]) && b[i] != fa[v])
		{
			fa[b[i]] = v;
			dp(b[i]);
			s[v] += s[b[i]];
			d[v] += d[b[i]] + c[i] * s[b[i]];
		}
}

void dfs_tr(int v)
{
	for (int i = head[v]; i; i = next[i])
		if ((!chk[b[i]]) && b[i] != fa[v])
		{
			f[b[i]] = d[b[i]] + (f[v] - d[b[i]] - s[b[i]] * c[i]) + (c[i] * (n - s[b[i]]));
			dfs_tr(b[i]);
		}
}

int main()
{
	// freopen("g.in","r",stdin);
	scanf("%d",&n);
	memset(head,0,sizeof(head));
	memset(next,0,sizeof(next));
	l = 0;
	int a0, b0;
	long long c0;
	for (int i = 1; i <= n; ++i)
	{
		scanf("%d%d%I64d",&a0,&b0,&c0);
		makeedge(a0, b0, c0);
		makeedge(b0, a0, c0);
		bj[l] = l - 1; bj[l - 1] = l;
	}

	memset(chk,0,sizeof(chk));
	memset(use,0,sizeof(use));
	p[0] = 0; p2[0] = 0;
	flag = false;
	dfs(1, 1);

	memset(s,0,sizeof(s));
	memset(d,0,sizeof(d));
	memset(fa,0,sizeof(fa));
	memset(chk,0,sizeof(chk));
	for (int i = 1; i <= p[0]; ++i) chk[p[i]] = true;
	for (int i = 1; i <= p[0]; ++i) dp(p[i]);

	divid = 2;
	d1 = q[1];
	s1 = 0;
	f1 = 0;
	d2 = f2 = s2 = 0;
	for (int i = 2; i <= p[0]; ++i)
	{
		d2 += q[i];
		s2 += s[p[i]];
		f2 += q[i] * s2;
	}

	for (; d1 < d2 && divid <= p[0]; ++divid)
	{
		s1 += s[p[divid]];
		s2 -= s[p[divid]];
		f1 += s[p[divid]] * d1;
		f2 -= s[p[divid]] * d2;
		d1 += q[divid];
		d2 -= q[divid];
	}

	int tmp = 0;
	for (int i = 1; i <= p[0]; ++i) tmp += d[p[i]];
	f[p[1]] = f1 + f2 + tmp;

	for (int i = 2; i <= p[0]; ++i)
	{
		f1 -= s1 * q[i - 1];
		s1 -= s[p[i]];
		s2 += s[p[i - 1]];
		f2 += s2 * q[i - 1];
		d1 -= q[i - 1];
		d2 += q[i - 1];
		for (; d1 < d2; divid = (divid % p[0]) + 1)
		{
			s1 += s[p[divid]];
			s2 -= s[p[divid]];
			f1 += s[p[divid]] * d1;
			f2 -= s[p[divid]] * d2;
			d1 += q[divid];
			d2 -= q[divid];
		}
		f[p[i]] = f1 + f2 + tmp;
		// printf("div:%d\n",divid);
		// printf("d1:%d d2:%d s1:%d s2:%d f1:%d f2:%d\n",d1, d2, s1, s2, f1, f2);
	}

	for (int i = 1; i <= p[0]; ++i) dfs_tr(p[i]);

	for (int i = 1; i < n; ++i) printf("%I64d ",f[i]); printf("%I64d\n",f[n]);
	return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<ctype.h>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<math.h>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<stdio.h>
#include<stdlib.h>
#include<streambuf>
#include<string.h>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
using namespace std;
typedef long long LL;
struct edge{
	int to,cost,id;
};
const int maxn=200010;
int n;
vector<edge> adj[maxn];
vector<int> cir,tmp;
int cirs;
bool vis[maxn],isc[maxn];
int sz[maxn];
int bel[maxn];
LL hve[maxn];
LL adc[maxn];
bool dfs(int i,int fa){
	if (vis[i]){
		int j=0,ssz=tmp.size();
		while(tmp[j]!=i) ++j;
		while(j<ssz){
			cir.push_back(tmp[j]);
			isc[tmp[j]]=1;
			++j;
		}
		cirs=cir.size();
		return 1;
	}
	vis[i]=1;
	tmp.push_back(i);
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to,iid=adj[i][j].id;
		if (iid==fa) continue;
		if (dfs(nxt,iid)) return 1;
	}
	tmp.pop_back();
	return 0;
}
void dfs1(int i,int fa){
	sz[i]=1;
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to;
		if (nxt==fa||isc[nxt]) continue;
		bel[nxt]=bel[i];
		dfs1(nxt,i);
		sz[i]+=sz[nxt];
	}
}
void dfs2(int i,int fa){
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to;
		if (nxt==fa||isc[nxt]) continue;
		dfs2(nxt,i);
		hve[i]+=1ll*sz[nxt]*adj[i][j].cost;
		hve[i]+=hve[nxt];
	}
}
void dfs3(int i,int fa,LL ad){
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to;
		if (nxt==fa||isc[nxt]) continue;
		dfs3(nxt,i,ad+1ll*(n-sz[nxt])*adj[i][j].cost+hve[i]-hve[nxt]-1ll*sz[nxt]*adj[i][j].cost);
	}
	hve[i]+=ad;
}
int main(void){
	scanf("%d",&n);
	{
		int u,v,w;
		for (int i=1;i<=n;++i){
			scanf("%d%d%d",&u,&v,&w);
			adj[u].push_back((edge){v,w,i});
			adj[v].push_back((edge){u,w,i});
		}
	}
	dfs(1,-1);
	for (int i=0;i<cirs;++i){
		bel[cir[i]]=cir[i];
		dfs1(cir[i],-1);
	}
	for (int i=0;i<cirs;++i){
		dfs2(cir[i],-1);
	}
	for (int i=0;i<cirs;++i){
		dfs3(cir[i],-1,0);
	}
	LL tot=0,crs=0;
	vector<int> cd;
	for (int i=0;i<cirs;++i){
		tot+=hve[cir[i]];
		int cur=cir[i],nn=cir[i+1];
		if (i==cirs-1) nn=cir[0];
		for (int j=adj[cur].size()-1;~j;--j){
			int nxt=adj[cur][j].to;
			if (nxt==nn) cd.push_back(adj[cur][j].cost),crs+=adj[cur][j].cost;
		}
	}
	crs>>=1;
	vector<int> cit=cir,cdt=cd;
	for (int i=0;i<cirs;++i){
		cit.push_back(cir[i]);
		cdt.push_back(cd[i]);
	}
	for (int i=0;i<cirs;++i){
		cit.push_back(cir[i]);
		cdt.push_back(cd[i]);
	}
	LL ls=0,rs=0;
	LL lc=0,rc=0;
	int lcnt=0,rcnt=0;
	int lb,rb;
	for (int i=cirs-1;i;--i){
		ls+=cdt[i];
		lc+=ls*sz[cir[i]];
		lcnt+=sz[cir[i]];
	}
	lb=1;
	rb=cirs;
	for (int i=0;i<cirs;++i){
		while(ls>rs+cdt[rb]){
			lc-=ls*sz[cit[lb]];
			lcnt-=sz[cit[lb]];
			ls-=cdt[lb];
			++lb;
			rs+=cdt[rb];
			rcnt+=sz[cit[rb+1]];
			rc+=rs*sz[cit[rb+1]];
			++rb;
		}
		adc[cit[i]]=lc+rc;
		if (i<cirs-1){
			if (rb-cirs==i){
				lc-=ls*sz[cit[lb]];
				lcnt-=sz[cit[lb]];
				ls-=cdt[lb];
				++lb;
				rs+=cdt[rb];
				rcnt+=sz[cit[rb+1]];
				rc+=rs*sz[cit[rb+1]];
				++rb;
			}
			lcnt+=sz[cit[i]];
			lc+=lcnt*cdt[i];
			rc-=rcnt*cdt[i];
			rcnt-=sz[cit[i+1]];
			ls+=cdt[i];
			rs-=cdt[i];
		}
	}
	for (int i=1;i<=n;++i){
		printf("%I64d ",hve[i]+adc[bel[i]]+tot-hve[bel[i]]);
	}
	return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<ctype.h>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<math.h>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<stdio.h>
#include<stdlib.h>
#include<streambuf>
#include<string.h>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
using namespace std;
typedef long long LL;
struct edge{
	int to,cost,id;
};
const int maxn=200010;
int n;
vector<edge> adj[maxn];
vector<int> cir,tmp;
int cirs;
bool vis[maxn],isc[maxn];
int sz[maxn];
int bel[maxn];
LL hve[maxn];
LL adc[maxn];
bool dfs(int i,int fa){
	if (vis[i]){
		int j=0,ssz=tmp.size();
		while(tmp[j]!=i) ++j;
		while(j<ssz){
			cir.push_back(tmp[j]);
			isc[tmp[j]]=1;
			++j;
		}
		cirs=cir.size();
		return 1;
	}
	vis[i]=1;
	tmp.push_back(i);
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to,iid=adj[i][j].id;
		if (iid==fa) continue;
		if (dfs(nxt,iid)) return 1;
	}
	tmp.pop_back();
	return 0;
}
void dfs1(int i,int fa){
	sz[i]=1;
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to;
		if (nxt==fa||isc[nxt]) continue;
		bel[nxt]=bel[i];
		dfs1(nxt,i);
		sz[i]+=sz[nxt];
	}
}
void dfs2(int i,int fa){
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to;
		if (nxt==fa||isc[nxt]) continue;
		dfs2(nxt,i);
		hve[i]+=1ll*sz[nxt]*adj[i][j].cost;
		hve[i]+=hve[nxt];
	}
}
void dfs3(int i,int fa,LL ad){
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to;
		if (nxt==fa||isc[nxt]) continue;
		dfs3(nxt,i,ad+1ll*(sz[bel[i]]-sz[nxt])*adj[i][j].cost+hve[i]-hve[nxt]-1ll*sz[nxt]*adj[i][j].cost);
	}
	hve[i]+=ad;
}
int main(void){
	scanf("%d",&n);
	{
		int u,v,w;
		for (int i=1;i<=n;++i){
			scanf("%d%d%d",&u,&v,&w);
			adj[u].push_back((edge){v,w,i});
			adj[v].push_back((edge){u,w,i});
		}
	}
	dfs(1,-1);
	for (int i=0;i<cirs;++i){
		bel[cir[i]]=cir[i];
		dfs1(cir[i],-1);
	}
	for (int i=0;i<cirs;++i){
		dfs2(cir[i],-1);
	}
	for (int i=0;i<cirs;++i){
		dfs3(cir[i],-1,0);
	}
	LL tot=0,crs=0;
	vector<int> cd;
	for (int i=0;i<cirs;++i){
		tot+=hve[cir[i]];
		int cur=cir[i],nn=cir[i+1];
		if (i==cirs-1) nn=cir[0];
		for (int j=adj[cur].size()-1;~j;--j){
			int nxt=adj[cur][j].to;
			if (nxt==nn) cd.push_back(adj[cur][j].cost),crs+=adj[cur][j].cost;
		}
	}
	crs>>=1;
	vector<int> cit=cir,cdt=cd;
	for (int i=0;i<cirs;++i){
		cit.push_back(cir[i]);
		cdt.push_back(cd[i]);
	}
	for (int i=0;i<cirs;++i){
		cit.push_back(cir[i]);
		cdt.push_back(cd[i]);
	}
	LL ls=0,rs=0;
	LL lc=0,rc=0;
	int lcnt=0,rcnt=0;
	int lb,rb;
	for (int i=cirs-1;i;--i){
		ls+=cdt[i];
		lc+=ls*sz[cir[i]];
		lcnt+=sz[cir[i]];
	}
	lb=1;
	rb=cirs;
	for (int i=0;i<cirs;++i){
		while(ls>rs+cdt[rb]){
			lc-=ls*sz[cit[lb]];
			lcnt-=sz[cit[lb]];
			ls-=cdt[lb];
			++lb;
			rs+=cdt[rb];
			rcnt+=sz[cit[rb+1]];
			rc+=rs*sz[cit[rb+1]];
			++rb;
		}
		adc[cit[i]]=lc+rc;
		if (i<cirs-1){
			if (rb-cirs==i){
				lc-=ls*sz[cit[lb]];
				lcnt-=sz[cit[lb]];
				ls-=cdt[lb];
				++lb;
				rs+=cdt[rb];
				rcnt+=sz[cit[rb+1]];
				rc+=rs*sz[cit[rb+1]];
				++rb;
			}
			lcnt+=sz[cit[i]];
			lc+=lcnt*cdt[i];
			rc-=rcnt*cdt[i];
			rcnt-=sz[cit[i+1]];
			ls+=cdt[i];
			rs-=cdt[i];
		}
	}
	for (int i=1;i<=n;++i){
		printf("%I64d ",hve[i]+adc[bel[i]]+tot-hve[bel[i]]);
	}
	return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<ctype.h>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<math.h>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<stdio.h>
#include<stdlib.h>
#include<streambuf>
#include<string.h>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
using namespace std;
typedef long long LL;
struct edge{
	int to,cost,id;
};
const int maxn=200010;
int n;
vector<edge> adj[maxn];
vector<int> cir,tmp;
int cirs;
bool vis[maxn],isc[maxn];
int sz[maxn];
int bel[maxn];
LL hve[maxn];
LL adc[maxn];
bool dfs(int i,int fa){
	if (vis[i]){
		int j=0,ssz=tmp.size();
		while(tmp[j]!=i) ++j;
		while(j<ssz){
			cir.push_back(tmp[j]);
			isc[tmp[j]]=1;
			++j;
		}
		cirs=cir.size();
		return 1;
	}
	vis[i]=1;
	tmp.push_back(i);
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to,iid=adj[i][j].id;
		if (iid==fa) continue;
		if (dfs(nxt,iid)) return 1;
	}
	tmp.pop_back();
	return 0;
}
void dfs1(int i,int fa){
	sz[i]=1;
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to;
		if (nxt==fa||isc[nxt]) continue;
		bel[nxt]=bel[i];
		dfs1(nxt,i);
		sz[i]+=sz[nxt];
	}
}
void dfs2(int i,int fa){
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to;
		if (nxt==fa||isc[nxt]) continue;
		dfs2(nxt,i);
		hve[i]+=1ll*sz[nxt]*adj[i][j].cost;
		hve[i]+=hve[nxt];
	}
}
void dfs3(int i,int fa,LL ad){
	for (int j=adj[i].size()-1;~j;--j){
		int nxt=adj[i][j].to;
		if (nxt==fa||isc[nxt]) continue;
		dfs3(nxt,i,ad+1ll*(sz[bel[i]]-sz[nxt])*adj[i][j].cost+hve[i]-hve[nxt]-1ll*sz[nxt]*adj[i][j].cost);
	}
	hve[i]+=ad;
}
int main(void){
	scanf("%d",&n);
	{
		int u,v,w;
		for (int i=1;i<=n;++i){
			scanf("%d%d%d",&u,&v,&w);
			adj[u].push_back((edge){v,w,i});
			adj[v].push_back((edge){u,w,i});
		}
	}
	dfs(1,-1);
	for (int i=0;i<cirs;++i){
		bel[cir[i]]=cir[i];
		dfs1(cir[i],-1);
	}
	for (int i=0;i<cirs;++i){
		dfs2(cir[i],-1);
	}
	for (int i=0;i<cirs;++i){
		dfs3(cir[i],-1,0);
	}
	LL tot=0,crs=0;
	vector<int> cd;
	for (int i=0;i<cirs;++i){
		tot+=hve[cir[i]];
		int cur=cir[i],nn=cir[i+1];
		if (i==cirs-1) nn=cir[0];
		for (int j=adj[cur].size()-1;~j;--j){
			int nxt=adj[cur][j].to;
			if (nxt==nn) cd.push_back(adj[cur][j].cost),crs+=adj[cur][j].cost;
		}
	}
	crs>>=1;
	vector<int> cit=cir,cdt=cd;
	for (int i=0;i<cirs;++i){
		cit.push_back(cir[i]);
		cdt.push_back(cd[i]);
	}
	for (int i=0;i<cirs;++i){
		cit.push_back(cir[i]);
		cdt.push_back(cd[i]);
	}
	for (int i=0;i<cirs;++i){
		cit.push_back(cir[i]);
		cdt.push_back(cd[i]);
	}
	LL ls=0,rs=0;
	LL lc=0,rc=0;
	int lcnt=0,rcnt=0;
	int lb,rb;
	for (int i=cirs-1;i;--i){
		ls+=cdt[i];
		lc+=ls*sz[cir[i]];
		lcnt+=sz[cir[i]];
	}
	lb=1;
	rb=cirs;
	for (int i=0;i<cirs;++i){
		while(ls>rs+cdt[rb]){
			lc-=ls*sz[cit[lb]];
			lcnt-=sz[cit[lb]];
			ls-=cdt[lb];
			++lb;
			rs+=cdt[rb];
			rcnt+=sz[cit[rb+1]];
			rc+=rs*sz[cit[rb+1]];
			++rb;
		}
		adc[cit[i]]=lc+rc;
		if (i<cirs-1){
			if (rb-cirs==i){
				lc-=ls*sz[cit[lb]];
				lcnt-=sz[cit[lb]];
				ls-=cdt[lb];
				++lb;
				rs+=cdt[rb];
				rcnt+=sz[cit[rb+1]];
				rc+=rs*sz[cit[rb+1]];
				++rb;
			}
			lcnt+=sz[cit[i]];
			lc+=lcnt*cdt[i];
			rc-=rcnt*cdt[i];
			rcnt-=sz[cit[i+1]];
			ls+=cdt[i];
			rs-=cdt[i];
		}
	}
	for (int i=1;i<=n;++i){
		printf("%I64d ",hve[i]+adc[bel[i]]+tot-hve[bel[i]]);
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < n; i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define y0 y3487465
#define y1 y8687969

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) {
	re x > 0 ? x : -x;
}

int n, r = 0;
int m;
int prev[200001], was[200001], q[200001], ql[200001], pl[200001], cyc[200001], cnt[200001];
ll sum[200001];
ll res[200001];
vii v[200001];

inline int gp (int x) {
	if (x > 0) re x - 1; else re n - 1;
}

inline int gn (int x) {
	if (x + 1 < n) re x + 1; else re 0;
}

inline int sp (int x) {
	re ql[x];
}

inline int sn (int x) {
	if (x + 1 < n) re ql[x + 1]; else re ql[0];
}

int go (int x, int p, int l) {
//	printf ("%d %d %d\n", x, p, l);
	prev[x] = p;
	pl[x] = l;
	was[x] = 1;
	for (int i = 0; i < sz (v[x]); i++) {
		int y = v[x][i].fi;
		int z = v[x][i].se;
		if (was[y] && p != y) {
			r = 0;
			int k = x;
			ql[0] = z;
			while (true) {
				cyc[k] = 1;
				q[r++] = k;
				if (k == y) break;
				ql[r] = pl[k];
				k = prev[k];
			}
			re 1;
		} else
		if (!was[y] && go (y, x, z)) re 1;
	}
	re 0;
}

int go2 (int x, int p, int l) {
	sum[x] = 0;
	cnt[x] = 1;
	for (int i = 0; i < sz (v[x]); i++) {
		int y = v[x][i].fi;
		if (cyc[y]) continue;
		int z = v[x][i].se;
		if (p != y) {
			go2 (y, x, z);
			sum[x] += sum[y] + cnt[y] * z;
			cnt[x] += cnt[y];
		}
	}
	re 0;
}

int go3 (int x, int p, int l) {
	prev[x] = p;
	pl[x] = l;
	if (x != p) res[x] = res[x] - l * cnt[x] + l * (n - cnt[x]);
	for (int i = 0; i < sz (v[x]); i++) {
		int y = v[x][i].fi;
		if (cyc[y]) continue;
		int z = v[x][i].se;
		if (p != y) go3 (y, x, z);
	}
	re 0;
}

int main() {
	scanf ("%d", &n);
	for (int i = 0; i < n; i++) {
		int a, b, c;
		scanf ("%d%d%d", &a, &b, &c); a--; b--;
		v[a].pb (mp (b, c));
		v[b].pb (mp (a, c));
	}
	go (0, 0, -1);
	ll all = 0;
	for (int i = 0; i < r; i++) {
		go2 (q[i], q[i], 0);
		all += sum[q[i]];
//		printf ("%I64d %d %d\n", sum[q[i]], q[i], ql[i]);
	}
	int a = 0, b = 0, sa = 0, sb = 0, ca = 0, cb = 0;
	ll ra = 0, rb = 0;
	while (gp (a) != b)
		if (sa + sp (a) < sb + sn (b)) {
			sa += sp (a);
			a = gp (a);
			ca += cnt[q[a]];
			ra += (ll)sa * cnt[q[a]];
		} else {
			sb += sn (b);
			b = gn (b);
			cb += cnt[q[b]];
			rb += (ll)sb * cnt[q[b]];
		}
	for (int i = 0; i < r; i++) {
		while (sb + sn (b) < sa) {
			ca -= cnt[q[a]];
			cb += cnt[q[a]];
			ra -= (ll)sa * cnt[q[a]];
			sa -= sn (a);
			sb += sn (b);
			rb += (ll)sb * cnt[q[a]];
			a = gn (a);
			b = gn (b);
		}
//		printf ("%d %d %d %d %I64d %I64d %I64d %d %d\n", a, b, sa, sb, ra, rb, all, ca, cb);
		res[q[i]] = all + ra + rb;
		go3 (q[i], q[i], 0);
		if (cb == 0) {
			ca -= cnt[q[a]];
			cb += cnt[q[a]];
			ra -= (ll)sa * cnt[q[a]];
			sa -= sn (a);
			sb += sn (b);
			rb += (ll)sb * cnt[q[a]];
			a = gn (a);
			b = gn (b);
//			printf ("go %d %d %d %d %I64d %I64d %I64d %d %d\n", a, b, sa, sb, ra, rb, all, ca, cb);
		}
		ra += (ll)(ca + cnt[q[i]]) * sn (i);
		rb -= (ll)cb * sn (i);
		ca += cnt[q[i]];
		cb -= cnt[q[gn (i)]];
//		printf ("%d\n", sn (i));
		sa += sn (i);
		sb -= sn (i);
	}
	for (int i = 0; i < n; i++) printf ("%I64d ", res[i]);
	printf ("\n");
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < n; i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define y0 y3487465
#define y1 y8687969

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) {
	re x > 0 ? x : -x;
}

int n, r = 0;
int m;
int prev[200001], was[200001], q[200001], ql[200001], pl[200001], cyc[200001], cnt[200001];
ll sum[200001];
ll res[200001];
vii v[200001];

inline int gp (int x) {
	if (x > 0) re x - 1; else re r - 1;
}

inline int gn (int x) {
	if (x + 1 < r) re x + 1; else re 0;
}

inline int sp (int x) {
	re ql[x];
}

inline int sn (int x) {
	if (x + 1 < r) re ql[x + 1]; else re ql[0];
}

int go (int x, int p, int l) {
//	printf ("%d %d %d\n", x, p, l);
	prev[x] = p;
	pl[x] = l;
	was[x] = 1;
	for (int i = 0; i < sz (v[x]); i++) {
		int y = v[x][i].fi;
		int z = v[x][i].se;
		if (was[y] && p != y) {
			r = 0;
			int k = x;
			ql[0] = z;
			while (true) {
				cyc[k] = 1;
				q[r++] = k;
				if (k == y) break;
				ql[r] = pl[k];
				k = prev[k];
			}
			re 1;
		} else
		if (!was[y] && go (y, x, z)) re 1;
	}
	re 0;
}

int go2 (int x, int p, int l) {
	sum[x] = 0;
	cnt[x] = 1;
	for (int i = 0; i < sz (v[x]); i++) {
		int y = v[x][i].fi;
		if (cyc[y]) continue;
		int z = v[x][i].se;
		if (p != y) {
			go2 (y, x, z);
			sum[x] += sum[y] + cnt[y] * z;
			cnt[x] += cnt[y];
		}
	}
	re 0;
}

int go3 (int x, int p, int l) {
	prev[x] = p;
	pl[x] = l;
	if (x != p) res[x] = res[p] - l * cnt[x] + l * (n - cnt[x]);
	for (int i = 0; i < sz (v[x]); i++) {
		int y = v[x][i].fi;
		if (cyc[y]) continue;
		int z = v[x][i].se;
		if (p != y) go3 (y, x, z);
	}
	re 0;
}

int main() {
	scanf ("%d", &n);
	for (int i = 0; i < n; i++) {
		int a, b, c;
		scanf ("%d%d%d", &a, &b, &c); a--; b--;
		v[a].pb (mp (b, c));
		v[b].pb (mp (a, c));
	}
	go (0, 0, -1);
	ll all = 0;
	for (int i = 0; i < r; i++) {
		go2 (q[i], q[i], 0);
		all += sum[q[i]];
//		printf ("%I64d %d %d\n", sum[q[i]], q[i], ql[i]);
	}
	int a = 0, b = 0, sa = 0, sb = 0, ca = 0, cb = 0;
	ll ra = 0, rb = 0;
	while (gp (a) != b)
		if (sa + sp (a) < sb + sn (b)) {
			sa += sp (a);
			a = gp (a);
			ca += cnt[q[a]];
			ra += (ll)sa * cnt[q[a]];
//			printf ("ra += %d * %d\n", sa, cnt[q[a]]);
		} else {
			sb += sn (b);
			b = gn (b);
			cb += cnt[q[b]];
			rb += (ll)sb * cnt[q[b]];
		}
	for (int i = 0; i < r; i++) {
		while (sb + sn (b) < sa) {
			ca -= cnt[q[a]];
			cb += cnt[q[a]];
			ra -= (ll)sa * cnt[q[a]];
			sa -= sn (a);
			sb += sn (b);
			rb += (ll)sb * cnt[q[a]];
			a = gn (a);
			b = gn (b);
		}
//		printf ("%d %d | %d %d | %I64d %I64d %I64d | %d %d\n", a, b, sa, sb, ra, rb, all, ca, cb);
		res[q[i]] = all + ra + rb;
//		printf ("%d = %I64d\n", q[i] + 1, res[q[i]]);
		go3 (q[i], q[i], 0);
		if (cb == 0) {
			ca -= cnt[q[a]];
			cb += cnt[q[a]];
			ra -= (ll)sa * cnt[q[a]];
			sa -= sn (a);
			sb += sn (b);
			rb += (ll)sb * cnt[q[a]];
			a = gn (a);
			b = gn (b);
//			printf ("go %d %d %d %d %I64d %I64d %I64d %d %d\n", a, b, sa, sb, ra, rb, all, ca, cb);
		}
		ra += (ll)(ca + cnt[q[i]]) * sn (i);
		rb -= (ll)cb * sn (i);
		ca += cnt[q[i]];
		cb -= cnt[q[gn (i)]];
//		printf ("%d\n", sn (i));
		sa += sn (i);
		sb -= sn (i);
	}
	for (int i = 0; i < n; i++) printf ("%I64d ", res[i]);
	printf ("\n");
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#define pii pair<int,int>
#define A first
#define B second
using namespace std;
vector <pii> e[200001];
int n,isc[200001],tag[200001],num[200001],m,h[200001];
pii ord[600001];
long long ans[200001],x1[600002],x2[600002],ll[600002],lr[600002],s[600002],sv[200001],sum;
int dfs1(int now,int head){
    tag[now] = 1;
    for(int i=0;i<e[now].size();i++)
        if(e[now][i].A != head){
            if(tag[e[now][i].A] == 1){
                isc[now] = 1;
                ord[m++] = pii(e[now][i].A,e[now][i].B);
                return e[now][i].A;
            }
            int tmp = dfs1(e[now][i].A,now);
            if(tmp != 0){
                isc[now] = 1;
                ord[m++] = pii(e[now][i].A,e[now][i].B);
                if(tmp == now)
                    tmp = 0;
                return tmp;
            }
        }
    return 0;
}
void dfs2(int now,int head,int svh){
    num[now] = 1; h[now] = svh;
    for(int i=0;i<e[now].size();i++)
        if(e[now][i].A != head && isc[e[now][i].A] != 1){
            dfs2(e[now][i].A,now,svh);
            num[now] += num[e[now][i].A];
            ans[now] += 1ll*num[e[now][i].A]*e[now][i].B;
            ans[now] += ans[e[now][i].A];
        }
}
void dfs3(int now,int head){
    for(int i=0;i<e[now].size();i++)
        if(e[now][i].A != head && isc[e[now][i].A] != 1){
            ans[e[now][i].A] += ans[now]-ans[e[now][i].A]-1ll*num[e[now][i].A]*e[now][i].B;
            ans[e[now][i].A] += 1ll*e[now][i].B*(n-num[e[now][i].A]);
            dfs3(e[now][i].A,now);
        }
}
long long cal(int f,int k){
    return x2[f]-x2[k]-(s[f]-s[k])*lr[k] + x1[f+m-1]-x1[k]-(s[k+1]-s[f+m])*ll[k];
}
int main(){
    scanf("%d",&n);
    for(int i=1,u,v,w;i<=n;i++){
        scanf("%d%d%d",&u,&v,&w);
        e[u].push_back(pii(v,w));
        e[v].push_back(pii(u,w));
    }
    dfs1(1,0);
    for(int i=1;i<=n;i++)
        if(isc[i] == 1)
            dfs2(i,0,i), dfs3(i,0), sum += ans[i];
    for(int i=0;i<m;i++) ord[i+m] = ord[i+2*m] = ord[i];
    for(int i=3*m;i>0;i--){
        ord[i] = ord[i-1];
        lr[i] = lr[i+1] + ord[i].B;
        s[i] = s[i+1] + num[ord[i].A];
        x2[i] = x2[i+1] + 1ll*num[ord[i].A]*lr[i];
    }
    for(int i=2;i<=3*m;i++){
        ll[i] = ll[i-1]+ord[i-1].B;
        x1[i] = x1[i-1]+ 1ll*num[ord[i].A]*ll[i];
    }
    int r = 2;
    for(int i=m+1;i<=2*m;i++){
        while(cal(r,i) >= cal(r+1,i) && r < i) r++;
        sv[ord[i].A] = cal(r,i);
    }
    for(int i=1;i<=n;i++) if(!isc[i]) ans[i] += sum-ans[h[i]];
    for(int i=1;i<=n;i++) if(isc[i]) ans[i] += sum-ans[h[i]];
    for(int i=1;i<=n;i++) ans[i] += sv[h[i]];
    for(int i=1;i<=n;i++) printf("%I64d ",ans[i]);
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
const int N=2e5+500 ;
int n,tot,To[N*2],nx[N*2],lst[N],w[N*2],fa[N],fae[N],num,
	q[N*3],sz[N] ;
bool cir[N] ;
ll f[N],g[N],ws[N*3],all=0 ;
bool vis[N] ;
void add(int x,int y,int z) {
	++tot ; To[tot]=y,nx[tot]=lst[x],lst[x]=tot,w[tot]=z ;
}
void findcir(int x) {
	vis[x]=1 ;
	for (int i=lst[x];i;i=nx[i]) if (To[i]^fa[x]) {
		if (!vis[To[i]]) {
			fa[To[i]]=x ;
			fae[To[i]]=w[i] ;
			findcir(To[i]) ;
			if (num) return ;
		}
		else {
			for (int t=x;t!=To[i];t=fa[t]) q[++num]=t ;
			q[++num]=To[i] ; fae[To[i]]=w[i] ;
			return ;
		}
	}
}
void dfs1(int t,int fa) {
	sz[t]=1 ;
	for (int i=lst[t];i;i=nx[i]) if (To[i]!=fa && !cir[To[i]]) {
		fae[To[i]]=w[i] ; dfs1(To[i],t) ;
		f[t]+=f[To[i]]+sz[To[i]]*w[i] ;
		sz[t]+=sz[To[i]] ;
	}
}
void dfs2(int t,int fa) {
	g[t]+=g[fa]+f[fa]-(f[t]+sz[t]*fae[t])+(n-sz[t])*fae[t] ;
	for (int i=lst[t];i;i=nx[i]) if (To[i]^fa)
		dfs2(To[i],t) ;
}
ll dis(int x,int y) { return ws[y]-ws[x] ; }
int main() {
	scanf("%d",&n) ;
	rep(i,1,n) {
		int x,y,z ; scanf("%d%d%d",&x,&y,&z) ;
		add(x,y,z) ; add(y,x,z) ;
	}
	findcir(1) ;
	rep(i,1,num/2) swap(q[i],q[num+1-i]) ;
	rep(i,1,num) {
		q[i+num]=q[i+num*2]=q[i] ;
		all+=fae[q[i]] ;
		cir[q[i]]=1 ;
	}
	rep(i,1,num) dfs1(q[i],0) ;
//	rep(i,1,num) printf("%d %d\n",q[i],fae[q[i]]) ;
	ws[0]=0 ;
	rep(i,1,num*3) ws[i]=ws[i-1]+fae[q[i]] ;
	int l=num+1,r=num+1,A=0,B=0 ; ll SA=0,SB=0 ;
	for (;dis(l-1,num+1)*2<=all;--l) {
		A+=sz[q[l-1]] ;
		SA+=f[q[l-1]]+dis(l-1,num+1)*sz[q[l-1]] ;
	}
	for (;dis(num+1,r+1)*2<all;++r) {
		B+=sz[q[r+1]] ;
		SB+=f[q[r+1]]+dis(num+1,r+1)*sz[q[r+1]] ;
	}
	rep(i,num+1,num*2) {
		for (;dis(l,i)*2>all;++l) {
			A-=sz[q[l]] ;
			SA-=f[q[l]]+dis(l,i)*sz[q[l]] ;
		}
		for (;dis(i,r+1)*2<all;++r) {
			B+=sz[q[r+1]] ;
			SB+=f[q[r+1]]+dis(i,r+1)*sz[q[r+1]] ;
		}
//		printf("%d %d %d : %lld %lld\n",i,l,r,SA,SB) ;
		g[q[i]]=SA+SB ;
		A+=sz[q[i]] ;
		SA+=f[q[i]]+A*fae[q[i+1]] ;
		if (r>i) {
			SB-=f[q[i+1]]+B*fae[q[i+1]] ;
			B-=sz[q[i+1]] ;
		} else r=i+1 ;
	}
	rep(i,1,num) for (int j=lst[q[i]];j;j=nx[j])
		if (!cir[To[j]]) dfs2(To[j],q[i]) ;
	rep(i,1,n) printf("%I64d ",f[i]+g[i]) ; puts("") ;
	return 0 ;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
const int N=2e5+600 ;
int n,tot,To[N*2],nx[N*2],lst[N],w[N*2],fa[N],fae[N],num,
	q[N*3],sz[N] ;
bool cir[N] ;
ll f[N],g[N],ws[N*3],all=0 ;
bool vis[N] ;
void add(int x,int y,int z) {
	++tot ; To[tot]=y,nx[tot]=lst[x],lst[x]=tot,w[tot]=z ;
}
void findcir(int x) {
	vis[x]=1 ;
	for (int i=lst[x];i;i=nx[i]) if (To[i]^fa[x]) {
		if (!vis[To[i]]) {
			fa[To[i]]=x ;
			fae[To[i]]=w[i] ;
			findcir(To[i]) ;
			if (num) return ;
		}
		else {
			for (int t=x;t!=To[i];t=fa[t]) q[++num]=t ;
			q[++num]=To[i] ; fae[To[i]]=w[i] ;
			return ;
		}
	}
}
void dfs1(int t,int fa) {
	sz[t]=1 ;
	for (int i=lst[t];i;i=nx[i]) if (To[i]!=fa && !cir[To[i]]) {
		fae[To[i]]=w[i] ; dfs1(To[i],t) ;
		f[t]+=f[To[i]]+sz[To[i]]*w[i] ;
		sz[t]+=sz[To[i]] ;
	}
}
void dfs2(int t,int fa) {
	g[t]+=g[fa]+f[fa]-(f[t]+sz[t]*fae[t])+(n-sz[t])*fae[t] ;
	for (int i=lst[t];i;i=nx[i]) if (To[i]^fa)
		dfs2(To[i],t) ;
}
ll dis(int x,int y) { return ws[y]-ws[x] ; }
int main() {
	scanf("%d",&n) ;
	rep(i,1,n) {
		int x,y,z ; scanf("%d%d%d",&x,&y,&z) ;
		add(x,y,z) ; add(y,x,z) ;
	}
	findcir(1) ;
	rep(i,1,num/2) swap(q[i],q[num+1-i]) ;
	rep(i,1,num) {
		q[i+num]=q[i+num*2]=q[i] ;
		all+=fae[q[i]] ;
		cir[q[i]]=1 ;
	}
	rep(i,1,num) dfs1(q[i],0) ;
//	rep(i,1,num) printf("%d %d\n",q[i],fae[q[i]]) ;
	ws[0]=0 ;
	rep(i,1,num*3) ws[i]=ws[i-1]+fae[q[i]] ;
	int l=num+1,r=num+1,A=0,B=0 ; ll SA=0,SB=0 ;
	for (;dis(l-1,num+1)*2<=all;--l) {
		A+=sz[q[l-1]] ;
		SA+=f[q[l-1]]+dis(l-1,num+1)*sz[q[l-1]] ;
	}
	for (;dis(num+1,r+1)*2<all;++r) {
		B+=sz[q[r+1]] ;
		SB+=f[q[r+1]]+dis(num+1,r+1)*sz[q[r+1]] ;
	}
	rep(i,num+1,num*2) {
		for (;dis(l,i)*2>all;++l) {
			A-=sz[q[l]] ;
			SA-=f[q[l]]+dis(l,i)*sz[q[l]] ;
		}
		for (;dis(i,r+1)*2<all;++r) {
			B+=sz[q[r+1]] ;
			SB+=f[q[r+1]]+dis(i,r+1)*sz[q[r+1]] ;
		}
//		printf("%d %d %d : %lld %lld\n",i,l,r,SA,SB) ;
		g[q[i]]=SA+SB ;
		A+=sz[q[i]] ;
		SA+=f[q[i]]+A*fae[q[i+1]] ;
		if (r>i) {
			SB-=f[q[i+1]]+B*fae[q[i+1]] ;
			B-=sz[q[i+1]] ;
		} else r=i+1 ;
	}
	rep(i,1,num) for (int j=lst[q[i]];j;j=nx[j])
		if (!cir[To[j]]) dfs2(To[j],q[i]) ;
	rep(i,1,n) printf("%I64d ",f[i]+g[i]) ; puts("") ;
	return 0 ;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
const int N=1e5+500 ;
int n,tot,To[N*2],nx[N*2],lst[N],w[N*2],fa[N],fae[N],num,
	q[N*3],sz[N] ;
bool cir[N] ;
ll f[N],g[N],ws[N*3],all=0 ;
bool vis[N] ;
void add(int x,int y,int z) {
	++tot ; To[tot]=y,nx[tot]=lst[x],lst[x]=tot,w[tot]=z ;
}
void findcir(int x) {
	vis[x]=1 ;
	for (int i=lst[x];i;i=nx[i]) if (To[i]^fa[x]) {
		if (!vis[To[i]]) {
			fa[To[i]]=x ;
			fae[To[i]]=w[i] ;
			findcir(To[i]) ;
			if (num) return ;
		}
		else {
			for (int t=x;t!=To[i];t=fa[t]) q[++num]=t ;
			q[++num]=To[i] ; fae[To[i]]=w[i] ;
			return ;
		}
	}
}
void dfs1(int t,int fa) {
	sz[t]=1 ;
	for (int i=lst[t];i;i=nx[i]) if (To[i]!=fa && !cir[To[i]]) {
		fae[To[i]]=w[i] ; dfs1(To[i],t) ;
		f[t]+=f[To[i]]+sz[To[i]]*w[i] ;
		sz[t]+=sz[To[i]] ;
	}
}
void dfs2(int t,int fa) {
	g[t]+=g[fa]+f[fa]-(f[t]+sz[t]*fae[t])+(n-sz[t])*fae[t] ;
	for (int i=lst[t];i;i=nx[i]) if (To[i]^fa)
		dfs2(To[i],t) ;
}
ll dis(int x,int y) { return ws[y]-ws[x] ; }
int main() {
	scanf("%d",&n) ;
	rep(i,1,n) {
		int x,y,z ; scanf("%d%d%d",&x,&y,&z) ;
		add(x,y,z) ; add(y,x,z) ;
	}
	findcir(1) ;
	rep(i,1,num/2) swap(q[i],q[num+1-i]) ;
	rep(i,1,num) {
		q[i+num]=q[i+num*2]=q[i] ;
		all+=fae[q[i]] ;
		cir[q[i]]=1 ;
	}
	rep(i,1,num) dfs1(q[i],0) ;
//	rep(i,1,num) printf("%d %d\n",q[i],fae[q[i]]) ;
	ws[0]=0 ;
	rep(i,1,num*3) ws[i]=ws[i-1]+fae[q[i]] ;
	int l=num+1,r=num+1,A=0,B=0 ; ll SA=0,SB=0 ;
	for (;dis(l-1,num+1)*2<=all;--l) {
		A+=sz[q[l-1]] ;
		SA+=f[q[l-1]]+dis(l-1,num+1)*sz[q[l-1]] ;
	}
	for (;dis(num+1,r+1)*2<all;++r) {
		B+=sz[q[r+1]] ;
		SB+=f[q[r+1]]+dis(num+1,r+1)*sz[q[r+1]] ;
	}
	rep(i,num+1,num*2) {
		for (;dis(l,i)*2>all;++l) {
			A-=sz[q[l]] ;
			SA-=f[q[l]]+dis(l,i)*sz[q[l]] ;
		}
		for (;dis(i,r+1)*2<all;++r) {
			B+=sz[q[r+1]] ;
			SB+=f[q[r+1]]+dis(i,r+1)*sz[q[r+1]] ;
		}
//		printf("%d %d %d : %lld %lld\n",i,l,r,SA,SB) ;
		g[q[i]]=SA+SB ;
		A+=sz[q[i]] ;
		SA+=f[q[i]]+A*fae[q[i+1]] ;
		if (r>i) {
			SB-=f[q[i+1]]+B*fae[q[i+1]] ;
			B-=sz[q[i+1]] ;
		} else r=i+1 ;
	}
	rep(i,1,num) for (int j=lst[q[i]];j;j=nx[j])
		if (!cir[To[j]]) dfs2(To[j],q[i]) ;
	rep(i,1,n) printf("%I64d ",f[i]+g[i]) ; puts("") ;
	return 0 ;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int const N=200005;
struct Edge
{
    int u,c,next;
    void read(int u,int c,int next)
    {
        this->u=u;
        this->c=c;
        this->next=next;
    }
}edge[N*2];
int son[N],rcd_ord[N],sonNum[N],hash[N],tree_root[N];
long long tree_val[N],rcd_edge[N],circle_val[N],son_root[N];
int n,m,st,ed;
bool vis[N];
bool FindCircle(int x,int fa,int k)
{
    if(vis[x]==true){st=hash[x];ed=k-2;return true;}
    vis[x]=true;
    int t=son[x],u;
    while(t)
    {
         u=edge[t].u;
         if(u!=fa)
         {
            rcd_ord[k]=u;
            if(!vis[u])hash[u]=k;
            rcd_edge[k-1]=edge[t].c;
            if(FindCircle(u,x,k+1))return true;
         }
         t=edge[t].next;
    }
    return false;
}
void dfs1(int x,int root,long long length)
{
    tree_root[x]=root;
    son_root[x]=length;
    vis[x]=true;
    sonNum[x]=1;
    tree_val[x]=0;
    int t=son[x],u;
    while(t)
    {
         u=edge[t].u;
         if(!vis[u])
         {
              dfs1(u,root,length+edge[t].c);
              sonNum[x]+=sonNum[u];
              tree_val[x]+=tree_val[u]+edge[t].c*sonNum[u];
         }
         t=edge[t].next;
    }

    if(x!=root)vis[x]=false;
}
void dfs2(int x,int fa,int length)
{
    vis[x]=true;
    if(x!=fa)
    {
         tree_val[x]=tree_val[fa]+(sonNum[tree_root[x]]-2*sonNum[x])*length;
    }

    int t=son[x],u;
    while(t)
    {
         u=edge[t].u;
         if(!vis[u])
         {
              dfs2(u,x,edge[t].c);
         }
         t=edge[t].next;
    }

    if(x!=fa)vis[x]=false;
}
void calc_tree()
{
    memset(vis,0,sizeof(vis));
    for(int i=st;i<=ed;i++)
    vis[rcd_ord[i]]=true;

    for(int i=st;i<=ed;i++)
    {
        dfs1(rcd_ord[i],rcd_ord[i],0);

        dfs2(rcd_ord[i],rcd_ord[i],0);
    }
}
void calc_circle()
{
    int p=st,num=0,x;
    long long tot=0,c=0;
    for(int i=st;i<=ed;i++)
    tot+=rcd_edge[i];

    x=rcd_ord[st];
    circle_val[x]=0;
    while(p+1<=ed&&c+rcd_edge[p]<=tot/2)
    {
        c+=rcd_edge[p];
        circle_val[x]+=c*sonNum[rcd_ord[++p]];
        num+=sonNum[rcd_ord[p]];
    }
    long long t=c;
    for(int i=p+1;i<=ed;i++)
    {
         t+=rcd_edge[i-1];
         circle_val[x]+=(tot-t)*sonNum[rcd_ord[i]];
    }

    for(int k=st+1;k<=ed;k++)
    {
         x=rcd_ord[k];
         circle_val[x]=circle_val[rcd_ord[k-1]]+(n-2*num)*rcd_edge[k-1];
         num-=sonNum[rcd_ord[k]];
         c-=rcd_edge[k-1];
         while(true)
         {
             int pp=p+1;
             if(pp==ed+1)pp=st;
             if(c+rcd_edge[p]<=tot/2)
             {
                    c+=rcd_edge[p];
                    circle_val[x]+=(2*c-tot)*sonNum[rcd_ord[pp]];
                    num+=sonNum[rcd_ord[pp]];
                    p=pp;
             }
             else break;
        }
    }
}
void solve()
{
     long long tot_root=0,ans;
     for(int i=st;i<=ed;i++)
     tot_root+=tree_val[rcd_ord[i]];
     for(int i=1;i<=n;i++)
     {
          ans=tot_root+tree_val[i]-tree_val[tree_root[i]]+(n-sonNum[tree_root[i]])*son_root[i]+circle_val[tree_root[i]];
          printf("%I64d ",ans);
     }
}
int main()
{

    int a,b,c;
    scanf("%d",&n);

    for(int i=1;i<=n;i++)
    {
       scanf("%d%d%d",&a,&b,&c);
       edge[i*2-1].read(b,c,son[a]);son[a]=i*2-1;
       edge[i*2  ].read(a,c,son[b]);son[b]=i*2  ;
    }
    rcd_ord[1]=hash[1]=1;
    FindCircle(1,-1,2);
    rcd_edge[st-1]=rcd_edge[ed];

    calc_tree();
    calc_circle();

    solve();

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int const N=200005;
struct Edge
{
    int u,c,next;
    void read(int u,int c,int next)
    {
        this->u=u;
        this->c=c;
        this->next=next;
    }
}edge[N*2];
int son[N],rcd_ord[N],sonNum[N],hash[N],tree_root[N];
long long tree_val[N],rcd_edge[N],circle_val[N];
int n,m,st,ed;
bool vis[N];
bool FindCircle(int x,int fa,int k)
{
    if(vis[x]==true){st=hash[x];ed=k-2;return true;}
    vis[x]=true;
    int t=son[x],u;
    while(t)
    {
         u=edge[t].u;
         if(u!=fa)
         {
            rcd_ord[k]=u;
            if(!vis[u])hash[u]=k;
            rcd_edge[k-1]=edge[t].c;
            if(FindCircle(u,x,k+1))return true;
         }
         t=edge[t].next;
    }
    return false;
}
void dfs1(int x,int root)
{
    tree_root[x]=root;
    vis[x]=true;
    sonNum[x]=1;
    tree_val[x]=0;
    int t=son[x],u;
    while(t)
    {
         u=edge[t].u;
         if(!vis[u])
         {
              dfs1(u,root);
              sonNum[x]+=sonNum[u];
              tree_val[x]+=tree_val[u]+edge[t].c*sonNum[u];
         }
         t=edge[t].next;
    }

    if(x!=root)vis[x]=false;
}
void dfs2(int x,int fa,int length)
{
    vis[x]=true;
    if(x!=fa)
    {
         tree_val[x]=tree_val[fa]+(sonNum[tree_root[x]]-2*sonNum[x])*length;
    }

    int t=son[x],u;
    while(t)
    {
         u=edge[t].u;
         if(!vis[u])
         {
              dfs2(u,x,edge[t].c);
         }
         t=edge[t].next;
    }

    if(x!=fa)vis[x]=false;
}
void calc_tree()
{
    memset(vis,0,sizeof(vis));
    for(int i=st;i<=ed;i++)
    vis[rcd_ord[i]]=true;

    for(int i=st;i<=ed;i++)
    {
        dfs1(rcd_ord[i],rcd_ord[i]);

        dfs2(rcd_ord[i],rcd_ord[i],0);
    }
}
void calc_circle()
{
    int p=st,num=0,x;
    long long tot=0,c=0;
    for(int i=st;i<=ed;i++)
    tot+=rcd_edge[i];

    x=rcd_ord[st];
    circle_val[x]=0;
    while(p+1<=ed&&c+rcd_edge[p]<=tot/2)
    {
        c+=rcd_edge[p];
        circle_val[x]+=c*sonNum[rcd_ord[++p]];
        num+=sonNum[rcd_ord[p]];
    }
    long long t=c;
    for(int i=p+1;i<=ed;i++)
    {
         t+=rcd_edge[i-1];
         circle_val[x]+=(tot-t)*sonNum[rcd_ord[i]];
    }

    for(int k=st+1;k<=ed;k++)
    {
         x=rcd_ord[k];
         circle_val[x]=circle_val[rcd_ord[k-1]]+(n-2*num)*rcd_edge[k-1];
         num-=sonNum[rcd_ord[k]];
         c-=rcd_edge[k-1];
         while(true)
         {
             int pp=p+1;
             if(pp==ed+1)pp=st;
             if(c+rcd_edge[p]<=tot/2)
             {
                    c+=rcd_edge[p];
                    circle_val[x]+=(2*c-tot)*sonNum[rcd_ord[pp]];
                    num+=sonNum[rcd_ord[pp]];
                    p=pp;
             }
             else break;
        }
    }
}
void solve()
{
     long long tot_root=0,ans;
     for(int i=st;i<=ed;i++)
     tot_root+=tree_val[rcd_ord[i]];
     for(int i=1;i<=n;i++)
     {
          ans=tot_root+tree_val[i]+circle_val[tree_root[i]];
          printf("%I64d ",ans);
     }
}
int main()
{

    int a,b,c;
    scanf("%d",&n);

    for(int i=1;i<=n;i++)
    {
       scanf("%d%d%d",&a,&b,&c);
       edge[i*2-1].read(b,c,son[a]);son[a]=i*2-1;
       edge[i*2  ].read(a,c,son[b]);son[b]=i*2  ;
    }
    rcd_ord[1]=hash[1]=1;
    FindCircle(1,-1,2);
    rcd_edge[st-1]=rcd_edge[ed];

    calc_tree();
    calc_circle();

    solve();

    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#define MAXN 200050
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
using namespace std;
typedef long long LL;
vector< pair<int,int> >g[MAXN];
int m,cir[MAXN*3];
LL len[MAXN*3],sum[MAXN*3];
int n,pre[MAXN],deg[MAXN];
LL all,size[MAXN],sum_size[MAXN*3],dep[MAXN],dp1[MAXN],dp2[MAXN],dp3[MAXN];
bool on_cir[MAXN],flag[MAXN];
int head,tail,queue[MAXN];
int root;
int find(int u)
{
    if(pre[u]^u)
        pre[u]=find(pre[u]);
    return pre[u];
}
void make_cir()
{
    memset(flag,false,sizeof(flag));
    head=tail=0;
    for(int i=1;i<=n;i++)
        if(deg[i]==1){
            queue[tail++]=i;
            flag[i]=true;
        }
    while(head<tail){
        int u=queue[head++];
        on_cir[u]=false;
        for(int i=0;i<g[u].size();i++){
            int v=g[u][i].ft;
            deg[v]--;
            if(!flag[v]&&deg[v]==1){
                flag[v]=true;
                queue[tail++]=v;
            }
        }
    }
    for(int i=1;i<=n;i++)
        if(on_cir[i])
            root=i;
    memset(flag,false,sizeof(flag));
    cir[0]=root;
    m=1;
    for(int i=0;i<m;i++){
        int u=cir[i];
        if(i&&u==cir[0]){
            m--;
            break;
        }
        for(int j=0;j<g[u].size();j++){
            int v=g[u][j].ft;
            int w=g[u][j].sc;
            if(on_cir[v]&&(!i||cir[i-1]!=v)){
                cir[m++]=v;
                len[m-1]=w;
                break;
            }
        }
    }
    for(int i=0;i<m;i++){
        cir[i+m]=cir[i+m+m]=cir[i];
        len[i+m+1]=len[i+m+m+1]=len[i+1];
    }
}
void dfs1(int u,LL down)
{
    dep[u]=down;
    pre[u]=u;
    flag[u]=true;
    size[u]=1;
    dp1[u]=0;
    for(int i=0;i<g[u].size();i++){
        int v=g[u][i].ft;
        int w=g[u][i].sc;
        if(!flag[v]&&!on_cir[v]){
            dfs1(v,down+w);
            pre[v]=u;
            size[u]+=size[v];
            dp1[u]+=size[v]*w+dp1[v];
        }
    }
}
void dfs2(int u,LL down)
{
    flag[u]=true;
    dp2[u]=down;
    for(int i=0;i<g[u].size();i++){
        int v=g[u][i].ft;
        int w=g[u][i].sc;
        if(!flag[v]&&!on_cir[v])
            dfs2(v,down+(n-size[v])*w+dp1[u]-dp1[v]-size[v]*w);
    }
}
void cal_cir()
{
    int l;
    sum[0]=0;
    sum_size[0]=0;
    for(int i=0;i<m+m+m;i++){
        sum[i+1]=sum[i]+len[i+1];
        sum_size[i+1]=size[cir[i]]+sum_size[i];
    }
    l=0;
    dp3[cir[m-1]]=0;
    for(int i=0;i<m-1;i++)
        dp3[cir[m-1]]+=size[cir[i]]*(sum[m-1]-sum[i]);
    while(l<m-1&&sum[l+m]-sum[m-1]<sum[m-1]-sum[l]){
        dp3[cir[m-1]]-=(sum[m-1]-sum[l])*size[cir[l]];
        dp3[cir[m-1]]+=(sum[l+m]-sum[m-1])*size[cir[l]];
        l++;
    }
    for(int i=m;i<m+m;i++){
        int u=cir[i];
        int last=cir[i-1];
        dp3[u]=dp3[last]+((sum_size[i]-sum_size[l])-(sum_size[l+m]-sum_size[i]))*len[i];
        while(l<i&&sum[l+m]-sum[i]<sum[i]-sum[l]){
            dp3[u]-=(sum[i]-sum[l])*size[cir[l]];
            dp3[u]+=(sum[l+m]-sum[i])*size[cir[l]];
            l++;
        }
    }
}
void solve()
{
    make_cir();
    all=0;
    memset(flag,false,sizeof(flag));
    for(int i=1;i<=n;i++)
        if(on_cir[i]){
            dfs1(i,0);
            all+=dp1[i];
        }
    memset(flag,false,sizeof(flag));
    for(int i=1;i<=n;i++)
        if(on_cir[i])
            dfs2(i,0);
    cal_cir();
    for(int i=1;i<=n;i++)
        printf("%I64d%c",dp1[i]+all-dp1[find(i)]+dp2[i]+dp3[find(i)],i<n?' ':'\n');
}
int main()
{
    int u,v,w;
    while(scanf("%d",&n)==1){
        for(int i=1;i<=n;i++){
            g[i].clear();
            on_cir[i]=true;
            deg[i]=0;
        }
        for(int i=0;i<n;i++){
            scanf("%d%d%d",&u,&v,&w);
            g[u].pb(mp(v,w));
            g[v].pb(mp(u,w));
            deg[u]++;
            deg[v]++;
        }
        solve();
    }
    return 0;
}