#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;

const int maxn = 200000 + 10;
int n;
int deg[maxn];
bool onCircle[maxn];
bool visit[maxn];
struct edge {
	int t, c;
	edge (int _t, int _c):
		t(_t), c(_c){}
};
vector<edge> E[maxn];
typedef vector<edge>::iterator vet;
typedef long long int64;
int size[maxn];
int64 ans[maxn], w[maxn];

void dfs(int x) {
	visit[x] = true, size[x] = 1, w[x] = 0;
	for (vet e = E[x].begin(); e != E[x].end(); ++e)
		if (!onCircle[e->t] && !visit[e->t]) {
			dfs(e->t);
			size[x] += size[e->t];
			w[x] += e->c * size[e->t] + w[e->t];
		}
}

void findCircle() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		int s, t, c;
		scanf("%d%d%d", &s, &t, &c);
		E[s].push_back(edge(t, c));
		E[t].push_back(edge(s, c));
		++deg[s];
		++deg[t];
	}
	static int q[maxn], r = 0;
	for (int i = 1; i <= n; ++i)
		if (deg[i] == 1)
			q[r++] = i;
	memset(onCircle, 1, sizeof(onCircle));
	memset(visit, 0, sizeof(visit));
	for (int i = 0; i < r; ++i) {
		int x = q[i];
		onCircle[x] = false;
		visit[x] = true;
		for (vet e = E[x].begin(); e != E[x].end(); ++e)
			if (!visit[e->t]) {
				--deg[e->t];
				if (deg[e->t] == 1)
					q[r++] = e->t;
			}
	}
	memset(visit, 0, sizeof(visit));
	for (int i = 1; i <= n; ++i)
		if (onCircle[i])
			dfs(i);
}

void getWeight() {
	vector<int> cur(1, 0), Len(1, 0);
	memset(visit, 0, sizeof(visit));
	int sumDist = 0, m;
	for (int i = 1, j; i <= n; ++i)
		if (onCircle[i]) {
			cur.push_back(i);
			visit[i] = true;
			for (j = i; ;) {
				bool flag = false;
				for (vet e = E[j].begin(); e != E[j].end(); ++e)
					if (onCircle[e->t] && !visit[e->t]) {
						cur.push_back(e->t);
						Len.push_back(e->c);
						sumDist += e->c;
						visit[j = e->t] = true;
						flag = true;
						break;
					}
				if (!flag)
					break;
			}
			for (vet e = E[j].begin(); e != E[j].end(); ++e)
				if (e->t == i) {
					sumDist += e->c;
					Len.push_back(e->c);
					break;
				}
			break;
		}
	m = cur.size() - 1;
	#define NEXT(i) (i == m ? 1 : i + 1)
	int64 value = 0;
	int i = 1, j = 1, k, nowSize = 0, nowDist = 0, temp;
	for (temp = 0; temp + Len[j] < sumDist - (temp + Len[j]); ++j)
		temp += Len[j];
	nowSize = size[cur[1]];
	for (k = 2; k <= j; ++k) {
		nowDist += Len[k - 1];
		nowSize += size[cur[k]];
	}
	for (k = 1, temp = 0; k <= m; ++k) {
		temp += Len[k - 1];
		value += min(temp, sumDist - temp) * size[cur[k]] + w[cur[k]];
	}
	ans[cur[1]] = value;
	for (i = 2; i <= m; ++i) {
		nowDist -= Len[i - 1];
		nowSize -= size[cur[i - 1]];
		value -= nowSize * Len[i - 1];
		value += (n - nowSize) * Len[i - 1];
		for (; nowDist + Len[j] < sumDist - (nowDist + Len[j]); j = NEXT(j)) {
			nowSize += size[cur[NEXT(j)]];
			nowDist += Len[j];
			value += nowDist * size[cur[NEXT(j)]];
			value -= (sumDist - nowDist) * size[cur[NEXT(j)]];
		}
		ans[cur[i]] = value;
	}
}

void dfs(int x, int64 v) {
	visit[x] = true;
	ans[x] = v;
	for (vet e = E[x].begin(); e != E[x].end(); ++e)
		if (!onCircle[e->t] && !visit[e->t])
			dfs(e->t, v - size[e->t] * e->c + (n - size[e->t]) * e->c);
}

void getAnswer() {
	memset(visit, 0, sizeof(visit));
	for (int i = 1; i <= n; ++i)
		if (onCircle[i])
			dfs(i, ans[i]);
	for (int i = 1; i <= n; ++i)
		printf("%I64d%c", ans[i], i < n ? ' ' : '\n');
}

int main() {
	//freopen("x.in", "r", stdin);
	findCircle();
	getWeight();
	getAnswer();
}


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li		long long
#define rep(i,to)	for(li i=0;i<((li)to);i++)
#define pb		push_back
#define sz(v)		((li)v.size())

#define MAX 200005
#define LEFT 0
#define RIGHT 1
li n;
vector<pair<li,li> > E[MAX];
vector<pair<li,li> > L;
li used[MAX],comp[MAX],cnt[MAX],ans[MAX],sum[MAX],dis[MAX*3];
li find_loop(li pos,li parent=-1){
	if(used[pos]) return pos;
	used[pos]=true;
	rep(i,sz(E[pos]))if(E[pos][i].first!=parent){
		li tmp=find_loop(E[pos][i].first,pos);
		if(tmp!=-1){
			comp[pos]=true;
			L.pb(make_pair(pos,E[pos][i].second));
			return (tmp==pos)?-1:tmp;
		}
	}
	return -1;
}

li dfs(li pos,li parent=-1){
	cnt[pos]=1,sum[pos]=0;
	rep(i,sz(E[pos]))if(E[pos][i].first!=parent && !comp[E[pos][i].first]){
		cnt[pos]+=dfs(E[pos][i].first,pos);
		sum[pos]+=sum[E[pos][i].first];
		sum[pos]+=E[pos][i].second*cnt[E[pos][i].first];
	}
	return cnt[pos];
}

void cal(li pos,li parent=-1){
	rep(i,sz(E[pos]))if(E[pos][i].first!=parent && !comp[E[pos][i].first]){
		ans[E[pos][i].first]=ans[pos];
		ans[E[pos][i].first]+=(n-cnt[E[pos][i].first])*E[pos][i].second;
		ans[E[pos][i].first]-=cnt[E[pos][i].first]*E[pos][i].second;
		cal(E[pos][i].first,pos);
	}
}

int main(){
	li a,b,c;
	cin>>n;
	rep(i,n){
		cin>>a>>b>>c;
		E[a-1].pb(make_pair(b-1,c));
		E[b-1].pb(make_pair(a-1,c));
	}
	rep(i,MAX) used[i]=comp[i]=false;
	find_loop(0);
	rep(i,sz(L)) dfs(L[i].first);
	dis[0]=0;
	rep(i,sz(L)*3)if(i) dis[i]=dis[i-1]+L[i%sz(L)].second;
	ans[L[0].first]=0;
	rep(i,sz(L)){
		ans[L[0].first]+=sum[L[i].first]+min(dis[i],dis[sz(L)]-dis[i])*cnt[L[i].first];
	}
	li num[2]={n,0},mid=0;
	while(dis[mid+1]<dis[sz(L)]-dis[mid+1]){
		mid++;
		num[LEFT]-=cnt[L[mid].first];
		num[RIGHT]+=cnt[L[mid].first];
	}
	rep(i,sz(L))if(i){
#define ANS(var) ans[L[var].first]
		ANS(i)=ANS(i-1)+num[LEFT]*(dis[i]-dis[i-1])-num[RIGHT]*(dis[i]-dis[i-1]);
		num[LEFT]+=cnt[L[i].first];
		num[RIGHT]-=cnt[L[i].first];
		while(dis[mid+1]-dis[i]<dis[sz(L)+i]-dis[mid+1]){
			mid++;
			num[LEFT]-=cnt[L[mid%sz(L)].first];
			num[RIGHT]+=cnt[L[mid%sz(L)].first];
			ANS(i)-=(dis[sz(L)+i]-dis[mid])*cnt[L[mid%sz(L)].first];
			ANS(i)+=(dis[mid]-dis[i])*cnt[L[mid%sz(L)].first];
		}
	}
	rep(i,sz(L)) cal(L[i].first);
	rep(i,n) cout<<ans[i]<<" "; cout<<endl;
}


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li		long long
#define rep(i,to)	for(li i=0;i<((li)to);i++)
#define pb		push_back
#define sz(v)		((li)v.size())

#define MAX 200005
#define LEFT 0
#define RIGHT 1
li n;
vector<pair<li,li> > E[MAX];
vector<pair<li,li> > L;
li used[MAX],comp[MAX],cnt[MAX],ans[MAX],sum[MAX],dis[MAX*3];
li find_loop(li pos,li parent=-1){
	if(used[pos]) return pos;
	used[pos]=true;
	rep(i,sz(E[pos]))if(E[pos][i].first!=parent){
		li tmp=find_loop(E[pos][i].first,pos);
		if(tmp!=-1){
			comp[pos]=true;
			L.pb(make_pair(pos,E[pos][i].second));
			return (tmp==pos)?-1:tmp;
		}
	}
	return -1;
}

li dfs(li pos,li parent=-1){
	cnt[pos]=1,sum[pos]=0;
	rep(i,sz(E[pos]))if(E[pos][i].first!=parent && !comp[E[pos][i].first]){
		cnt[pos]+=dfs(E[pos][i].first,pos);
		sum[pos]+=sum[E[pos][i].first];
		sum[pos]+=E[pos][i].second*cnt[E[pos][i].first];
	}
	return cnt[pos];
}

void cal(li pos,li parent=-1){
	rep(i,sz(E[pos]))if(E[pos][i].first!=parent && !comp[E[pos][i].first]){
		ans[E[pos][i].first]=ans[pos];
		ans[E[pos][i].first]+=(n-cnt[E[pos][i].first])*E[pos][i].second;
		ans[E[pos][i].first]-=cnt[E[pos][i].first]*E[pos][i].second;
		cal(E[pos][i].first,pos);
	}
}

int main(){
	li a,b,c;
	cin>>n;
	rep(i,n){
		cin>>a>>b>>c;
		E[a-1].pb(make_pair(b-1,c));
		E[b-1].pb(make_pair(a-1,c));
	}
	rep(i,MAX) used[i]=comp[i]=false;
	find_loop(0);
	rep(i,sz(L)) dfs(L[i].first);
	dis[0]=0;
	rep(i,sz(L)*3)if(i) dis[i]=dis[i-1]+L[i%sz(L)].second;
	ans[L[0].first]=0;
	rep(i,sz(L)){
		ans[L[0].first]+=sum[L[i].first]+min(dis[i],dis[sz(L)]-dis[i])*cnt[L[i].first];
	}
	li num[2]={n,0},mid=0;
	while(dis[mid+1]<dis[sz(L)]-dis[mid+1]){
		mid++;
		num[LEFT]-=cnt[L[mid].first];
		num[RIGHT]+=cnt[L[mid].first];
	}
	rep(i,sz(L))if(i){
#define ANS(var) ans[L[var].first]
		ANS(i)=ANS(i-1)+num[LEFT]*(dis[i]-dis[i-1])-num[RIGHT]*(dis[i]-dis[i-1]);
		num[LEFT]+=cnt[L[i].first];
		num[RIGHT]-=cnt[L[i].first];
		while(dis[mid+1]-dis[i]<dis[sz(L)+i]-dis[mid+1]){
			mid++;
			num[LEFT]-=cnt[L[mid].first];
			num[RIGHT]+=cnt[L[mid].first];
			ANS(i)-=(dis[sz(L)+i]-dis[mid])*cnt[L[mid%sz(L)].first];
			ANS(i)+=(dis[mid]-dis[i])*cnt[L[mid%sz(L)].first];
		}
	}
	rep(i,sz(L)) cal(L[i].first);
	rep(i,n) cout<<ans[i]<<" "; cout<<endl;
}


#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define maxn 200005
#define ll long long
using namespace std;

int S,tot,N,head[maxn],next[maxn*2],lk[maxn*2],w[maxn*2],fa[maxn],dfn[maxn],q[maxn],in[maxn],s[maxn],qw[maxn];
ll W,f[maxn],g[maxn],ww[maxn];

void add(int u,int v,int x,int t) {lk[t]=v,w[t]=x,next[t]=head[u],head[u]=t;}

void init()
{
	scanf("%d",&N);
	for (int i=1,u,v,w; i<=N; i++) scanf("%d%d%d",&u,&v,&w),add(u,v,w,i),add(v,u,w,i+N);
}

void loop(int u)
{
    dfn[u]=++tot;
    for (int i=head[u],v; v=lk[i],i; i=next[i]) if (fa[u]!=v&&!dfn[v]) fa[v]=u,f[v]=w[i],loop(v);
    for (int i=head[u],v; v=lk[i],i; i=next[i]) if (dfn[v]>dfn[u]&&fa[v]!=u)
    {
        for (int x=v; x!=u; x=fa[x]) q[++S]=x,qw[S]=f[x],in[x]=1;
        q[++S]=u,qw[S]=w[i],in[u]=1;
    }
}

void dfs1(int u,int fa)
{
	s[u]=1,f[u]=0;
	for (int i=head[u],v; v=lk[i],i; i=next[i]) if (!in[v]&&v!=fa) dfs1(v,u),f[u]+=f[v]+1ll*s[v]*w[i],s[u]+=s[v];
}

void dfs2(int u,int fa,int bg)
{
	g[u]=g[fa]+f[fa]-f[u]+1ll*bg*(N-2*s[u]);
	for (int i=head[u],v; v=lk[i],i; i=next[i]) if (v!=fa) dfs2(v,u,w[i]);
}

void doit()
{
	loop(1);
	for (int i=1; i<=S; i++) fa[q[i]]=0,dfs1(q[i],0);

	for (int i=1; i<=S; i++) q[i+S]=q[i],qw[i+S]=qw[i],W+=qw[i];
	for (int i=1; i<=2*S; i++) ww[i]=ww[i-1]+qw[i];
	int SA=0,SB=0; ll A=0,B=0;
	for (int i=2; i<=S; i++) SB+=s[q[i]],B+=f[q[i]]+(W-ww[i-1])*s[q[i]];
	for (int i=1,j=2; i<=S; i++)
	{
		for (;ww[j-1]-ww[i-1]<=W-ww[j-1]+ww[i-1]; j++) SA+=s[q[j]],A+=f[q[j]]+(ww[j-1]-ww[i-1])*s[q[j]],SB-=s[q[j]],B-=f[q[j]]+(W-ww[j-1]+ww[i-1])*s[q[j]];
		g[q[i]]=A+B,A-=1ll*qw[i]*SA+f[q[i+1]],SA-=s[q[i+1]],SB+=s[q[i]],B+=f[q[i]]+1ll*qw[i]*SB;
	}

	for (int i=1; i<=S; i++)
		for (int u=q[i],j=head[u],v; v=lk[j],j; j=next[j]) if (!in[v]) dfs2(v,u,w[j]);
	for (int i=1; i<=N; i++) printf("%I64d%c",f[i]+g[i],i==N?'\n':' ');
}

int main()
{
	init();
	doit();
	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define maxn 200005
#define ll long long
#ifdef __linux
	#define LLD "%lld"
#else
	#define LLD "%I64d"
#endif
using namespace std;

int S,tot,N,head[maxn],next[maxn*2],lk[maxn*2],w[maxn*2],fa[maxn],dfn[maxn],q[maxn],in[maxn],s[maxn],qw[maxn];
ll W,f[maxn],g[maxn],ww[maxn];

void add(int u,int v,int x,int t) {lk[t]=v,w[t]=x,next[t]=head[u],head[u]=t;}

void read(int&x)
{
	char c;
	for (;!isdigit(c=getchar()););
	for (x=0; isdigit(c); c=getchar()) (x*=10)+=c-48;
}

void init()
{
	read(N);
	for (int i=1,u,v,w; i<=N; i++) read(u),read(v),read(w),add(u,v,w,i),add(v,u,w,i+N);
}

void loop(int u)
{
    dfn[u]=++tot;
    for (int i=head[u],v; v=lk[i],i; i=next[i]) if (fa[u]!=v&&!dfn[v]) fa[v]=u,f[v]=w[i],loop(v);
    for (int i=head[u],v; v=lk[i],i; i=next[i]) if (dfn[v]>dfn[u]&&fa[v]!=u)
    {
        for (int x=v; x!=u; x=fa[x]) q[++S]=x,qw[S]=f[x],in[x]=1;
        q[++S]=u,qw[S]=w[i],in[u]=1;
    }
}

void dfs1(int u,int fa)
{
	s[u]=1,f[u]=0;
	for (int i=head[u],v; v=lk[i],i; i=next[i]) if (!in[v]&&v!=fa) dfs1(v,u),f[u]+=f[v]+1ll*s[v]*w[i],s[u]+=s[v];
}

void dfs2(int u,int fa,int bg)
{
	g[u]=g[fa]+f[fa]-f[u]+1ll*bg*(N-2*s[u]);
	for (int i=head[u],v; v=lk[i],i; i=next[i]) if (v!=fa) dfs2(v,u,w[i]);
}

void doit()
{
	loop(1);
	for (int i=1; i<=S; i++) fa[q[i]]=0,dfs1(q[i],0);

	for (int i=1; i<=S; i++) q[i+S]=q[i],qw[i+S]=qw[i],W+=qw[i];
	for (int i=1; i<=2*S; i++) ww[i]=ww[i-1]+qw[i];
	int SA=0,SB=0; ll A=0,B=0;
	for (int i=2; i<=S; i++) SB+=s[q[i]],B+=f[q[i]]+(W-ww[i-1])*s[q[i]];
	for (int i=1,j=2; i<=S; i++)
	{
		for (;ww[j-1]-ww[i-1]<=W-ww[j-1]+ww[i-1]; j++) SA+=s[q[j]],A+=f[q[j]]+(ww[j-1]-ww[i-1])*s[q[j]],SB-=s[q[j]],B-=f[q[j]]+(W-ww[j-1]+ww[i-1])*s[q[j]];
		g[q[i]]=A+B,A-=1ll*qw[i]*SA+f[q[i+1]],SA-=s[q[i+1]],SB+=s[q[i]],B+=f[q[i]]+1ll*qw[i]*SB;
	}

	for (int i=1; i<=S; i++)
		for (int u=q[i],j=head[u],v; v=lk[j],j; j=next[j]) if (!in[v]) dfs2(v,u,w[j]);
	for (int i=1; i<=N; i++) printf(LLD"%c",f[i]+g[i],i==N?'\n':' ');
}

int main()
{
	init();
	doit();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;

int in[N], n, x, y, z, size[N], h[N];
ll ddd[N], ans[N];
pii next[N], prev[N];
vector< pii > v[N];

void go(int node, int root) {
	size[node] = 1;
	foreach(it, v[node]) {
		if(!h[it->st] && root != it->st) {
			go(it->st, node);
			size[node] += size[it->st];
			ddd[node] += ddd[it->st] + size[it->st] * (ll) it->nd;
		}
	}
}

void doit(int node, int root, ll ans, int size) {
	::ans[node] = ans + ddd[node];
	foreach(it, v[node])
		if(!h[it->st] && it->st != root) {
			doit(it->st,node,ans+(size+::size[node]-::size[it->st])*(ll)it->nd+(ddd[node]-ddd[it->st]-::size[it->st]*(ll)it->nd),size+(::size[node]-::size[it->st]));
		}
}

int main() {

	scanf("%d", &n);

	FOR(i, 1, n) {
		scanf("%d %d %d", &x, &y, &z);
		v[x].pb(mp(y, z));
		v[y].pb(mp(x, z));
		in[x]++, in[y]++;
	}

	queue< int > q;
	FOR(i, 1, n)
		if(1 == in[i])
			q.push(i);

	FOR(i, 1, n) h[i] = 1;

	while(!q.empty()) {
		int node = q.front();
		h[node] = 0;
		q.pop();
		foreach(it, v[node])
			if(1 == --in[it->st]) {
				q.push(it->st);
			}
	}

	int start = 1;

	FOR(i, 1, n) {
		if(h[i]) {
			go(i, 0);
			start = i;
		}
	}

	start = 5;

	int sss = start, bef = 0;
	ll ball = 0, sall = 0, all = 0;

	while(1) {
		int go = 0;
		foreach(it, v[start])
			if(h[it->st] && it->st != bef) {
				prev[it->st] = mp(start, it->nd);
				next[start] = *it;
				bef = start;
				start = it->st;
				all += it->nd;
				break;
			}
		sall += size[start];
		ball += ddd[start];
		if(start == sss) break;
	}

	ll ans = 0, opt = start, node = start, dist = 0, sum = 0;

	while(1) {
		if(next[node].st == start) { node = start; break; }
		dist += next[node].nd;
		ans += dist * size[next[node].st];
		node = next[node].st;
	} dist = 0;

	while(1) {
		while((prev[opt].nd + dist) * 2 <= all) {
			sum += size[prev[opt].st];
			dist += prev[opt].nd;
			ans -= size[prev[opt].st] * (ll) (all - 2 * dist);
			opt = prev[opt].st;
		}
		doit(node, 0, ans + ball - ddd[node], n - size[node]);
		ans += (n - sum) * (ll) prev[node].nd;
		ans -= (sum) * (ll) prev[node].nd;
		sum -= size[prev[node].st];
		dist -= prev[node].nd;
		node = prev[node].st;
		if(node == start) break;
	}

	FOR(i, 1, n) {
		printf("%lld ", :: ans[i]);
	} printf("\n");

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define endl '\n'
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define mp make_pair
#define nd second
#define st first
#define type(x) __typeof(x.begin())

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;

int in[N], n, x, y, z, size[N], h[N];
ll ddd[N], ans[N];
pii next[N], prev[N];
vector< pii > v[N];

void go(int node, int root) {
	size[node] = 1;
	foreach(it, v[node]) {
		if(!h[it->st] && root != it->st) {
			go(it->st, node);
			size[node] += size[it->st];
			ddd[node] += ddd[it->st] + size[it->st] * (ll) it->nd;
		}
	}
}

void doit(int node, int root, ll ans, int size) {
	::ans[node] = ans + ddd[node];
	foreach(it, v[node])
		if(!h[it->st] && it->st != root) {
			doit(it->st,node,ans+(size+::size[node]-::size[it->st])*(ll)it->nd+(ddd[node]-ddd[it->st]-::size[it->st]*(ll)it->nd),size+(::size[node]-::size[it->st]));
		}
}

int main() {

	scanf("%d", &n);

	FOR(i, 1, n) {
		scanf("%d %d %d", &x, &y, &z);
		v[x].pb(mp(y, z));
		v[y].pb(mp(x, z));
		in[x]++, in[y]++;
	}

	queue< int > q;
	FOR(i, 1, n)
		if(1 == in[i])
			q.push(i);

	FOR(i, 1, n) h[i] = 1;

	while(!q.empty()) {
		int node = q.front();
		h[node] = 0;
		q.pop();
		foreach(it, v[node])
			if(1 == --in[it->st]) {
				q.push(it->st);
			}
	}

	int start = 1;

	FOR(i, 1, n) {
		if(h[i]) {
			go(i, 0);
			start = i;
		}
	}

	int sss = start, bef = 0;
	ll ball = 0, sall = 0, all = 0;

	while(1) {
		int go = 0;
		foreach(it, v[start])
			if(h[it->st] && it->st != bef) {
				prev[it->st] = mp(start, it->nd);
				next[start] = *it;
				bef = start;
				start = it->st;
				all += it->nd;
				break;
			}
		sall += size[start];
		ball += ddd[start];
		if(start == sss) break;
	}

	ll ans = 0, opt = start, node = start, dist = 0, sum = 0;

	while(1) {
		if(next[node].st == start) { node = start; break; }
		dist += next[node].nd;
		ans += dist * size[next[node].st];
		node = next[node].st;
	} dist = 0;

	while(1) {
		while((prev[opt].nd + dist) * 2 <= all) {
			sum += size[prev[opt].st];
			dist += prev[opt].nd;
			ans -= size[prev[opt].st] * (ll) (all - 2 * dist);
			opt = prev[opt].st;
		}
		doit(node, 0, ans + ball - ddd[node], n - size[node]);
		ans += (n - sum) * (ll) prev[node].nd;
		ans -= (sum) * (ll) prev[node].nd;
		sum -= size[prev[node].st];
		dist -= prev[node].nd;
		node = prev[node].st;
		if(node == start) break;
	}

	FOR(i, 1, n) {
		printf("%lld ", :: ans[i]);
	} printf("\n");

	return 0;
}


#include<bits/stdc++.h>
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;

inline ll read()
{
    long long f=1,sum=0;
    char c=getchar();
    while (c<'0' || c>'9')
    {
        if (c=='-') f=-1;
        c=getchar();
    }
    while (c>='0' && c<='9')
    {
        sum=sum*10+c-'0';
        c=getchar();
    }
    return sum*f;
}
const int MAXN=200010;
struct edge
{
    int next,to,val;
};
edge e[MAXN*2];
int head[MAXN],cnt=1;
void addedge(int u,int v,int w)
{
    e[++cnt].next=head[u];
    e[cnt].to=v;
    e[cnt].val=w;
    head[u]=cnt;
}
bool in_circle[MAXN],visit[MAXN];
bool Found;
int from[MAXN];
vector <int> cir;
void dfs_cl(int x,int fa)
{
    visit[x]=1;
    for (int i=head[x];i;i=e[i].next)
    {
        int v=e[i].to;
        if (v==fa) continue;
        if (visit[v])
        {
            int tmp=x;
            while (tmp!=v)
            {
                cir.push_back(tmp);
                in_circle[tmp]=1;
                tmp=from[tmp];
            }
            Found=1;
            in_circle[v]=1;
            cir.push_back(v);
            continue;
        }
        from[v]=x;
        dfs_cl(v,x);
        if (Found) return ;
    }
}
int sz[MAXN],fa[MAXN],root[MAXN];
ll f[MAXN],dis[MAXN];
void dfs(int x,int rt)
{
    sz[x]++;
    root[x]=rt;
    for (int i=head[x];i;i=e[i].next)
    {
        int v=e[i].to;
        if (in_circle[v] || fa[v]) continue;
        fa[v]=x;
        dis[v]=dis[x]+e[i].val;
        dfs(v,rt);
        sz[x]+=sz[v];
        f[x]+=f[v]+sz[v]*e[i].val;
    }
}
ll ans[MAXN];
int n;
void dfs2(int x,int dist,int size)
{
    visit[x]=1;
    ans[x]+=dist+f[x]+dis[x]*(n-sz[root[x]]);
    for (int i=head[x];i;i=e[i].next)
    {
        int v=e[i].to;
        if (visit[v] || in_circle[v]) continue;
        int szv=size+sz[x]-sz[v];
        int disv=dist+e[i].val*(szv-sz[v])+f[x]-f[v];
        dfs2(v,disv,szv);
    }
}
ll cst[MAXN*3],Ans[MAXN];
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        addedge(u,v,w);
        addedge(v,u,w);
    }
    for (int i=1;i<=n;i++)
        if (!Found)
            dfs_cl(i,-1);
    int num=cir.size();
    for (int i=0;i<num;i++)
        dfs(cir[i],cir[i]);
    memset(visit,0,sizeof(visit));
    for (int i=0;i<num;i++)
        dfs2(cir[i],0,0),cir.push_back(cir[i]);
    for (int i=0;i<num;i++)
        cir.push_back(cir[i]);
    for (int i=0;i<num;i++)
    {
        for (int j=head[cir[i+1]];j;j=e[j].next)
        {
            int v=e[j].to;
            if (v==cir[i])
                cst[i+num+num+1]=cst[i+num+1]=cst[i+1]=e[j].val;
        }
    }
    for (int i=1;i<3*num;i++)
        cst[i]+=cst[i-1];
    ll anss=0;
    for (int i=1;i<num;i++)
        anss+=(cst[num]-cst[i])*sz[cir[i]];
    int j=num+1,ncnt=sz[cir[num]];
    ll tot=cst[num];
    ll all=0;
    for (int i=0;i<num;i++)
        all+=ans[cir[i]];
    for (int i=num;i<2*num;i++)
    {
        while (cst[j]-cst[i]<=tot/2)
        {
            ncnt+=sz[cir[j]];
            anss+=(2*(cst[j]-cst[i])-tot)*sz[cir[j]];
            j++;
        }
        Ans[cir[i]]=anss;
        ncnt-=sz[cir[i]];
        anss+=(n-ncnt*2)*(cst[i+1]-cst[i]);
    }
    for (int i=1;i<=n;i++)
        printf("%I64d ",Ans[root[i]]+ans[i]+all-ans[root[i]]);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <bitset>
#include <cmath>
#include <ctime>
#include <cassert>
using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); e++)
#define FAST_IO std::ios::sync_with_stdio(false)
typedef long long LL;
typedef pair<int, int> PII;
int rd() { return RAND_MAX == 32767 ? ((rand() << 15) ^ rand()) : rand(); }

const int maxn = 200010;

int n, m, pos, tal, sav;
int d[maxn], g[maxn], p[maxn], s[maxn], sl[maxn], sr[maxn], cir[maxn], dis[maxn], ins[maxn], pre[maxn], vis[maxn], sum[maxn];
long long f[maxn], h[maxn], ls[maxn], rs[maxn];
int len[maxn*2], vec[maxn*2], nxt[maxn*2];

int sc(int x)
{
	return (x-1+m) % m;
}
int ps(int x)
{
	return (x+1) % m;
}
void Dfs(int t)
{
	vis[t] = 1;
	for (int i = g[t]; i != -1; i = nxt[i])
		if (! vis[vec[i]]) {
			p[vec[i]] = t;
			pre[vec[i]] = len[i];
			Dfs(vec[i]);
		}
		else if (vec[i] != p[t]) {
			if (pos == -1) {
				pos = t;
				tal = vec[i];
				sav = len[i];
			}
		}
}
void Dp(int t)
{
	s[t] = 1;
	for (int i = g[t]; i != -1; i = nxt[i])
		if (vec[i] != p[t] && ! ins[vec[i]]) {
			p[vec[i]] = t;
			Dp(vec[i]);
			s[t] += s[vec[i]];
			f[t] += (long long)s[vec[i]]*len[i]+f[vec[i]];
		}
}
void Calc(int t)
{
	for (int i = g[t]; i != -1; i = nxt[i])
		if (vec[i] != p[t] && ! ins[vec[i]]) {
			p[vec[i]] = t;
			h[vec[i]] = h[t]+f[t]-(long long)s[vec[i]]*len[i]-f[vec[i]]+(long long)(n-s[vec[i]])*len[i];
			Calc(vec[i]);
		}
}
int dist(int u, int v)
{
	if (u < v)
		return sum[v]-sum[u];
	else
		return sum[m-1]-sum[u]+dis[m-1]+sum[v];
}
int dist_(int u, int v)
{
	if (u <= v)
		return sum[v]-sum[u];
	else
		return sum[m-1]-sum[u]+dis[m-1]+sum[v];
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin >> n;
	memset(g, -1, sizeof(g));
	long long tot = 0;
	for (int i = 0; i < n; i++) {
		int u, v, w;
		cin >> u >> v >> w; u--; v--;
		len[tot] = w, vec[tot] = v, nxt[tot] = g[u], g[u] = tot++;
		len[tot] = w, vec[tot] = u, nxt[tot] = g[v], g[v] = tot++;
	}

	memset(vis, 0, sizeof(vis));
	p[0] = -1;
	pos = -1;
	Dfs(0);
	memset(ins, 0, sizeof(ins));
	m = 0;
	while (pos != tal) {
		ins[pos] = 1;
		cir[m] = pos;
		dis[m++] = pre[pos];
		pos = p[pos];
	}
	ins[pos] = 1;
	cir[m] = pos;
	dis[m++] = sav;

	sum[0] = 0;
	for (int i = 1; i < m; i++)
		sum[i] = sum[i-1]+dis[i-1];

	memset(p, -1, sizeof(p));
	for (int i = 0; i < n; i++)
		if (ins[i])
			Dp(i);

	tot = 0;
	for (int i = 0; i < m; i++) tot += f[cir[i]];
	for (d[0] = 0; dist(0, ps(d[0])) <= dist_(ps(d[0]), 0); d[0] = ps(d[0]));
	for (int i = 1; i < m; i++) {
		d[i] = d[i-1] == i-1 ? i : d[i-1];
		for ( ; dist(i, ps(d[i])) <= dist_(ps(d[i]), i); d[i] = ps(d[i]));
	}
	// sum = tot_dis
	// ss = tot_s
	sl[0] = s[cir[0]];
	for (int i = 1; i < m; i++) sl[i] = sl[i-1]+s[cir[i]];
	sr[m-1] = s[cir[m-1]];
	for (int i = m-2; i >= 0; i--) sr[i] = sr[i+1]+s[cir[i]];
	ls[0] = 0;
	for (int i = 1; i < m; i++) ls[i] = ls[i-1]+(long long)s[cir[i]]*sum[i];
	rs[m-1] = 0;
	for (int i = m-2; i >= 0; i--) rs[i] = rs[i+1]+(long long)s[cir[i]]*(sum[m-1]-sum[i]);
	for (int i = 0; i < m; i++) {
		long long res = tot-f[cir[i]];
		if (i != d[i]) {
			if (i < d[i])
				res += ls[d[i]]-ls[i]-(long long)(sl[d[i]]-sl[i])*sum[i];
			else {
				res += ls[m-1]-ls[i]-(long long)(sl[m-1]-sl[i])*sum[i];
				res += ls[d[i]]+(long long)sl[d[i]]*(dis[m-1]+sum[m-1]-sum[i]);
			}
		}
		int k = ps(d[i]);
		if (i != k) {
			if (k < i)
				res += rs[k]-rs[i]-(long long)(sr[k]-sr[i])*(sum[m-1]-sum[i]);
			else {
				res += rs[0]-rs[i]-(long long)(sr[0]-sr[i])*(sum[m-1]-sum[i]);
				res += rs[k]+(long long)sr[k]*(dis[m-1]+sum[i]);
			}
		}
		h[cir[i]] = res;
	}
	memset(p, -1, sizeof(p));
	for (int i = 0; i < n; i++)
		if (ins[i])
			Calc(i);

	for (int i = 0; i < n; i++) {
		cout << f[i]+h[i];
		if (i < n-1)
			cout << ' ';
		else
			cout << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <bitset>
#include <cmath>
#include <ctime>
#include <cassert>
using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); e++)
#define FAST_IO std::ios::sync_with_stdio(false)
typedef long long LL;
typedef pair<int, int> PII;
int rd() { return RAND_MAX == 32767 ? ((rand() << 15) ^ rand()) : rand(); }

const int maxn = 200010;

int n, m, pos, tal, sav;
int d[maxn], g[maxn], p[maxn], s[maxn], sl[maxn], sr[maxn], cir[maxn], dis[maxn], ins[maxn], pre[maxn], vis[maxn], sum[maxn];
long long f[maxn], h[maxn], ls[maxn], rs[maxn];
int len[maxn*2], vec[maxn*2], nxt[maxn*2];

int sc(int x)
{
	return (x-1+m) % m;
}
int ps(int x)
{
	return (x+1) % m;
}
void Dfs(int t)
{
	vis[t] = 1;
	for (int i = g[t]; i != -1; i = nxt[i])
		if (! vis[vec[i]]) {
			p[vec[i]] = t;
			pre[vec[i]] = len[i];
			Dfs(vec[i]);
		}
		else if (vec[i] != p[t]) {
			if (pos == -1) {
				pos = t;
				tal = vec[i];
				sav = len[i];
			}
		}
}
void Dp(int t)
{
	s[t] = 1;
	for (int i = g[t]; i != -1; i = nxt[i])
		if (vec[i] != p[t] && ! ins[vec[i]]) {
			p[vec[i]] = t;
			Dp(vec[i]);
			s[t] += s[vec[i]];
			f[t] += (long long)s[vec[i]]*len[i]+f[vec[i]];
		}
}
void Calc(int t)
{
	for (int i = g[t]; i != -1; i = nxt[i])
		if (vec[i] != p[t] && ! ins[vec[i]]) {
			p[vec[i]] = t;
			h[vec[i]] = h[t]+f[t]-(long long)s[vec[i]]*len[i]-f[vec[i]]+(long long)(n-s[vec[i]])*len[i];
			Calc(vec[i]);
		}
}
int dist(int u, int v)
{
	if (u < v)
		return sum[v]-sum[u];
	else
		return sum[m-1]-sum[u]+dis[m-1]+sum[v];
}
int dist_(int u, int v)
{
	if (u <= v)
		return sum[v]-sum[u];
	else
		return sum[m-1]-sum[u]+dis[m-1]+sum[v];
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin >> n;
	memset(g, -1, sizeof(g));
	long long tot = 0;
	for (int i = 0; i < n; i++) {
		int u, v, w;
		cin >> u >> v >> w; u--; v--;
		len[tot] = w, vec[tot] = v, nxt[tot] = g[u], g[u] = tot++;
		len[tot] = w, vec[tot] = u, nxt[tot] = g[v], g[v] = tot++;
	}

	memset(vis, 0, sizeof(vis));
	p[0] = -1;
	pos = -1;
	Dfs(0);
	memset(ins, 0, sizeof(ins));
	m = 0;
	while (pos != tal) {
		ins[pos] = 1;
		cir[m] = pos;
		dis[m++] = pre[pos];
		pos = p[pos];
	}
	ins[pos] = 1;
	cir[m] = pos;
	dis[m++] = sav;

	sum[0] = 0;
	for (int i = 1; i < m; i++)
		sum[i] = sum[i-1]+dis[i-1];

	for (int i = 0; i < n; i++)
		if (ins[i]) {
			p[i] = -1;
			Dp(i);
		}

	tot = 0;
	for (int i = 0; i < m; i++) tot += f[cir[i]];
	for (d[0] = 0; dist(0, ps(d[0])) <= dist_(ps(d[0]), 0); d[0] = ps(d[0]));
	for (int i = 1; i < m; i++) {
		d[i] = d[i-1] == i-1 ? i : d[i-1];
		for ( ; dist(i, ps(d[i])) <= dist_(ps(d[i]), i); d[i] = ps(d[i]));
	}
	// sum = tot_dis
	// ss = tot_s
	sl[0] = s[cir[0]];
	for (int i = 1; i < m; i++) sl[i] = sl[i-1]+s[cir[i]];
	sr[m-1] = s[cir[m-1]];
	for (int i = m-2; i >= 0; i--) sr[i] = sr[i+1]+s[cir[i]];
	ls[0] = 0;
	for (int i = 1; i < m; i++) ls[i] = ls[i-1]+(long long)s[i]*sum[i];
	rs[m-1] = 0;
	for (int i = m-2; i >= 0; i--) rs[i] = rs[i+1]+(long long)s[i]*(sum[m-1]-sum[i]);
	for (int i = 0; i < m; i++) {
		long long res = tot-f[i];
		if (i != d[i]) {
			if (i < d[i])
				res += ls[d[i]]-ls[i]-(long long)(sl[d[i]]-sl[i])*sum[i];
			else {
				res += ls[m-1]-ls[i]-(long long)(sl[m-1]-sl[i])*sum[i];
				res += ls[d[i]]+(long long)sl[d[i]]*(dis[m-1]+sum[m-1]-sum[i]);
			}
		}
		int k = ps(d[i]);
		if (i != k) {
			if (k < i)
				res += rs[k]-rs[i]-(long long)(sr[k]-sr[i])*(sum[m-1]-sum[i]);
			else {
				res += rs[0]-rs[i]-(long long)(sr[0]-sr[i])*(sum[m-1]-sum[i]);
				res += rs[k]+(long long)sr[k]*(dis[m-1]+sum[i]);
			}
		}
		h[cir[i]] = res;
	}
	for (int i = 0; i < m; i++)
		if (ins[i]) {
			p[i] = -1;
			Calc(i);
		}

	for (int i = 0; i < n; i++) {
		cout << f[i]+h[i];
		if (i < n-1)
			cout << ' ';
		else
			cout << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <bitset>
#include <cmath>
#include <ctime>
#include <cassert>
using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); e++)
#define FAST_IO std::ios::sync_with_stdio(false)
typedef long long LL;
typedef pair<int, int> PII;
int rd() { return RAND_MAX == 32767 ? ((rand() << 15) ^ rand()) : rand(); }

const int maxn = 200010;

int n, m, pos, tal, sav;
int d[maxn], g[maxn], p[maxn], s[maxn], sl[maxn], sr[maxn], cir[maxn], dis[maxn], ins[maxn], pre[maxn], vis[maxn], sum[maxn];
long long f[maxn], h[maxn], ls[maxn], rs[maxn];
int len[maxn*2], vec[maxn*2], nxt[maxn*2];

int sc(int x)
{
	return (x-1+m) % m;
}
int ps(int x)
{
	return (x+1) % m;
}
void Dfs(int t)
{
	vis[t] = 1;
	for (int i = g[t]; i != -1; i = nxt[i])
		if (! vis[vec[i]]) {
			p[vec[i]] = t;
			pre[vec[i]] = len[i];
			Dfs(vec[i]);
		}
		else if (vec[i] != p[t]) {
			if (pos == -1) {
				pos = t;
				tal = vec[i];
				sav = len[i];
			}
		}
}
void Dp(int t)
{
	s[t] = 1;
	for (int i = g[t]; i != -1; i = nxt[i])
		if (vec[i] != p[t] && ! ins[vec[i]]) {
			p[vec[i]] = t;
			Dp(vec[i]);
			s[t] += s[vec[i]];
			f[t] += (long long)s[vec[i]]*len[i]+f[vec[i]];
		}
}
void Calc(int t)
{
	for (int i = g[t]; i != -1; i = nxt[i])
		if (vec[i] != p[t] && ! ins[vec[i]]) {
			p[vec[i]] = t;
			h[vec[i]] = h[t]+f[t]-(long long)s[vec[i]]*len[i]-f[vec[i]]+(long long)(n-s[vec[i]])*len[i];
			Calc(vec[i]);
		}
}
int dist(int u, int v)
{
	if (u < v)
		return sum[v]-sum[u];
	else
		return sum[m-1]-sum[u]+dis[m-1]+sum[v];
}
int dist_(int u, int v)
{
	if (u <= v)
		return sum[v]-sum[u];
	else
		return sum[m-1]-sum[u]+dis[m-1]+sum[v];
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin >> n;
	memset(g, -1, sizeof(g));
	long long tot = 0;
	for (int i = 0; i < n; i++) {
		int u, v, w;
		cin >> u >> v >> w; u--; v--;
		len[tot] = w, vec[tot] = v, nxt[tot] = g[u], g[u] = tot++;
		len[tot] = w, vec[tot] = u, nxt[tot] = g[v], g[v] = tot++;
	}

	memset(vis, 0, sizeof(vis));
	p[0] = -1;
	pos = -1;
	Dfs(0);
	memset(ins, 0, sizeof(ins));
	m = 0;
	while (pos != tal) {
		ins[pos] = 1;
		cir[m] = pos;
		dis[m++] = pre[pos];
		pos = p[pos];
	}
	ins[pos] = 1;
	cir[m] = pos;
	dis[m++] = sav;

	sum[0] = 0;
	for (int i = 1; i < m; i++)
		sum[i] = sum[i-1]+dis[i-1];

	for (int i = 0; i < n; i++)
		if (ins[i]) {
			p[i] = -1;
			Dp(i);
		}

	tot = 0;
	for (int i = 0; i < m; i++) tot += f[cir[i]];
	for (d[0] = 0; dist(0, ps(d[0])) <= dist_(ps(d[0]), 0); d[0] = ps(d[0]));
	for (int i = 1; i < m; i++) {
		d[i] = d[i-1] == i-1 ? i : d[i-1];
		for ( ; dist(i, ps(d[i])) <= dist_(ps(d[i]), i); d[i] = ps(d[i]));
	}
	// sum = tot_dis
	// ss = tot_s
	sl[0] = s[cir[0]];
	for (int i = 1; i < m; i++) sl[i] = sl[i-1]+s[cir[i]];
	sr[m-1] = s[cir[m-1]];
	for (int i = m-2; i >= 0; i--) sr[i] = sr[i+1]+s[cir[i]];
	ls[0] = 0;
	for (int i = 1; i < m; i++) ls[i] = ls[i-1]+(long long)s[cir[i]]*sum[i];
	rs[m-1] = 0;
	for (int i = m-2; i >= 0; i--) rs[i] = rs[i+1]+(long long)s[cir[i]]*(sum[m-1]-sum[i]);
	for (int i = 0; i < m; i++) {
		long long res = tot-f[cir[i]];
		if (i != d[i]) {
			if (i < d[i])
				res += ls[d[i]]-ls[i]-(long long)(sl[d[i]]-sl[i])*sum[i];
			else {
				res += ls[m-1]-ls[i]-(long long)(sl[m-1]-sl[i])*sum[i];
				res += ls[d[i]]+(long long)sl[d[i]]*(dis[m-1]+sum[m-1]-sum[i]);
			}
		}
		int k = ps(d[i]);
		if (i != k) {
			if (k < i)
				res += rs[k]-rs[i]-(long long)(sr[k]-sr[i])*(sum[m-1]-sum[i]);
			else {
				res += rs[0]-rs[i]-(long long)(sr[0]-sr[i])*(sum[m-1]-sum[i]);
				res += rs[k]+(long long)sr[k]*(dis[m-1]+sum[i]);
			}
		}
		h[cir[i]] = res;
	}
	for (int i = 0; i < m; i++)
		if (ins[i]) {
			p[i] = -1;
			Calc(i);
		}

	for (int i = 0; i < n; i++) {
		cout << f[i]+h[i];
		if (i < n-1)
			cout << ' ';
		else
			cout << endl;
	}
	return 0;
}


//Tomasz Kulczyński
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <numeric>
#include <cmath>
#include <cstdlib>
using namespace std;

#define X first
#define Y second
#define MP make_pair
#define PB push_back
typedef long long LL;
typedef double D;
typedef long double ld;
typedef vector<int> VI;
typedef pair<int,int> PII;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(VAR(i,a);i<=(b);++i)
#define FORD(i,a,b) for(VAR(i,a);i>=(b);--i)
#define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a)
#define VAR(a,b) typeof(b) a=(b)
#define SIZE(a) ((int)((a).size()))
#define ALL(x) (x).begin(),(x).end()
#define CLR(x,a) memset(x,a,sizeof(x))
int cond = 1;
#define db(x) {if(cond){cerr << __LINE__ << " " << #x << " " << x << endl; } }
#define dbv(x) {if(cond){cerr << __LINE__ << " " << #x << ": "; FORE(__i,x) cerr << *__i << " "; cerr << endl;} }

const int M = 201013;

int n,ee;
vector<PII> pod[M], v[M];
LL su[M],ret[M];
int w[M],s[M],e[M];
PII f[M+M];

void dfs(int x)
{
    FORE(i, pod[x])
    {
        ret[i->X] = ret[x] + i->Y * (n - 2 * w[i->X]);
        dfs(i->X);
    }
}

int main()
{
    scanf("%d",&n);
    REP(i,n)
    {
        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));
        s[a]++;
        s[b]++;
        su[i] = 0;
        w[i] = 1;
    }
    REP(i,n) if(s[i] == 1) e[ee++] = i;
    while(ee)
    {
        int x = e[--ee];
        s[x] = 0;
        FORE(i,v[x]) if(s[i->X])
        {
            su[i->X] += su[x] + w[x] * i->Y;
            w[i->X] += w[x];
            pod[i->X].PB(MP(x, i->Y));
            if(!--s[i->X]) e[ee++] = i->X;
        }
    }
    int x = 1;
    REP(i,n) if(s[i]) { x = i; }
    ee = 1;
    FORD(i,SIZE(v[x])-1,0) if(s[v[x][i].X]) { f[0] = MP(x, v[x][i].Y); break; }
    while(s[x])
    {
        s[x] = 0;
        FORE(i, v[x]) if(s[i->X])
        {
            f[ee++]= *i;
            x = i->X;
            break;
        }
    }
    {
        int S = 0, W = 0;
        LL SU = 0;
        REP(i,ee)
        {
       //     printf("%d ",f[i].X);
            S += f[i].Y;
            f[ee+i] = f[i];
            SU += su[f[i].X] + 1LL * W * f[i].Y;
            W += w[f[i].X];
        }
        SU += 1LL * W * f[ee].Y;
      //  printf("\n__ %lld %d\n",SU,W);
        int j = 0, SS = S;
        FOR(i, ee, ee+ee-1) {
            while(SS+SS > S)
            {
                SU -= su[f[j].X] + 1LL * w[f[j].X] * SS;
                W -= w[f[j].X];
                SS -= f[j+1].Y;
                j++;
            }
    //        printf("%lld %d\n",SU,W);
            ret[f[i].X] = su[f[i].X] + SU;
            W += w[f[i].X];
            SU += f[i+1].Y * W + su[f[i].X];
            SS += f[i+1].Y;
        }
    }
    REP(i,ee)
        swap(f[i], f[ee+ee-1-i]);
    {
        int S=0, W = 0;
        LL SU = 0;
        REP(i,ee)
        {
            S += f[i].Y;
            SU += su[f[i].X] + 1LL * W * f[i-1].Y;
            W += w[f[i].X];
        }
        SU += 1LL * W * f[ee-1].Y;
  //      printf("__ %lld %d\n",SU,W);
        int j = 0, SS = S;
        FOR(i, ee, ee+ee-1) {
            while(SS+SS >= S)
            {
                SU -= su[f[j].X] + 1LL * w[f[j].X] * SS;
                W -= w[f[j].X];
                SS -= f[j].Y;
                j++;
            }
//            printf("%lld %d\n",SU,W);
            ret[f[i].X] += SU;
            W += w[f[i].X];
            SU += f[i].Y * W + su[f[i].X];
            SS += f[i].Y;
        }
    }

    REP(i,ee) dfs(f[i].X);
    REP(i,n) cout << ret[i] << " ";
    cout << endl;
    return 0;
}


//Tomasz Kulczyński
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <numeric>
#include <cmath>
#include <cstdlib>
using namespace std;

#define X first
#define Y second
#define MP make_pair
#define PB push_back
typedef long long LL;
typedef double D;
typedef long double ld;
typedef vector<int> VI;
typedef pair<int,int> PII;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(VAR(i,a);i<=(b);++i)
#define FORD(i,a,b) for(VAR(i,a);i>=(b);--i)
#define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a)
#define VAR(a,b) typeof(b) a=(b)
#define SIZE(a) ((int)((a).size()))
#define ALL(x) (x).begin(),(x).end()
#define CLR(x,a) memset(x,a,sizeof(x))
int cond = 1;
#define db(x) {if(cond){cerr << __LINE__ << " " << #x << " " << x << endl; } }
#define dbv(x) {if(cond){cerr << __LINE__ << " " << #x << ": "; FORE(__i,x) cerr << *__i << " "; cerr << endl;} }

const int M = 201013;

int n,ee;
vector<PII> pod[M], v[M];
LL su[M],ret[M];
int w[M],s[M],e[M];
PII f[M+M];

void dfs(int x)
{
    FORE(i, pod[x])
    {
        ret[i->X] = ret[x] + 1LL * i->Y * (n - 2 * w[i->X]);
        dfs(i->X);
    }
}

int main()
{
    scanf("%d",&n);
    REP(i,n)
    {
        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));
        s[a]++;
        s[b]++;
        su[i] = 0;
        w[i] = 1;
    }
    REP(i,n) if(s[i] == 1) e[ee++] = i;
    while(ee)
    {
        int x = e[--ee];
        s[x] = 0;
        FORE(i,v[x]) if(s[i->X])
        {
            su[i->X] += su[x] + 1LL * w[x] * i->Y;
            w[i->X] += w[x];
            pod[i->X].PB(MP(x, i->Y));
            if((--s[i->X]) == 1) e[ee++] = i->X;
        }
    }
    int x = 1;
    REP(i,n) if(s[i]) { x = i; }
    ee = 1;
    FORD(i,SIZE(v[x])-1,0) if(s[v[x][i].X]) { f[0] = MP(x, v[x][i].Y); break; }
    while(s[x])
    {
        s[x] = 0;
        FORE(i, v[x]) if(s[i->X])
        {
            f[ee++]= *i;
            x = i->X;
            break;
        }
    }
    {
        int S = 0, W = 0;
        LL SU = 0;
        REP(i,ee)
        {
       //     printf("%d ",f[i].X);
            S += f[i].Y;
            f[ee+i] = f[i];
            SU += su[f[i].X] + 1LL * W * f[i].Y;
            W += w[f[i].X];
        }
        SU += 1LL * W * f[ee].Y;
      //  printf("\n__ %lld %d\n",SU,W);
        int j = 0, SS = S;
        FOR(i, ee, ee+ee-1) {
            while(SS+SS > S)
            {
                SU -= su[f[j].X] + 1LL * w[f[j].X] * SS;
                W -= w[f[j].X];
                SS -= f[j+1].Y;
                j++;
            }
    //        printf("%lld %d\n",SU,W);
            ret[f[i].X] = su[f[i].X] + SU;
            W += w[f[i].X];
            SU += 1LL * f[i+1].Y * W + su[f[i].X];
            SS += f[i+1].Y;
        }
    }
    REP(i,ee)
        swap(f[i], f[ee+ee-1-i]);
    {
        int S=0, W = 0;
        LL SU = 0;
        REP(i,ee)
        {
            S += f[i].Y;
            SU += su[f[i].X] + 1LL * W * f[i-1].Y;
            W += w[f[i].X];
        }
        SU += 1LL * W * f[ee-1].Y;
  //      printf("__ %lld %d\n",SU,W);
        int j = 0, SS = S;
        FOR(i, ee, ee+ee-1) {
            while(SS+SS >= S)
            {
                SU -= su[f[j].X] + 1LL * w[f[j].X] * SS;
                W -= w[f[j].X];
                SS -= f[j].Y;
                j++;
            }
//            printf("%lld %d\n",SU,W);
            ret[f[i].X] += SU;
            W += w[f[i].X];
            SU += 1LL * f[i].Y * W + su[f[i].X];
            SS += f[i].Y;
        }
    }

    REP(i,ee) dfs(f[i].X);
    REP(i,n) cout << ret[i] << " ";
    cout << endl;
    return 0;
}


#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<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int n;
const int MAXN = 200010;
struct XD{
    int y,l;
    XD(int yy=0,int ll=0):y(yy),l(ll){}
};
vector<XD> ed[MAXN];
int stk[2*MAXN][2],sc;
int vis[MAXN];
int onc[MAXN];
int pre[MAXN];
int prei[MAXN];
vector<pair<int,int> > cyc;
inline void dfs(int st){
    sc=0;
    stk[sc][0]=st;
    stk[sc][1]=0;
    sc++;
    pre[st]=-1;
    vis[st]=1;
    bool ced = 0;
    while(sc){
    int np=stk[sc-1][0],i=stk[sc-1][1];
    sc--;
    if(i==ed[np].size())continue;
    if(ed[np][i].y==pre[np]){
        stk[sc][0]=np;stk[sc][1]=i+1;sc++;continue;
    }
    int y=ed[np][i].y;
    if(vis[y]){
        if(ced)continue;
//      printf("np=%d y=%d\n",np,y);
        // Found the cycle;
        for(int x=np;x!=y;x=pre[x]){
//      printf("# %d %d %d\n",x,pre[x],prei[x]);
        cyc.push_back(make_pair(pre[x],prei[x]));
        onc[pre[x]]=1;
        }
        cyc.push_back(make_pair(np,i));
        onc[np]=1;
        ced=1;
        continue;
    }
    pre[y]=np;
    prei[y]=i;
    vis[y]=1;
    stk[sc][0]=np;stk[sc][1]=i+1;sc++;
    stk[sc][0]=y;stk[sc][1]=0;sc++;
    }
}
long long ans[MAXN];
int cycc;
int dp[MAXN];
long long tor[MAXN];
int root[MAXN];
inline long long dfshei(int np,int p,int& num,long long to,int rr){
    int i;
    long long ret=0;
    tor[np]=to;
    root[np]=rr;
    num=1;
    for(i=0;i<ed[np].size();i++){
    int y=ed[np][i].y;
    if(y==p)continue;
    if(onc[y])continue;
    int s;
    long long w=dfshei(y,np,s,to+ed[np][i].l,rr);
    ret+=s*ed[np][i].l+w;
    num+=s;
    }
    dp[np]=num;
    return ret;
}
inline void dfs2(int np,int p,long long ls,int totp){
    ans[np]+=ls;
    int i;
    for(i=0;i<ed[np].size();i++){
    int y=ed[np][i].y;
    if(y==p)continue;
    if(onc[y])continue;
    dfs2(y,np,ls+(totp-dp[y]-dp[y])*ed[np][i].l,totp);
    }
}
long long totd[MAXN];
int cycl[MAXN];
int tt[MAXN];
//long long tots[MAXN];
int cn[MAXN];
//long long totlw[MAXN];
//long long totl[MAXN];
long long ansc[MAXN];
int main(){
    int i,j;
    scanf("%d",&n);
    for(i=0;i<n;i++){
    int x,y,l;
    scanf("%d%d%d",&x,&y,&l);
    x--;y--;
    ed[x].push_back(XD(y,l));
    ed[y].push_back(XD(x,l));
    }
    dfs(0);
    /*
    for(i=0;i<n;i++){
    for(j=0;j<ed[i].size();j++)printf("%d->%d #%d\n",i,ed[i][j].y,j);
    }
    for(i=0;i<n;i++)printf("%d pre=%d prei=%d\n",i,pre[i],prei[i]);
    for(i=0;i<cyc.size();i++)printf("%d<-%d\n",ed[cyc[i].first][cyc[i].second].y,cyc[i].first);
    */
    cycc=cyc.size();
    for(i=0,j=cycc-1;i<j;i++,j--)swap(cyc[i],cyc[j]);
    for(i=0;i<cycc;i++){
    int r;
    long long tot=dfshei(cyc[i].first,-1,r,0,cyc[i].first);
    totd[cyc[i].first]=tot;
//  printf("%d %I64d\n",cyc[i].first,totd[cyc[i].first]);
    dfs2(cyc[i].first,-1,tot,r);
    }
    long long ctl=0;
    for(i=0;i<cycc;i++){
    cn[cyc[i].first]=i;
    cycl[i]=ed[cyc[i].first][cyc[i].second].l;
    ctl+=cycl[i];
//  printf("%d -(%d)> ",cyc[i].first,cycl[i]);
    }
//    puts("");
    j=0;
    long long cs=0;
    for(i=0;i<cycc;i++){
    while(cs<=ctl-cs){
        cs+=cycl[j];
        j++;
        if(j==cycc)j=0;
    }
    tt[i]=j;
    cs-=cycl[i];
//  printf("%d %d %d\n",i,cyc[i].first,tt[i]);
    }
    /*
    tots[0]=0;
    totlw[0]=0;
    totl[0]=0;
    for(i=0;i<cycc;i++){
    tots[i+1]=tots[i]+totd[cyc[i].first];
    totl[i+1]=totl[i]+cycl[i];
    totlw[i+1]=totlw[i]+totl[i]*dp[cyc[i].first];
    }*/
    for(i=0;i<n;i++){
    ans[i]+=(n-dp[root[i]])*tor[i];
    }
    long long ls=0;
    long long ll=0;
    long long dc=0;
    j=0;
    for(i=0;i<cycc;i++){
    // left half
    while((j+1)%cycc!=tt[i] || ll<0){
        int nj=(j==cycc-1?0:j+1);
        ll+=cycl[j];
        dc+=dp[cyc[nj].first];
        ls+=totd[cyc[nj].first]+ll*dp[cyc[nj].first];
        j=nj;
    }
    ansc[i]+=ls;
    ll-=cycl[i];
    ls-=totd[cyc[(i+1)%cycc].first]+cycl[i]*dc;
    dc-=dp[cyc[(i+1)%cycc].first];
    }
    j=cycc-1;
    ll=0;ls=0;dc=0;
    for(i=cycc-1;i>=0;i--){
    // right half
    while(j!=tt[i] || ll<0){
        int nj=(j==0?cycc-1:j-1);
        ll+=cycl[nj];
        dc+=dp[cyc[nj].first];
        ls+=totd[cyc[nj].first]+ll*dp[cyc[nj].first];
        j=nj;
    }
    ansc[i]+=ls;
    ll-=cycl[(i+cycc-1)%cycc];
    ls-=totd[cyc[(i+cycc-1)%cycc].first]+cycl[(i+cycc-1)%cycc]*dc;
    dc-=dp[cyc[(i+cycc-1)%cycc].first];
    }
    for(i=0;i<n;i++)ans[i]+=ansc[cn[root[i]]];

    for(i=0;i<n;i++)printf("%I64d%c",ans[i],(i==n-1)?'\n':' ');
}


#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int n;
const int MAXN = 200010;
struct XD{
    int y,l;
    XD(int yy=0,int ll=0):y(yy),l(ll){}
};
vector<XD> ed[MAXN];
int stk[2*MAXN][2],sc;
int vis[MAXN];
int onc[MAXN];
int pre[MAXN];
int prei[MAXN];
vector<pair<int,int> > cyc;
inline void dfs(int st){
    sc=0;
    stk[sc][0]=st;
    stk[sc][1]=0;
    sc++;
    pre[st]=-1;
    vis[st]=1;
    bool ced = 0;
    while(sc){
    int np=stk[sc-1][0],i=stk[sc-1][1];
    sc--;
    if(i==ed[np].size())continue;
    if(ed[np][i].y==pre[np]){
        stk[sc][0]=np;stk[sc][1]=i+1;sc++;continue;
    }
    int y=ed[np][i].y;
    if(vis[y]){
        if(ced)continue;
//      printf("np=%d y=%d\n",np,y);
        // Found the cycle;
        for(int x=np;x!=y;x=pre[x]){
//      printf("# %d %d %d\n",x,pre[x],prei[x]);
        cyc.push_back(make_pair(pre[x],prei[x]));
        onc[pre[x]]=1;
        }
        cyc.push_back(make_pair(np,i));
        onc[np]=1;
        ced=1;
        continue;
    }
    pre[y]=np;
    prei[y]=i;
    vis[y]=1;
    stk[sc][0]=np;stk[sc][1]=i+1;sc++;
    stk[sc][0]=y;stk[sc][1]=0;sc++;
    }
}
long long ans[MAXN];
int cycc;
int dp[MAXN];
long long tor[MAXN];
int root[MAXN];
inline long long dfshei(int np,int p,int& num,long long to,int rr){
    int i;
    long long ret=0;
    tor[np]=to;
    root[np]=rr;
    num=1;
    for(i=0;i<ed[np].size();i++){
    int y=ed[np][i].y;
    if(y==p)continue;
    if(onc[y])continue;
    int s;
    long long w=dfshei(y,np,s,to+ed[np][i].l,rr);
    ret+=s*ed[np][i].l+w;
    num+=s;
    }
    dp[np]=num;
    return ret;
}
inline void dfs2(int np,int p,long long ls,int totp){
    ans[np]+=ls;
    int i;
    for(i=0;i<ed[np].size();i++){
    int y=ed[np][i].y;
    if(y==p)continue;
    if(onc[y])continue;
    dfs2(y,np,ls+(totp-dp[y]-dp[y])*ed[np][i].l,totp);
    }
}
long long totd[MAXN];
int cycl[MAXN];
int tt[MAXN];
//long long tots[MAXN];
int cn[MAXN];
//long long totlw[MAXN];
//long long totl[MAXN];
long long ansc[MAXN];
int main(){
    int i,j;
    scanf("%d",&n);
    for(i=0;i<n;i++){
    int x,y,l;
    scanf("%d%d%d",&x,&y,&l);
    x--;y--;
    ed[x].push_back(XD(y,l));
    ed[y].push_back(XD(x,l));
    }
    dfs(0);
    /*
    for(i=0;i<n;i++){
    for(j=0;j<ed[i].size();j++)printf("%d->%d #%d\n",i,ed[i][j].y,j);
    }
    for(i=0;i<n;i++)printf("%d pre=%d prei=%d\n",i,pre[i],prei[i]);
    for(i=0;i<cyc.size();i++)printf("%d<-%d\n",ed[cyc[i].first][cyc[i].second].y,cyc[i].first);
    */
    cycc=cyc.size();
    for(i=0,j=cycc-1;i<j;i++,j--)swap(cyc[i],cyc[j]);
    for(i=0;i<cycc;i++){
    int r;
    long long tot=dfshei(cyc[i].first,-1,r,0,cyc[i].first);
    totd[cyc[i].first]=tot;
    dfs2(cyc[i].first,-1,tot,r);
    }
    long long ctl=0;
    for(i=0;i<cycc;i++){
    cn[cyc[i].first]=i;
    cycl[i]=ed[cyc[i].first][cyc[i].second].l;
    ctl+=cycl[i];
    //printf("%d -(%d)> ",cyc[i].first,cycl[i]);
    }
//    puts("");
    j=0;
    long long cs=0;
    for(i=0;i<cycc;i++){
    while(cs<=ctl-cs){
        cs+=cycl[j];
        j++;
        if(j==cycc)j=0;
    }
    tt[i]=j;
    cs-=cycl[i];
//  printf("%d %d %d\n",i,cyc[i].first,tt[i]);
    }
    /*
    tots[0]=0;
    totlw[0]=0;
    totl[0]=0;
    for(i=0;i<cycc;i++){
    tots[i+1]=tots[i]+totd[cyc[i].first];
    totl[i+1]=totl[i]+cycl[i];
    totlw[i+1]=totlw[i]+totl[i]*dp[cyc[i].first];
    }*/
    for(i=0;i<n;i++){
    ans[i]+=(n-dp[root[i]])*tor[i];
    }
    long long ls=0;
    long long ll=0;
    long long dc=0;
    j=0;
    for(i=0;i<cycc;i++){
    // left half
    while((j+1)%cycc!=tt[i]){
        int nj=(j==cycc-1?0:j+1);
        ll+=cycl[j];
        dc+=dp[cyc[nj].first];
        ls+=totd[cyc[nj].first]+ll*dp[cyc[nj].first];
        j=nj;
    }
    ansc[i]+=ls;
    ll-=cycl[i];
    ls-=totd[cyc[(i+1)%cycc].first]+cycl[i]*dc;
    dc-=dp[cyc[(i+1)%cycc].first];
    }
    j=cycc-1;
    ll=0;ls=0;dc=0;
    for(i=cycc-1;i>=0;i--){
    // right half
    while(j!=tt[i]){
        int nj=(j==0?cycc-1:j-1);
        ll+=cycl[nj];
        dc+=dp[cyc[nj].first];
        ls+=totd[cyc[nj].first]+ll*dp[cyc[nj].first];
        j=nj;
    }
    ansc[i]+=ls;
    ll-=cycl[(i+cycc-1)%cycc];
    ls-=totd[cyc[(i+cycc-1)%cycc].first]+cycl[(i+cycc-1)%cycc]*dc;
    dc-=dp[cyc[(i+cycc-1)%cycc].first];
    }
    for(i=0;i<n;i++)ans[i]+=ansc[cn[root[i]]];

    for(i=0;i<n;i++)printf("%I64d%c",ans[i],(i==n-1)?'\n':' ');
}


#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int n;
const int MAXN = 200010;
struct XD{
    int y,l;
    XD(int yy=0,int ll=0):y(yy),l(ll){}
};
vector<XD> ed[MAXN];
int stk[2*MAXN][2],sc;
int vis[MAXN];
int onc[MAXN];
int pre[MAXN];
int prei[MAXN];
vector<pair<int,int> > cyc;
inline void dfs(int st){
    sc=0;
    stk[sc][0]=st;
    stk[sc][1]=0;
    sc++;
    pre[st]=-1;
    vis[st]=1;
    bool ced = 0;
    while(sc){
	int np=stk[sc-1][0],i=stk[sc-1][1];
	sc--;
	if(i==ed[np].size())continue;
	if(ed[np][i].y==pre[np]){
	    stk[sc][0]=np;stk[sc][1]=i+1;sc++;continue;
	}
	int y=ed[np][i].y;
	if(vis[y]){
	    if(ced)continue;
//	    printf("np=%d y=%d\n",np,y);
	    // Found the cycle;
	    for(int x=np;x!=y;x=pre[x]){
//		printf("# %d %d %d\n",x,pre[x],prei[x]);
		cyc.push_back(make_pair(pre[x],prei[x]));
		onc[pre[x]]=1;
	    }
	    cyc.push_back(make_pair(np,i));
	    onc[np]=1;
	    ced=1;
	    continue;
	}
	pre[y]=np;
	prei[y]=i;
	vis[y]=1;
	stk[sc][0]=np;stk[sc][1]=i+1;sc++;
	stk[sc][0]=y;stk[sc][1]=0;sc++;
    }
}
long long ans[MAXN];
int cycc;
int dp[MAXN];
long long tor[MAXN];
int root[MAXN];
inline long long dfshei(int np,int p,int& num,long long to,int rr){
    int i;
    long long ret=0;
    tor[np]=to;
    root[np]=rr;
    num=1;
    for(i=0;i<ed[np].size();i++){
	int y=ed[np][i].y;
	if(y==p)continue;
	if(onc[y])continue;
	int s;
	long long w=dfshei(y,np,s,to+ed[np][i].l,rr);
	ret+=s*ed[np][i].l+w;
	num+=s;
    }
    dp[np]=num;
    return ret;
}
inline void dfs2(int np,int p,long long ls,int totp){
    ans[np]+=ls;
    int i;
    for(i=0;i<ed[np].size();i++){
	int y=ed[np][i].y;
	if(y==p)continue;
	if(onc[y])continue;
	dfs2(y,np,ls+(totp-dp[y]-dp[y])*ed[np][i].l,totp);
    }
}
long long totd[MAXN];
int cycl[MAXN];
int tt[MAXN];
//long long tots[MAXN];
int cn[MAXN];
//long long totlw[MAXN];
//long long totl[MAXN];
long long ansc[MAXN];
int main(){
    int i,j;
    scanf("%d",&n);
    for(i=0;i<n;i++){
	int x,y,l;
	scanf("%d%d%d",&x,&y,&l);
	x--;y--;
	ed[x].push_back(XD(y,l));
	ed[y].push_back(XD(x,l));
    }
    dfs(0);
    /*
    for(i=0;i<n;i++){
	for(j=0;j<ed[i].size();j++)printf("%d->%d #%d\n",i,ed[i][j].y,j);
    }
    for(i=0;i<n;i++)printf("%d pre=%d prei=%d\n",i,pre[i],prei[i]);
    for(i=0;i<cyc.size();i++)printf("%d<-%d\n",ed[cyc[i].first][cyc[i].second].y,cyc[i].first);
    */
    cycc=cyc.size();
    for(i=0,j=cycc-1;i<j;i++,j--)swap(cyc[i],cyc[j]);
    for(i=0;i<cycc;i++){
	int r;
	long long tot=dfshei(cyc[i].first,-1,r,0,cyc[i].first);
	totd[cyc[i].first]=tot;
	dfs2(cyc[i].first,-1,tot,r);
    }
    long long ctl=0;
    for(i=0;i<cycc;i++){
	cn[cyc[i].first]=i;
	cycl[i]=ed[cyc[i].first][cyc[i].second].l;
	ctl+=cycl[i];
	//printf("%d -(%d)> ",cyc[i].first,cycl[i]);
    }
//    puts("");
    j=0;
    long long cs=0;
    for(i=0;i<cycc;i++){
	while(cs<=ctl-cs){
	    cs+=cycl[j];
	    j++;
	    if(j==cycc)j=0;
	}
	tt[i]=j;
	cs-=cycl[i];
//	printf("%d %d %d\n",i,cyc[i].first,tt[i]);
    }
    /*
    tots[0]=0;
    totlw[0]=0;
    totl[0]=0;
    for(i=0;i<cycc;i++){
	tots[i+1]=tots[i]+totd[cyc[i].first];
	totl[i+1]=totl[i]+cycl[i];
	totlw[i+1]=totlw[i]+totl[i]*dp[cyc[i].first];
    }*/
    for(i=0;i<n;i++){
	ans[i]+=(n-dp[root[i]])*tor[i];
    }
    long long ls=0;
    long long ll=0;
    long long dc=0;
    j=0;
    for(i=0;i<cycc;i++){
	// left half
	while((j+1)%cycc!=tt[i]){
	    int nj=(j==cycc-1?0:j+1);
	    ll+=cycl[j];
	    dc+=dp[cyc[nj].first];
	    ls+=totd[cyc[nj].first]+ll*dp[cyc[nj].first];
	    j=nj;
	}
	ansc[i]+=ls;
	ll-=cycl[i];
	ls-=totd[cyc[(i+1)%cycc].first]+cycl[i]*dc;
	dc-=dp[cyc[(i+1)%cycc].first];
    }
    j=cycc-1;
    ll=0;ls=0;dc=0;
    for(i=cycc-1;i>=0;i--){
	// right half
	while(j!=tt[i]){
	    int nj=(j==0?cycc-1:j-1);
	    ll+=cycl[nj];
	    dc+=dp[cyc[nj].first];
	    ls+=totd[cyc[nj].first]+ll*dp[cyc[nj].first];
	    j=nj;
	}
	ansc[i]+=ls;
	ll-=cycl[(i+cycc-1)%cycc];
	ls-=totd[cyc[(i+cycc-1)%cycc].first]+cycl[(i+cycc-1)%cycc]*dc;
	dc-=dp[cyc[(i+cycc-1)%cycc].first];
    }
    for(i=0;i<n;i++)ans[i]+=ansc[cn[root[i]]];

    for(i=0;i<n;i++)printf("%I64d%c",ans[i],(i==n-1)?'\n':' ');
}


// Codeforces Beta Round #45
// Problem G -- Galaxy Union
#include <cstdio>
#include <cstring>
#include <iostream>

#define N 200000

#define LL long long

using std::cin;
using std::cout;

int n, m, gu[N], gv[N + N], gn[N + N], dg[N], hd, tl, qu[N], pa[N], ci[N], si[N];
LL gw[N + N], cw[N], dd[N], td[N], wst, ws[N];

inline LL get_w(int i, int j){return ws[j] - ws[i] + (i <= j? 0: wst);}

int main()
{
	cin >> n;
	memset(dg, 0, sizeof(dg));
	memset(gu, -1, sizeof(gu));
	for(int i = 0, a, b, c; i != n; ++ i)
	{
		cin >> a >> b >> c;
		dg[-- a] ++, dg[-- b] ++;
		gv[i + i] = b, 	gv[i + i + 1] = a;
		gw[i + i] = gw[i + i + 1] = c;
		gn[i + i] = gu[a], gu[a] = i + i;
		gn[i + i + 1] = gu[b], gu[b] = i + i + 1;
	}

	hd = tl = 0;
	for(int i = 0; i != n; ++ i)
		if(dg[i] == 1)
			qu[tl ++] = i;
	for(; hd != tl; ++ hd)
		for(int i = gu[qu[hd]]; i != -1; i = gn[i])
		{
			if(dg[gv[i]] == 1)
				continue;
			pa[qu[hd]] = i;
			if(-- dg[gv[i]] == 1)
				qu[tl ++] = gv[i];
		}
	m = 1;
	for(ci[0] = 0; dg[ci[0]] != 2; ++ ci[0]);
	for(int i = 0; i != m; ++ i)
	{
		int pv = i? ci[i - 1]: -1;
		for(int j = gu[ci[i]]; j != -1; j = gn[j])
			if(dg[gv[j]] == 2 && gv[j] != pv)
			{
				cw[i] = gw[j];
				if(gv[j] != ci[0])
				{
					ci[m ++] = gv[j];
					break;
				}
			}
	}

	for(int i = 0; i != n; ++ i)
		si[i] = 1;
	memset(dd, 0, sizeof(dd));
	for(int i = 0; i != tl; ++ i)
	{
		int u = qu[i], v = gv[pa[u]], w = gw[pa[u]];
		si[v] += si[u];
		dd[v] += dd[u] + si[u] * w;
	}

	LL tmp = 0;
	for(int i = 0; i != m; ++ i)
		tmp += dd[ci[i]];
	for(int i = 0; i != m; ++ i)
		td[ci[i]] = tmp;

	wst = ws[0] = 0;
	for(int i = 0; i != m; ++ i)
	{
		wst += cw[i];
		if(i)
			ws[i] = ws[i - 1] + cw[i - 1];
	}

	int p = 0;
	while(p != m - 1 && get_w(0, p + 1) < get_w(p + 1, 0))
		p ++;

	int tmp_s_l = 0, tmp_s_r = 0;
	LL tmp_w_l = 0, tmp_w_r = 0;
	for(int i = 0; i != m; ++ i)
		if(i > p)
			tmp_s_l += si[ci[i]], tmp_w_l += si[ci[i]] * get_w(i, 0);
		else
			tmp_s_r += si[ci[i]], tmp_w_r += si[ci[i]] * get_w(0, i);
	td[ci[0]] += tmp_w_l + tmp_w_r;

	for(int i = 1; i != m; ++ i)
	{
		tmp_w_r -= cw[i - 1] * (tmp_s_r -= si[ci[i - 1]]);
		tmp_w_l += cw[i - 1] * (tmp_s_l += si[ci[i - 1]]);
		if(p == i - 1)
		{
			p = (p + 1) % m;
			tmp_w_l -= wst * si[ci[i]];
			tmp_s_l -= si[ci[i]];
			tmp_s_r += si[ci[i]];
		}
		while((p + 1) % m != i && get_w(i, (p + 1) % m) < get_w((p + 1) % m, i))
		{
			p = (p + 1) % m;
			tmp_s_r ++, tmp_w_r += si[ci[p]] * get_w(i, p);
			tmp_s_l --, tmp_w_l -= si[ci[p]] * get_w(p, i);
		}
		td[ci[i]] += tmp_w_l + tmp_w_r;
	}

	for(int i = tl - 1; i != -1; -- i)
	{
		int u = qu[i], v = gv[pa[u]], w = gw[pa[u]];
		td[u] = td[v] + (n - si[u] * 2) * w;
	}

	for(int i = 0; i != n; ++ i)
		cout << td[i] << " ";
	cout << "\n";
	return 0;
}


// Codeforces Beta Round #45
// Problem G -- Galaxy Union
#include <cstdio>
#include <cstring>
#include <iostream>

#define N 200000

#define LL long long

using std::cin;
using std::cout;

int n, m, gu[N], gv[N + N], gn[N + N], dg[N], hd, tl, qu[N], pa[N], ci[N], si[N];
LL gw[N + N], cw[N], dd[N], td[N], wst, ws[N];

inline LL get_w(int i, int j){return ws[j] - ws[i] + (i <= j? 0: wst);}

int main()
{
	cin >> n;
	memset(dg, 0, sizeof(dg));
	memset(gu, -1, sizeof(gu));
	for(int i = 0, a, b, c; i != n; ++ i)
	{
		cin >> a >> b >> c;
		dg[-- a] ++, dg[-- b] ++;
		gv[i + i] = b, 	gv[i + i + 1] = a;
		gw[i + i] = gw[i + i + 1] = c;
		gn[i + i] = gu[a], gu[a] = i + i;
		gn[i + i + 1] = gu[b], gu[b] = i + i + 1;
	}

	hd = tl = 0;
	for(int i = 0; i != n; ++ i)
		if(dg[i] == 1)
			qu[tl ++] = i;
	for(; hd != tl; ++ hd)
		for(int i = gu[qu[hd]]; i != -1; i = gn[i])
		{
			if(dg[gv[i]] == 1)
				continue;
			pa[qu[hd]] = i;
			if(-- dg[gv[i]] == 1)
				qu[tl ++] = gv[i];
		}
	m = 1;
	for(ci[0] = 0; dg[ci[0]] != 2; ++ ci[0]);
	for(int i = 0; i != m; ++ i)
	{
		int pv = i? ci[i - 1]: -1;
		for(int j = gu[ci[i]]; j != -1; j = gn[j])
			if(dg[gv[j]] == 2 && gv[j] != pv)
			{
				cw[i] = gw[j];
				if(gv[j] != ci[0])
				{
					ci[m ++] = gv[j];
					break;
				}
			}
	}

	for(int i = 0; i != n; ++ i)
		si[i] = 1;
	memset(dd, 0, sizeof(dd));
	for(int i = 0; i != tl; ++ i)
	{
		int u = qu[i], v = gv[pa[u]], w = gw[pa[u]];
		si[v] += si[u];
		dd[v] += dd[u] + si[u] * w;
	}
	for(int i = 0; i != n; ++ i)
		if(dg[i] == 2)
			td[i] = dd[i];

	LL tmp = 0;
	for(int i = 0; i != m; ++ i)
		tmp += dd[ci[i]];
	for(int i = 0; i != m; ++ i)
		td[ci[i]] = tmp - dd[ci[i]];

	wst = ws[0] = 0;
	for(int i = 0; i != m; ++ i)
	{
		wst += cw[i];
		if(i)
			ws[i] = ws[i - 1] + cw[i - 1];
	}

	int p = 0;
	while(p != m - 1 && get_w(0, p + 1) < get_w(p + 1, 0))
		p ++;

	int tmp_s_l = 0, tmp_s_r = 0;
	LL tmp_w_l = 0, tmp_w_r = 0;
	for(int i = 0; i != m; ++ i)
		if(i > p)
			tmp_s_l += si[ci[i]], tmp_w_l += si[ci[i]] * get_w(i, 0);
		else
			tmp_s_r += si[ci[i]], tmp_w_r += si[ci[i]] * get_w(0, i);
	td[ci[0]] += tmp_w_l + tmp_w_r;

	for(int i = 1; i != m; ++ i)
	{
		tmp_w_r -= cw[i - 1] * (tmp_s_r -= si[ci[i - 1]]);
		tmp_w_l += cw[i - 1] * (tmp_s_l += si[ci[i - 1]]);
		if(p == i - 1)
		{
			p = (p + 1) % m;
			tmp_w_l -= wst * si[ci[i]];
			tmp_s_l -= si[ci[i]];
			tmp_s_r += si[ci[i]];
		}
		while((p + 1) % m != i && get_w(i, (p + 1) % m) < get_w((p + 1) % m, i))
		{
			p = (p + 1) % m;
			tmp_s_r ++, tmp_w_r += si[ci[p]] * get_w(i, p);
			tmp_s_l --, tmp_w_l -= si[ci[p]] * get_w(p, i);
		}
		td[ci[i]] += tmp_w_l + tmp_w_r;
	}

	for(int i = tl - 1; i != -1; -- i)
	{
		int u = qu[i], v = gv[pa[u]], w = gw[pa[u]];
		td[u] = td[v] + (si[v] - si[u] * 2) * w;
	}

	for(int i = 0; i != n; ++ i)
		cout << td[i] << " ";
	cout << "\n";
	return 0;
}


// Codeforces Beta Round #45
// Problem G -- Galaxy Union
#include <cstdio>
#include <cstring>
#include <iostream>

#define N 200000

#define LL long long

using std::cin;
using std::cout;

int n, m, gu[N], gv[N + N], gn[N + N], dg[N], hd, tl, qu[N], pa[N], ci[N], si[N];
LL gw[N + N], cw[N], dd[N], td[N], wst, ws[N];

inline LL get_w(int i, int j){return ws[j] - ws[i] + (i <= j? 0: wst);}

int main()
{
	cin >> n;
	memset(dg, 0, sizeof(dg));
	memset(gu, -1, sizeof(gu));
	for(int i = 0, a, b, c; i != n; ++ i)
	{
		cin >> a >> b >> c;
		dg[-- a] ++, dg[-- b] ++;
		gv[i + i] = b, 	gv[i + i + 1] = a;
		gw[i + i] = gw[i + i + 1] = c;
		gn[i + i] = gu[a], gu[a] = i + i;
		gn[i + i + 1] = gu[b], gu[b] = i + i + 1;
	}

	hd = tl = 0;
	for(int i = 0; i != n; ++ i)
		if(dg[i] == 1)
			qu[tl ++] = i;
	for(; hd != tl; ++ hd)
		for(int i = gu[qu[hd]]; i != -1; i = gn[i])
		{
			if(dg[gv[i]] == 1)
				continue;
			pa[qu[hd]] = i;
			if(-- dg[gv[i]] == 1)
				qu[tl ++] = gv[i];
		}
	m = 1;
	for(ci[0] = 0; dg[ci[0]] != 2; ++ ci[0]);
	for(int i = 0; i != m; ++ i)
	{
		int pv = i? ci[i - 1]: -1;
		for(int j = gu[ci[i]]; j != -1; j = gn[j])
			if(dg[gv[j]] == 2 && gv[j] != pv)
			{
				cw[i] = gw[j];
				if(gv[j] != ci[0])
				{
					ci[m ++] = gv[j];
					break;
				}
			}
	}

	for(int i = 0; i != n; ++ i)
		si[i] = 1;
	memset(dd, 0, sizeof(dd));
	for(int i = 0; i != tl; ++ i)
	{
		int u = qu[i], v = gv[pa[u]], w = gw[pa[u]];
		si[v] += si[u];
		dd[v] += dd[u] + si[u] * w;
	}

	LL tmp = 0;
	for(int i = 0; i != m; ++ i)
		tmp += dd[ci[i]];
	for(int i = 0; i != m; ++ i)
		td[ci[i]] = tmp;

	wst = ws[0] = 0;
	for(int i = 0; i != m; ++ i)
	{
		wst += cw[i];
		if(i)
			ws[i] = ws[i - 1] + cw[i - 1];
	}

	int p = 0;
	while(p != m - 1 && get_w(0, p + 1) < get_w(p + 1, 0))
		p ++;

	int tmp_s_l = 0, tmp_s_r = 0;
	LL tmp_w_l = 0, tmp_w_r = 0;
	for(int i = 0; i != m; ++ i)
		if(i > p)
			tmp_s_l += si[ci[i]], tmp_w_l += si[ci[i]] * get_w(i, 0);
		else
			tmp_s_r += si[ci[i]], tmp_w_r += si[ci[i]] * get_w(0, i);
	td[ci[0]] += tmp_w_l + tmp_w_r;

	for(int i = 1; i != m; ++ i)
	{
		tmp_w_r -= cw[i - 1] * (tmp_s_r -= si[ci[i - 1]]);
		tmp_w_l += cw[i - 1] * (tmp_s_l += si[ci[i - 1]]);
		if(p == i - 1)
		{
			p = i;
			tmp_w_l -= wst * si[ci[i]];
			tmp_s_l -= si[ci[i]];
			tmp_s_r += si[ci[i]];
		}
		while((p + 1) % m != i && get_w(i, (p + 1) % m) < get_w((p + 1) % m, i))
		{
			p = (p + 1) % m;
			tmp_s_r += si[ci[p]], tmp_w_r += si[ci[p]] * get_w(i, p);
			tmp_s_l -= si[ci[p]], tmp_w_l -= si[ci[p]] * get_w(p, i);
		}
		td[ci[i]] += tmp_w_l + tmp_w_r;
	}

	for(int i = tl - 1; i != -1; -- i)
	{
		int u = qu[i], v = gv[pa[u]], w = gw[pa[u]];
		td[u] = td[v] + (n - si[u] * 2) * w;
	}

	for(int i = 0; i != n; ++ i)
		cout << td[i] << " ";
	cout << "\n";
	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()
{
	//freopen("input.txt","r",stdin);
	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;
}


#include <stack>
#include <cstdio>
#include <vector>
#include <numeric>
#include <utility>
#include <algorithm>

using namespace std;

struct Root {
	int p, c;
	long long e, x;
	vector<int> v;
	Root(int p, long long e) : p(p), e(e) { }
};

const int MAXN = 200200;
bool oncirc[MAXN];
vector<pair<int, long long> > e[MAXN];
vector<Root> circ;
long long ans[MAXN];

bool mark[MAXN];

int dfs(int v, int p) {
	if (mark[v]) {
		return v;
	} else {
		mark[v] = true;
	}
	for (vector<pair<int, long long> >::const_iterator it = e[v].begin(); it != e[v].end(); ++it) {
		int w = it->first;
		if (w == p) {
			continue;
		}
		int t = dfs(w, v);
		if (t != -1) {
			circ.push_back(Root(w, it->second));
			return v == t ? -1 : t;
		}
	}
	return -1;
}

int findcirc(int n) {
	int m;

	dfs(0, -1);
	m = circ.size();
	for (int i = 0; i < m; ++i) {
		oncirc[circ[i].p] = true;
	}
	return m;
}

Root* root;
long long dd[MAXN], cc[MAXN], ss[MAXN], xx[MAXN];

void gao1(int v, int p) {
	root->v.push_back(v);
	cc[v] = 1;
	ss[v] = 0;
	for (vector<pair<int, long long> >::const_iterator it = e[v].begin(); it != e[v].end(); ++it) {
		int w = it->first;
		if (w == p || oncirc[w]) {
			continue;
		}
		dd[w] = dd[v] + it->second;
		gao1(w, v);
		cc[v] += cc[w];
		ss[v] += ss[w] + cc[w] * it->second;
	}
}

void gao2(int v, int p, int n) {
	for (vector<pair<int, long long> >::const_iterator it = e[v].begin(); it != e[v].end(); ++it) {
		int w = it->first;
		if (w == p || oncirc[w]) {
			continue;
		}
		xx[w] = xx[v] + (ss[v] - (ss[w] + cc[w] * it->second)) + (n - cc[w]) * it->second;
		gao2(w, v, n);
	}
}

void calctree(Root& r) {
	root = &r;
	dd[r.p] = 0;
	gao1(r.p, -1);
	xx[r.p] = 0;
	r.c = cc[r.p];
	gao2(r.p, -1, r.c);
	for (vector<int>::const_iterator it = r.v.begin(); it != r.v.end(); ++it) {
		ans[*it] = xx[*it] + ss[*it];
	}
	r.x = ans[r.p];
}

long long ee[MAXN + MAXN];

void calccirc(int n, int m) {
	long long circlen = 0;
	long long total = 0;
	long long cursum = 0;
	int curcnt = 0;
	int q = 0;

	for (int i = 0; i < m; ++i) {
		circlen += circ[i].e;
		total += circ[i].x;
		ee[i + 1 + m] = ee[i + 1] = circ[i].e;
	}
	ee[0] = 0;
	partial_sum(ee, ee + m + m + 1, ee);
	while (2 * (ee[q] - ee[0]) <= circlen) {
		cursum += (ee[q] - ee[0]) * circ[q % m].c;
		curcnt += circ[q % m].c;
		++q;
	}
	for (int i = q; i < m; ++i) {
		cursum += (circlen - (ee[i] - ee[0])) * circ[i % m].c;
	}
	for (int i = 0; i < m; ++i) {
		while (2 * (ee[q] - ee[i]) <= circlen) {
			cursum -= (circlen - 2 * (ee[q] - ee[i])) * circ[q % m].c;
			curcnt += circ[q % m].c;
			++q;
		}
		for (vector<int>::const_iterator it = circ[i].v.begin(); it != circ[i].v.end(); ++it) {
			ans[*it] += cursum + total - circ[i].x + dd[*it] * (n - circ[i].c);
		}
		curcnt -= circ[i].c;
		cursum += (n - curcnt) * circ[i].e;
		cursum -= curcnt * circ[i].e;
	}
}

int main() {
	int n, m, a, b, c;

	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d%d%d", &a, &b, &c);
		--a;
		--b;
		e[a].push_back(make_pair(b, c));
		e[b].push_back(make_pair(a, c));
	}

	m = findcirc(n);
	for (int i = 0; i < m; ++i) {
		calctree(circ[i]);
	}
	calccirc(n, m);

	for (int i = 0; i < n; ++i) {
		printf("%I64d%c", ans[i], i == n - 1 ? '\n' : ' ');
	}

	return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define LL long long
const int maxn = 200000;
int n;
vector <pair<int,int> > edge[200000];
bool onCircle[maxn];
bool vis[maxn];
LL down[maxn];
LL up[maxn];
int cnt[maxn];
int dis[maxn * 3];
int circle[maxn * 3];
int num[maxn * 3];
int m;

int findCircle(int u,int pre) {
	if (vis[u]) return u;
	vis[u] = true;
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre) continue;
		int t = findCircle(v , u);
		if (t != -1) {
			circle[m] = v;
			dis[m++] = it->second;
			onCircle[v] = true;
			return t == u ? -1 : t;
		}
	}
	return -1;
}
void godown(int u,int pre) {
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre || onCircle[v]) continue;
		godown(v , u);
		cnt[u] += cnt[v];
		down[u] += cnt[v] * it->second + down[u];
	}
}
void goup(int u,int pre) {
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre || onCircle[v]) continue;
		up[v] = up[u] + down[u] - down[v] + (n - 2 * cnt[v]) * it->second;
		godown(v , u);
	}
}
void calcCircle() {
	LL tots = 0;
	for (int i = 0 ; i < m ; i ++) {
		tots += down[ circle[i] ];
		num[i] = num[i+m] = num[i+m+m] = cnt[ circle[i] ];
		circle[i+m] = circle[i+m+m] = circle[i];
		dis[i+m] = dis[i+m+m] = dis[i];
	}
	int leftd = 0, rightd = 0;
	int leftc = 0, rightc = 0;
	LL lefts = 0, rights = 0;
	int l = m, r = m , it = m;
	int T = m - 1;
	while(T --) {
		if (leftd + dis[l - 1] <= rightd + dis[r]) {
			l --;
			leftd += dis[l];
			leftc += num[l];
			lefts += num[l] * leftd;
		} else {
			rightd += dis[r];
			rightc += num[r];
			rights += num[r] * rightd;
			r ++;
		}
	}
	up[ circle[it] ] = lefts + rights + tots - down[ circle[it] ];
	T = m - 1;
	while(T --) {
		leftc += num[it];
		leftd += dis[it];
		lefts += dis[it] * leftc;
		rights -= dis[it] * rightc;
		rightc -= num[it];
		rightd -= dis[it];
		it ++;
		while(leftd > rightd + dis[r]) {
			lefts -= num[l] * leftd;
			leftd -= dis[l];
			leftc -= num[l];
			l ++;
			rightd += dis[r];
			rightc += num[r];
			rights += num[r] * rightd;
			r ++;
		}
		up[ circle[it] ] = lefts + rights + tots - down[ circle[it] ];
	}
}
void init() {
	for (int i = 0 ; i < n ; i ++) {
		edge[i].clear();
		vis[i] = false;
		onCircle[i] = false;
		cnt[i] = 1;
		down[i] = up[i] = 0;
	}
	m = 0;
}
void input() {
	for (int i = 0 ; i < n ; i ++) {
		int a , b , c;
		scanf("%d%d%d",&a,&b,&c);
		a -- , b --;
		edge[a].push_back(make_pair(b , c));
		edge[b].push_back(make_pair(a , c));
	}
}
int main() {
	while (~scanf("%d",&n)){
		init();
		input();

		findCircle(0 , -1);
		for (int i = 0 ; i < m ; i ++) {
			godown(circle[i] , -1);
		}
		calcCircle();
		for (int i = 0 ; i < m ; i ++) {
			goup(circle[i], -1);
		}
		for (int i = 0 ; i < n ; i ++) {
			printf("%I64d%c",up[i] + down[i] , i == n - 1 ? '\n' : ' ');
		}
	}
	return 0;
}
/*
5
1 2 1
2 3 1
3 4 1
4 5 1
5 2 1

*/


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define LL long long
const int maxn = 200000;
int n;
vector <pair<int,int> > edge[200000];
bool onCircle[maxn];
bool vis[maxn];
LL down[maxn];
LL up[maxn];
int cnt[maxn];
int dis[maxn * 3];
int circle[maxn * 3];
int num[maxn * 3];
int m;

int findCircle(int u,int pre) {
	if (vis[u]) return u;
	vis[u] = true;
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre) continue;
		int t = findCircle(v , u);
		if (t != -1) {
			circle[m] = v;
			dis[m++] = it->second;
			onCircle[v] = true;
			return t == u ? -1 : t;
		}
	}
	return -1;
}
void godown(int u,int pre) {
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre || onCircle[v]) continue;
		godown(v , u);
		cnt[u] += cnt[v];
		down[u] += cnt[v] * it->second + down[u];
	}
}
void goup(int u,int pre) {
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre || onCircle[v]) continue;
		up[v] = up[u] + down[u] - down[v] + (n - 2 * cnt[v]) * it->second;
		godown(v , u);
	}
}
void calcCircle() {
	LL tots = 0;
	for (int i = 0 ; i < m ; i ++) {
		tots += down[ circle[i] ];
		num[i] = num[i+m] = num[i+m+m] = cnt[ circle[i] ];
		circle[i+m] = circle[i+m+m] = circle[i];
		dis[i+m] = dis[i+m+m] = dis[i];
	}
	int leftd = 0, rightd = 0;
	int leftc = 0, rightc = 0;
	LL lefts = 0, rights = 0;
	int l = m, r = m , it = m;
	int T = m - 1;
	while(T --) {
		if (leftd + dis[l - 1] <= rightd + dis[r]) {
			l --;
			leftd += dis[l];
			leftc += num[l];
			lefts += num[l] * leftd;
		} else {
			rightd += dis[r];
			rightc += num[r];
			rights += num[r] * rightd;
			r ++;
		}
	}
	up[ circle[it] ] = lefts + rights + tots - down[ circle[it] ];
	T = m - 1;
	while(T --) {
		leftc += num[it];
		leftd += dis[it];
		lefts += dis[it] * leftc;
		rights -= dis[it] * rightc;
		rightc -= num[it];
		rightd -= dis[it];
		it ++;
		while(leftd > rightd + dis[r]) {
			lefts -= num[l] * leftd;
			leftd -= dis[l];
			leftc -= num[l];
			l ++;
			rightd += dis[r];
			rightc += num[r];
			rights += num[r] * rightd;
			r ++;
		}
		up[ circle[it] ] = lefts + rights + tots - down[ circle[it] ];
	}
}
void init() {
	for (int i = 0 ; i < n ; i ++) {
		edge[i].clear();
		vis[i] = false;
		onCircle[i] = false;
		cnt[i] = 1;
		down[i] = up[i] = 0;
	}
	m = 0;
}
void input() {
	for (int i = 0 ; i < n ; i ++) {
		int a , b , c;
		scanf("%d%d%d",&a,&b,&c);
		a -- , b --;
		edge[a].push_back(make_pair(b , c));
		edge[b].push_back(make_pair(a , c));
	}
}
int main() {
	while (scanf("%d",&n)){
		init();
		input();

		findCircle(0 , -1);
		for (int i = 0 ; i < m ; i ++) {
			godown(circle[i] , -1);
		}
		calcCircle();
		for (int i = 0 ; i < m ; i ++) {
			goup(circle[i], -1);
		}
		for (int i = 0 ; i < n ; i ++) {
			printf("%I64d%c",up[i] + down[i] , i == n - 1 ? '\n' : ' ');
		}
	}
	return 0;
}
/*
5
1 2 1
2 3 1
3 4 1
4 5 1
5 2 1

*/


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define LL long long
const int maxn = 200000;
int n;
vector <pair<int,int> > edge[200000];
bool onCircle[maxn];
bool vis[maxn];
LL down[maxn];
LL up[maxn];
int cnt[maxn];
int dis[maxn * 3];
int circle[maxn * 3];
int num[maxn * 3];
int m;

int findCircle(int u,int pre) {
	if (vis[u]) return u;
	vis[u] = true;
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre) continue;
		int t = findCircle(v , u);
		if (t != -1) {
			circle[m] = v;
			dis[m++] = it->second;
			onCircle[v] = true;
			return t == u ? -1 : t;
		}
	}
	return -1;
}
void godown(int u,int pre) {
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre || onCircle[v]) continue;
		godown(v , u);
		cnt[u] += cnt[v];
		down[u] += cnt[v] * it->second + down[v];
	}
}
void goup(int u,int pre) {
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre || onCircle[v]) continue;
		up[v] = up[u] + down[u] - down[v] + (n - 2 * cnt[v]) * it->second;
		goup(v , u);
	}
}
void calcCircle() {
	LL tots = 0;
	for (int i = 0 ; i < m ; i ++) {
		tots += down[ circle[i] ];
		num[i] = num[i+m] = num[i+m+m] = cnt[ circle[i] ];
		circle[i+m] = circle[i+m+m] = circle[i];
		dis[i+m] = dis[i+m+m] = dis[i];
	}
	int leftd = 0, rightd = 0;
	int leftc = 0, rightc = 0;
	LL lefts = 0, rights = 0;
	int l = m, r = m , it = m;
	int T = m - 1;
	while(T --) {
		if (leftd + dis[l - 1] <= rightd + dis[r]) {
			l --;
			leftd += dis[l];
			leftc += num[l];
			lefts += num[l] * leftd;
		} else {
			rightd += dis[r];
			rightc += num[r+1];
			rights += num[r+1] * rightd;
			r ++;
		}
	}
	up[ circle[it] ] = lefts + rights + tots - down[ circle[it] ];
	T = m - 1;
	while(T --) {
		leftc += num[it];
		leftd += dis[it];
		lefts += dis[it] * leftc;
		rights -= dis[it] * rightc;
		rightc -= num[it+1];
		rightd -= dis[it];
		it ++;
		while(leftd > rightd + dis[r]) {
			lefts -= num[l] * leftd;
			leftd -= dis[l];
			leftc -= num[l];
			l ++;
			rightd += dis[r];
			rightc += num[r+1];
			rights += num[r+1] * rightd;
			r ++;
		}
		up[ circle[it] ] = lefts + rights + tots - down[ circle[it] ];
	}
}
void init() {
	for (int i = 0 ; i < n ; i ++) {
		edge[i].clear();
		vis[i] = false;
		onCircle[i] = false;
		cnt[i] = 1;
		down[i] = up[i] = 0;
	}
	m = 0;
}
void input() {
	for (int i = 0 ; i < n ; i ++) {
		int a , b , c;
		scanf("%d%d%d",&a,&b,&c);
		a -- , b --;
		edge[a].push_back(make_pair(b , c));
		edge[b].push_back(make_pair(a , c));
	}
}
int main() {
	while (~scanf("%d",&n)){
		init();
		input();

		findCircle(0 , -1);
		for (int i = 0 ; i < m ; i ++) {
			godown(circle[i] , -1);
		}
		calcCircle();
		for (int i = 0 ; i < m ; i ++) {
			goup(circle[i], -1);
		}
		for (int i = 0 ; i < n ; i ++) {
			printf("%I64d%c",up[i] + down[i] , i == n - 1 ? '\n' : ' ');
		}
	}
	return 0;
}
/*
5
1 2 1
2 3 1
3 4 1
4 5 1
5 2 1

*/


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define LL long long
const int maxn = 200000;
int n;
vector <pair<int,int> > edge[200000];
bool onCircle[maxn];
bool vis[maxn];
LL down[maxn];
LL up[maxn];
int cnt[maxn];
int dis[maxn * 3];
int circle[maxn * 3];
int num[maxn * 3];
int m;

int findCircle(int u,int pre) {
	if (vis[u]) return u;
	vis[u] = true;
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre) continue;
		int t = findCircle(v , u);
		if (t != -1) {
			circle[m] = v;
			dis[m++] = it->second;
			onCircle[v] = true;
			return t == u ? -1 : t;
		}
	}
	return -1;
}
void godown(int u,int pre) {
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre || onCircle[v]) continue;
		godown(v , u);
		cnt[u] += cnt[v];
		down[u] += cnt[v] * it->second + down[u];
	}
}
void goup(int u,int pre) {
	for (vector <pair<int,int> >::iterator it = edge[u].begin() ; it != edge[u].end() ; it ++) {
		int v = it->first;
		if (v == pre || onCircle[v]) continue;
		up[v] = up[u] + down[u] - down[v] + (n - 2 * cnt[v]) * it->second;
		godown(v , u);
	}
}
void calcCircle() {
	LL tots = 0;
	for (int i = 0 ; i < m ; i ++) {
		tots += down[ circle[i] ];
		num[i] = num[i+m] = num[i+m+m] = cnt[ circle[i] ];
		circle[i+m] = circle[i+m+m] = circle[i];
		dis[i+m] = dis[i+m+m] = dis[i];
	}
	int leftd = 0, rightd = 0;
	int leftc = 0, rightc = 0;
	LL lefts = 0, rights = 0;
	int l = m, r = m , it = m;
	int T = m - 1;
	while(T --) {
		if (leftd + dis[l - 1] <= rightd + dis[r]) {
			l --;
			leftd += dis[l];
			leftc += num[l];
			lefts += num[l] * leftd;
		} else {
			rightd += dis[r];
			rightc += num[r+1];
			rights += num[r+1] * rightd;
			r ++;
		}
	}
	up[ circle[it] ] = lefts + rights + tots - down[ circle[it] ];
	T = m - 1;
	while(T --) {
		leftc += num[it];
		leftd += dis[it];
		lefts += dis[it] * leftc;
		rights -= dis[it] * rightc;
		rightc -= num[it+1];
		rightd -= dis[it];
		it ++;
		while(leftd > rightd + dis[r]) {
			lefts -= num[l] * leftd;
			leftd -= dis[l];
			leftc -= num[l];
			l ++;
			rightd += dis[r];
			rightc += num[r+1];
			rights += num[r+1] * rightd;
			r ++;
		}
		up[ circle[it] ] = lefts + rights + tots - down[ circle[it] ];
	}
}
void init() {
	for (int i = 0 ; i < n ; i ++) {
		edge[i].clear();
		vis[i] = false;
		onCircle[i] = false;
		cnt[i] = 1;
		down[i] = up[i] = 0;
	}
	m = 0;
}
void input() {
	for (int i = 0 ; i < n ; i ++) {
		int a , b , c;
		scanf("%d%d%d",&a,&b,&c);
		a -- , b --;
		edge[a].push_back(make_pair(b , c));
		edge[b].push_back(make_pair(a , c));
	}
}
int main() {
	while (~scanf("%d",&n)){
		init();
		input();

		findCircle(0 , -1);
		for (int i = 0 ; i < m ; i ++) {
			godown(circle[i] , -1);
		}
		calcCircle();
		for (int i = 0 ; i < m ; i ++) {
			goup(circle[i], -1);
		}
		for (int i = 0 ; i < n ; i ++) {
			printf("%I64d%c",up[i] + down[i] , i == n - 1 ? '\n' : ' ');
		}
	}
	return 0;
}
/*
5
1 2 1
2 3 1
3 4 1
4 5 1
5 2 1

*/


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

const int MAXN = 200005;
vector< pair<int, int> > gra[MAXN];
int fa[MAXN], c[MAXN];
int loop[MAXN], loopn, loopcost[MAXN], loopsum;
bool flag[MAXN];
int treecnt[MAXN], treesum[MAXN], sum[MAXN];
int n;

void DFS0(int nowp, int pre)
{

    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre)
    {
        if(fa[iter->first] == 0)
        {
            fa[iter->first] = nowp;
            c[iter->first] = iter->second;
            DFS0(iter->first, nowp);
        }
        else
        {
            if(loopn > 0) return;
            for(int x = nowp; x != iter->first; x = fa[x])
                loop[loopn] = x, loopcost[loopn++] = c[x];
            loop[loopn] = iter->first, loopcost[loopn++] = c[iter->first];
            return;
        }
    }

    return;
}

void DFS1(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        DFS1(iter->first, nowp);
        treecnt[nowp] += treecnt[iter->first];
        treesum[nowp] += treecnt[iter->first] * iter->second;
    }

    return;
}

void DFS2(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        sum[iter->first] = sum[nowp] - treecnt[iter->first] * iter->second + (n - treecnt[iter->first]) * iter->second;
        DFS2(iter->first, nowp);
    }

    return;
}

int main()
{
    int loopintv, nowindex, temsum, intvcnt;

    // freopen("D:\\1.in", "r", stdin);
    // freopen("D:\\1.out", "w", stdout);

    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        int a, b, t;
        scanf("%d%d%d", &a, &b, &t);
        gra[a].push_back(make_pair(b, t));
        gra[b].push_back(make_pair(a, t));
    }

    // Find loop
    fa[1] = -1;
    DFS0(1, 0);
    for(int i = 0; i < loopn; i++) loopsum += loopcost[i];

    // DFS nodes except for the loop
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS1(loop[i], 0);

    // Calculate answers for loop nodes
    loopintv = nowindex = temsum = intvcnt = 0;
    for(nowindex = 0; nowindex < loopn; nowindex++)
    if((loopintv + loopcost[nowindex]) * 2 <= loopsum)
    {
        loopintv += loopcost[nowindex];
        temsum += treesum[loop[(nowindex + 1) % loopn]] + loopintv * treecnt[loop[(nowindex + 1) % loopn]];
        intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
    }
    else break;
    for(int i = nowindex, j = loopsum - loopintv; i < loopn; i++)
    {
        j -= loopcost[i];
        temsum += treesum[loop[(i + 1) % loopn]] + j * treecnt[loop[(i + 1) % loopn]];
    }
    for(int i = 0; i < loopn; i++)
    {
        sum[loop[i]] = temsum;
        temsum += (n - intvcnt) * loopcost[i] - intvcnt * loopcost[i];
        loopintv -= loopcost[i];
        intvcnt -= treecnt[loop[i]];
        while((loopintv + loopcost[nowindex]) * 2 <= loopsum)
        {
            loopintv += loopcost[nowindex];
            temsum += loopintv * treecnt[loop[(nowindex + 1) % loopn]] - (loopsum - loopintv) * treecnt[loop[(nowindex + 1) % loopn]];
            intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
            nowindex = (nowindex + 1) % loopn;
        }
    }

    // Calculate tree nodes
    memset(flag, 0, sizeof(flag));
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS2(loop[i], 0);

    // Output
    for(int i = 1; i <= n; i++) printf("%d ", sum[i]);

    return 0;
}


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

const int MAXN = 200005;
vector< pair<int, int> > gra[MAXN];
int fa[MAXN], c[MAXN];
int loop[MAXN], loopn, loopcost[MAXN], loopsum;
bool flag[MAXN];
int treecnt[MAXN], treesum[MAXN], sum[MAXN];
int n;

void DFS0(int nowp, int pre)
{

    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre)
    {
        if(fa[iter->first] == 0)
        {
            fa[iter->first] = nowp;
            c[iter->first] = iter->second;
            DFS0(iter->first, nowp);
        }
        else
        {
            if(loopn > 0) return;
            for(int x = nowp; x != iter->first; x = fa[x])
                loop[loopn] = x, loopcost[loopn++] = c[x];
            loop[loopn] = iter->first, loopcost[loopn++] = iter->second;
            return;
        }
    }

    return;
}

void DFS1(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        DFS1(iter->first, nowp);
        treecnt[nowp] += treecnt[iter->first];
        treesum[nowp] += treecnt[iter->first] * iter->second;
    }

    return;
}

void DFS2(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        sum[iter->first] = sum[nowp] - treecnt[iter->first] * iter->second + (n - treecnt[iter->first]) * iter->second;
        DFS2(iter->first, nowp);
    }

    return;
}

int main()
{
    int loopintv, nowindex, temsum, intvcnt;

    // freopen("D:\\1.in", "r", stdin);
    // freopen("D:\\1.out", "w", stdout);

    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        int a, b, t;
        scanf("%d%d%d", &a, &b, &t);
        gra[a].push_back(make_pair(b, t));
        gra[b].push_back(make_pair(a, t));
    }

    // Find loop
    fa[1] = -1;
    DFS0(1, 0);
    for(int i = 0; i < loopn; i++) loopsum += loopcost[i];

    // DFS nodes except for the loop
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS1(loop[i], 0);

    // Calculate answers for loop nodes
    loopintv = nowindex = temsum = intvcnt = 0;
    for(nowindex = 0; nowindex < loopn; nowindex++)
    if((loopintv + loopcost[nowindex]) * 2 <= loopsum)
    {
        loopintv += loopcost[nowindex];
        temsum += treesum[loop[(nowindex + 1) % loopn]] + loopintv * treecnt[loop[(nowindex + 1) % loopn]];
        intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
    }
    else break;
    for(int i = nowindex, j = loopsum - loopintv; i < loopn; i++)
    {
        j -= loopcost[i];
        temsum += treesum[loop[(i + 1) % loopn]] + j * treecnt[loop[(i + 1) % loopn]];
    }
    for(int i = 0; i < loopn; i++)
    {
        sum[loop[i]] = temsum;
        temsum += (n - intvcnt) * loopcost[i] - intvcnt * loopcost[i];
        loopintv -= loopcost[i];
        intvcnt -= treecnt[loop[i]];
        while((loopintv + loopcost[nowindex]) * 2 <= loopsum)
        {
            loopintv += loopcost[nowindex];
            temsum += loopintv * treecnt[loop[(nowindex + 1) % loopn]] - (loopsum - loopintv) * treecnt[loop[(nowindex + 1) % loopn]];
            intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
            nowindex = (nowindex + 1) % loopn;
        }
    }

    // Calculate tree nodes
    memset(flag, 0, sizeof(flag));
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS2(loop[i], 0);

    // Output
    for(int i = 1; i <= n; i++) printf("%d ", sum[i]);

    return 0;
}


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

const int MAXN = 200005;
vector< pair<int, int> > gra[MAXN];
int fa[MAXN], c[MAXN];
int loop[MAXN], loopn, loopcost[MAXN], loopsum;
bool flag[MAXN];
int treecnt[MAXN], treesum[MAXN], sum[MAXN];
int n;

void DFS0(int nowp, int pre)
{

    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre)
    {
        if(fa[iter->first] == 0)
        {
            fa[iter->first] = nowp;
            c[iter->first] = iter->second;
            DFS0(iter->first, nowp);
        }
        else
        {
            if(loopn > 0) return;
            for(int x = nowp; x != iter->first; x = fa[x])
                loop[loopn] = x, loopcost[loopn++] = c[x];
            loop[loopn] = iter->first, loopcost[loopn++] = iter->second;
            return;
        }
    }

    return;
}

void DFS1(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        DFS1(iter->first, nowp);
        treecnt[nowp] += treecnt[iter->first];
        treesum[nowp] += treesum[iter->first] + treecnt[iter->first] * iter->second;
    }

    return;
}

void DFS2(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        sum[iter->first] = sum[nowp] - treecnt[iter->first] * iter->second + (n - treecnt[iter->first]) * iter->second;
        DFS2(iter->first, nowp);
    }

    return;
}

int main()
{
    int loopintv, nowindex, temsum, intvcnt;

    // freopen("D:\\1.in", "r", stdin);
    // freopen("D:\\1.out", "w", stdout);

    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        int a, b, t;
        scanf("%d%d%d", &a, &b, &t);
        gra[a].push_back(make_pair(b, t));
        gra[b].push_back(make_pair(a, t));
    }

    // Find loop
    fa[1] = -1;
    DFS0(1, 0);
    for(int i = 0; i < loopn; i++) loopsum += loopcost[i];

    // DFS nodes except for the loop
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS1(loop[i], 0);

    // Calculate answers for loop nodes
    loopintv = nowindex = temsum = intvcnt = 0;
    for(nowindex = 0; nowindex < loopn; nowindex++)
    if((loopintv + loopcost[nowindex]) * 2 <= loopsum)
    {
        loopintv += loopcost[nowindex];
        temsum += treesum[loop[(nowindex + 1) % loopn]] + loopintv * treecnt[loop[(nowindex + 1) % loopn]];
        intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
    }
    else break;
    for(int i = nowindex, j = loopsum - loopintv; i < loopn; i++)
    {
        j -= loopcost[i];
        temsum += treesum[loop[(i + 1) % loopn]] + j * treecnt[loop[(i + 1) % loopn]];
    }
    for(int i = 0; i < loopn; i++)
    {
        sum[loop[i]] = temsum;
        temsum += (n - intvcnt) * loopcost[i] - intvcnt * loopcost[i];
        loopintv -= loopcost[i];
        intvcnt -= treecnt[loop[(i + 1) % loopn]];
        while((loopintv + loopcost[nowindex]) * 2 <= loopsum)
        {
            loopintv += loopcost[nowindex];
            temsum += loopintv * treecnt[loop[(nowindex + 1) % loopn]] - (loopsum - loopintv) * treecnt[loop[(nowindex + 1) % loopn]];
            intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
            nowindex = (nowindex + 1) % loopn;
        }
    }

    // Calculate tree nodes
    memset(flag, 0, sizeof(flag));
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS2(loop[i], 0);

    // Output
    for(int i = 1; i <= n; i++) printf("%d ", sum[i]);

    return 0;
}


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

const int MAXN = 200005;
vector< pair<int, int> > gra[MAXN];
int fa[MAXN], c[MAXN];
int loop[MAXN], loopn, loopcost[MAXN], loopsum;
bool flag[MAXN];
int treecnt[MAXN];
long long treesum[MAXN], sum[MAXN];
int n;

void DFS0(int nowp, int pre)
{

    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre)
    {
        if(fa[iter->first] == 0)
        {
            fa[iter->first] = nowp;
            c[iter->first] = iter->second;
            DFS0(iter->first, nowp);
        }
        else
        {
            if(loopn > 0) return;
            for(int x = nowp; x != iter->first; x = fa[x])
                loop[loopn] = x, loopcost[loopn++] = c[x];
            loop[loopn] = iter->first, loopcost[loopn++] = iter->second;
            return;
        }
    }

    return;
}

void DFS1(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        DFS1(iter->first, nowp);
        treecnt[nowp] += treecnt[iter->first];
        treesum[nowp] += treesum[iter->first] + treecnt[iter->first] * iter->second;
    }

    return;
}

void DFS2(int nowp, int pre)
{
    treecnt[nowp] = 1;
    for(vector< pair<int, int> >::iterator iter = gra[nowp].begin(); iter != gra[nowp].end(); iter++)
    if(iter->first != pre && !flag[iter->first])
    {
        flag[iter->first] = true;
        sum[iter->first] = sum[nowp] - treecnt[iter->first] * iter->second + (n - treecnt[iter->first]) * iter->second;
        DFS2(iter->first, nowp);
    }

    return;
}

int main()
{
    int loopintv, nowindex, intvcnt;
    long long temsum;

    // freopen("D:\\1.in", "r", stdin);
    // freopen("D:\\1.out", "w", stdout);

    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        int a, b, t;
        scanf("%d%d%d", &a, &b, &t);
        gra[a].push_back(make_pair(b, t));
        gra[b].push_back(make_pair(a, t));
    }

    // Find loop
    fa[1] = -1;
    DFS0(1, 0);
    for(int i = 0; i < loopn; i++) loopsum += loopcost[i];

    // DFS nodes except for the loop
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS1(loop[i], 0);

    // Calculate answers for loop nodes
    loopintv = nowindex = temsum = intvcnt = 0;
    for(nowindex = 0; nowindex < loopn; nowindex++)
    if((loopintv + loopcost[nowindex]) * 2 <= loopsum)
    {
        loopintv += loopcost[nowindex];
        temsum += treesum[loop[(nowindex + 1) % loopn]] + (long long) loopintv * treecnt[loop[(nowindex + 1) % loopn]];
        intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
    }
    else break;
    for(int i = nowindex, j = loopsum - loopintv; i < loopn; i++)
    {
        j -= loopcost[i];
        temsum += treesum[loop[(i + 1) % loopn]] + (long long) j * treecnt[loop[(i + 1) % loopn]];
    }
    for(int i = 0; i < loopn; i++)
    {
        sum[loop[i]] = temsum;
        temsum += (n - intvcnt) * loopcost[i] - intvcnt * loopcost[i];
        loopintv -= loopcost[i];
        intvcnt -= treecnt[loop[(i + 1) % loopn]];
        while((loopintv + loopcost[nowindex]) * 2 <= loopsum)
        {
            loopintv += loopcost[nowindex];
            temsum += (long long) loopintv * treecnt[loop[(nowindex + 1) % loopn]] - (long long) (loopsum - loopintv) * treecnt[loop[(nowindex + 1) % loopn]];
            intvcnt += treecnt[loop[(nowindex + 1) % loopn]];
            nowindex = (nowindex + 1) % loopn;
        }
    }

    // Calculate tree nodes
    memset(flag, 0, sizeof(flag));
    for(int i = 0; i < loopn; i++) flag[loop[i]] = true;
    for(int i = 0; i < loopn; i++) DFS2(loop[i], 0);

    // Output
    for(int i = 1; i <= n; i++) printf("%I64d ", sum[i]);

    return 0;
}