#include <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

typedef struct {
    vector <int> next;
    vector <int> t;
    bool onCycle;
    int size;
} planet;

typedef struct {
    vector <int> child;
    vector <int> t;
    vector <int> size;
    vector <long long> weight;
    long long total_weight;
    int total_size;
    int dp;
} cycle_planet;

int n, m;

planet p[300000];
bool found[300000];
int prev[300000];
int prev2[300000];
long long ans[300000];
bool use[300000];
int cycle_total = 0;
long long tree_total = 0;
map <pair <int, int>, int>  weight;

bool made_cycle = false;
vector <int> cycle;
cycle_planet cp[300000];

int dist[300000]; /* least distance from i to a point on the cycle */

int cycle_dist (int i, int j)
{
    if (i == j) return 0;
    if (j < i) return cycle_dist (j, i);
    return min(cp[j].dp - cp[i].dp, cycle_total - (cp[j].dp - cp[i].dp));
}

int right_dist (int i, int j)
{
    if (i == j) return 0;
    int d = cp[j].dp - cp[i].dp + cycle_total;
    return d % cycle_total;
}

int left_dist (int i, int j)
{
    if (i == j) return 0;
    int d = cp[i].dp - cp[j].dp + cycle_total;
    return d % cycle_total;
}

void compute(int v, int parent)
{
    ans[v] = ans[parent] + weight[make_pair(v, parent)] * (n - 2 * (p[v].size));
    // printf("ans %d: %d\n", v, ans[v]);
    for (int i = 0; i < p[v].next.size(); i++)
    {
        int w = p[v].next[i];
        if (w == parent) continue;
        compute(w, v);
    }
}

void get_cycle(int v, int before)
{
    if (made_cycle) return;
    if (!found[v])
    {
        found[v] = true;
        for (int i = 0; i < p[v].next.size(); i++)
        {
            int w = p[v].next[i];
            if (w == before) continue;
            prev[w] = v;
            get_cycle(w, v);
        }
    }
    else
    {
        p[v].onCycle = true;
        cycle.push_back(v);

        int a = prev[v];
        while (a != v)
        {
            cycle.push_back(a);
            p[a].onCycle = true;
            a = prev[a];
        }
        made_cycle = true;
    }
}

int tree_size(int v, int parent)
{
    int sum = 1;
    for (int i = 0; i < p[v].next.size(); i++)
    {
        int w = p[v].next[i];
        if (w == parent) continue;
        sum += tree_size(w, v);
    }
    p[v].size = sum;
    return sum;
}

long long tree_weight(int v, int parent)
{
    long long sum = dist[v];
    for (int i = 0; i < p[v].next.size(); i++)
    {
        int w = p[v].next[i];
        if (w == parent) continue;
        sum += tree_weight(w, v);
    }
    return sum;
}

void Dijkstra (int v)
{
    dist[v] = 0;
    priority_queue < pair <int, int> > pq;
    pq.push(make_pair(-0, v));
    while (!pq.empty())
    {
        pair <int, int> next = pq.top();
        int w = next.second;
        pq.pop();

        if (use[w]) continue;
        use[w] = true;
        for (int i = 0; i < p[w].next.size(); i++)
        {
            int u = p[w].next[i];
            if (p[u].onCycle || use[u]) continue;
            dist[u] = min(dist[u], dist[w] + p[w].t[i]);
            pq.push(make_pair(-dist[u], u));
        }
    }
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int a, b, t;
        scanf("%d %d %d", &a, &b, &t);
        p[a].next.push_back(b);
        p[b].next.push_back(a);
        p[a].t.push_back(t);
        p[b].t.push_back(t);
        weight[make_pair(a, b)] = t;
        weight[make_pair(b, a)] = t;
        // printf("%d %d %d\n", a, b, weight[make_pair(b, a)]);

        found[i] = false;
        p[i].onCycle = false;
        prev[i] = -1;
        prev2[i] = -1;
        dist[i] = (int) 1e9;
        use[i] = false;
        ans[i] = 0;
    }
    get_cycle(1, -1);
    m = cycle.size();
    // printf("cycle:\n");
    int curdp = 0;
    for (int i = 0; i < cycle.size(); i++)
    {
        // printf(" %d\n", cycle[i]);
        Dijkstra(cycle[i]);
        if (i != 0)
        {
            curdp += weight[make_pair(cycle[i], cycle[i - 1])];
            // printf("%d %d %d\n", cycle[i], cycle[i - 1], weight[make_pair(cycle[i], cycle[i - 1])]);
        }
        cp[i].total_size = 1;
        cp[i].total_weight = 0;
        cp[i].dp = curdp;
        for (int j = 0; j < p[cycle[i]].next.size(); j++)
        {
            if (p[p[cycle[i]].next[j]].onCycle)
            {
                continue;
            }
            cp[i].child.push_back(p[cycle[i]].next[j]);
            cp[i].t.push_back(p[cycle[i]].t[j]);
            cp[i].size.push_back(tree_size(p[cycle[i]].next[j], cycle[i]));
            cp[i].weight.push_back(tree_weight(p[cycle[i]].next[j], cycle[i]));
            cp[i].total_size += cp[i].size[cp[i].size.size() - 1];
            cp[i].total_weight += cp[i].weight[cp[i].weight.size() - 1];
        }
        // printf("size: %d\n", cp[i].total_size);
        // printf("total_weight: %I64d\n", cp[i].total_weight);
        // printf("dp: %d\n", cp[i].dp);
        tree_total += cp[i].total_weight;
    }
    cycle_total = curdp + weight[make_pair(cycle[0], *(cycle.end() - 1))];
    // printf("\n");
    // printf("distance\n");
    /* for (int i = 1; i <= n; i++)
    {
        printf("%d %d\n", i, dist[i]);
    } */
    // printf("cycle total: %d\n", cycle_total);
    // printf("tree total: %I64d\n", tree_total);
    /* for (int i = 0; i < cycle.size(); i++)
    {
        for (int j = 0; j < cycle.size(); j++)
        {
            printf("dist %d %d: %d\n", cycle[i], cycle[j], cycle_dist(i, j));
        }
    } */
    int left = 0;
    int right = 0;
    int index = 0;
    ans[cycle[0]] = tree_total;
    for (int i = 0; i < cycle.size(); i++)
    {
        if (i == 0)
        {
            left += cp[i].total_size;
        }
        else if (right_dist(0, i) < left_dist(0, i))
        {
            right += cp[i].total_size;
            index = i;
        }
        else
        {
            left += cp[i].total_size;
        }
        ans[cycle[0]] += cp[i].total_size * cycle_dist(0, i);
    }
    // printf("ans %d: %I64d\n", cycle[0], ans[cycle[0]]);
    // printf("%d %d %d\n", left, right, index);
    for (int i = 1; i < m; i++)
    {
        if (index == i - 1)
        {
            int swap = right;
            right = left;
            left = swap;
            ans[cycle[i]] = tree_total;
            for (int j = 0; j < m; j++)
            {
                ans[cycle[i]] += cp[j].total_size * cycle_dist(i, j);
            }
        }
        else
        {
            int t = weight[make_pair(cycle[i - 1], cycle[i])];
            ans[cycle[i]] = ans[cycle[i - 1]] + (left - right) * t;
            while (right_dist(i, (index + 1) % m) < left_dist(i, (index + 1) % m))
            {
                index = (index + 1) % m;
                right += cp[index].total_size;
                left  -= cp[index].total_size;
                ans[cycle[i]] -= (left_dist(i, index) - right_dist(i, index)) * cp[index].total_size;
            }
        }
        right -= cp[i].total_size;
        left += cp[i].total_size;
        // printf("ans %d: %I64d\n", cycle[i], ans[cycle[i]]);
        // printf("%d %d %d\n", left, right, index);
    }
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < cp[i].child.size(); j++)
        {
            compute(cp[i].child[j], cycle[i]);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        printf("%d ", ans[i]);
    }
    fflush(stdin);
    getchar();
}


#include <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

typedef struct {
    vector <int> next;
    vector <int> t;
    bool onCycle;
    int size;
} planet;

typedef struct {
    vector <int> child;
    vector <int> t;
    vector <int> size;
    vector <long long> weight;
    long long total_weight;
    int total_size;
    int dp;
} cycle_planet;

int n, m;

planet p[300000];
bool found[300000];
int prev[300000];
int prev2[300000];
long long ans[300000];
bool use[300000];
int cycle_total = 0;
long long tree_total = 0;
map <pair <int, int>, int>  weight;

bool made_cycle = false;
vector <int> cycle;
cycle_planet cp[300000];

int dist[300000]; /* least distance from i to a point on the cycle */

long long cycle_dist (int i, int j)
{
    if (i == j) return 0;
    if (j < i) return cycle_dist (j, i);
    return min(cp[j].dp - cp[i].dp, cycle_total - (cp[j].dp - cp[i].dp));
}

long long right_dist (int i, int j)
{
    if (i == j) return 0;
    int d = cp[j].dp - cp[i].dp + cycle_total;
    return d % cycle_total;
}

long long left_dist (int i, int j)
{
    if (i == j) return 0;
    int d = cp[i].dp - cp[j].dp + cycle_total;
    return d % cycle_total;
}

void compute(int v, int parent)
{
    ans[v] = ans[parent] + weight[make_pair(v, parent)] * (n - 2 * (p[v].size));
    // printf("ans %d: %d\n", v, ans[v]);
    for (int i = 0; i < p[v].next.size(); i++)
    {
        int w = p[v].next[i];
        if (w == parent) continue;
        compute(w, v);
    }
}

void get_cycle(int v, int before)
{
    if (made_cycle) return;
    if (!found[v])
    {
        found[v] = true;
        for (int i = 0; i < p[v].next.size(); i++)
        {
            int w = p[v].next[i];
            if (w == before) continue;
            prev[w] = v;
            get_cycle(w, v);
        }
    }
    else
    {
        p[v].onCycle = true;
        cycle.push_back(v);

        int a = prev[v];
        while (a != v)
        {
            cycle.push_back(a);
            p[a].onCycle = true;
            a = prev[a];
        }
        made_cycle = true;
    }
}

int tree_size(int v, int parent)
{
    int sum = 1;
    for (int i = 0; i < p[v].next.size(); i++)
    {
        int w = p[v].next[i];
        if (w == parent) continue;
        sum += tree_size(w, v);
    }
    p[v].size = sum;
    return sum;
}

long long tree_weight(int v, int parent)
{
    long long sum = dist[v];
    for (int i = 0; i < p[v].next.size(); i++)
    {
        int w = p[v].next[i];
        if (w == parent) continue;
        sum += tree_weight(w, v);
    }
    return sum;
}

void Dijkstra (int v)
{
    dist[v] = 0;
    priority_queue < pair <int, int> > pq;
    pq.push(make_pair(-0, v));
    while (!pq.empty())
    {
        pair <int, int> next = pq.top();
        int w = next.second;
        pq.pop();

        if (use[w]) continue;
        use[w] = true;
        for (int i = 0; i < p[w].next.size(); i++)
        {
            int u = p[w].next[i];
            if (p[u].onCycle || use[u]) continue;
            dist[u] = min(dist[u], dist[w] + p[w].t[i]);
            pq.push(make_pair(-dist[u], u));
        }
    }
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int a, b, t;
        scanf("%d %d %d", &a, &b, &t);
        p[a].next.push_back(b);
        p[b].next.push_back(a);
        p[a].t.push_back(t);
        p[b].t.push_back(t);
        weight[make_pair(a, b)] = t;
        weight[make_pair(b, a)] = t;
        // printf("%d %d %d\n", a, b, weight[make_pair(b, a)]);

        found[i] = false;
        p[i].onCycle = false;
        prev[i] = -1;
        prev2[i] = -1;
        dist[i] = (int) 1e9;
        use[i] = false;
        ans[i] = 0;
    }
    get_cycle(1, -1);
    m = cycle.size();
    // printf("cycle:\n");
    int curdp = 0;
    for (int i = 0; i < cycle.size(); i++)
    {
        // printf(" %d\n", cycle[i]);
        Dijkstra(cycle[i]);
        if (i != 0)
        {
            curdp += weight[make_pair(cycle[i], cycle[i - 1])];
            // printf("%d %d %d\n", cycle[i], cycle[i - 1], weight[make_pair(cycle[i], cycle[i - 1])]);
        }
        cp[i].total_size = 1;
        cp[i].total_weight = 0;
        cp[i].dp = curdp;
        for (int j = 0; j < p[cycle[i]].next.size(); j++)
        {
            if (p[p[cycle[i]].next[j]].onCycle)
            {
                continue;
            }
            cp[i].child.push_back(p[cycle[i]].next[j]);
            cp[i].t.push_back(p[cycle[i]].t[j]);
            cp[i].size.push_back(tree_size(p[cycle[i]].next[j], cycle[i]));
            cp[i].weight.push_back(tree_weight(p[cycle[i]].next[j], cycle[i]));
            cp[i].total_size += cp[i].size[cp[i].size.size() - 1];
            cp[i].total_weight += cp[i].weight[cp[i].weight.size() - 1];
        }
        // printf("size: %d\n", cp[i].total_size);
        // printf("total_weight: %I64d\n", cp[i].total_weight);
        // printf("dp: %d\n", cp[i].dp);
        tree_total += cp[i].total_weight;
    }
    cycle_total = curdp + weight[make_pair(cycle[0], *(cycle.end() - 1))];
    // printf("\n");
    // printf("distance\n");
    /* for (int i = 1; i <= n; i++)
    {
        printf("%d %d\n", i, dist[i]);
    } */
    // printf("cycle total: %d\n", cycle_total);
    // printf("tree total: %I64d\n", tree_total);
    /* for (int i = 0; i < cycle.size(); i++)
    {
        for (int j = 0; j < cycle.size(); j++)
        {
            printf("dist %d %d: %d\n", cycle[i], cycle[j], cycle_dist(i, j));
        }
    } */
    int left = 0;
    int right = 0;
    int index = 0;
    ans[cycle[0]] = tree_total;
    for (int i = 0; i < cycle.size(); i++)
    {
        if (i == 0)
        {
            left += cp[i].total_size;
        }
        else if (right_dist(0, i) < left_dist(0, i))
        {
            right += cp[i].total_size;
            index = i;
        }
        else
        {
            left += cp[i].total_size;
        }
        ans[cycle[0]] += cp[i].total_size * cycle_dist(0, i);
    }
    // printf("ans %d: %I64d\n", cycle[0], ans[cycle[0]]);
    // printf("%d %d %d\n", left, right, index);
    for (int i = 1; i < m; i++)
    {
        if (index == i - 1)
        {
            int swap = right;
            right = left;
            left = swap;
            ans[cycle[i]] = tree_total;
            for (int j = 0; j < m; j++)
            {
                ans[cycle[i]] += cp[j].total_size * cycle_dist(i, j);
            }
        }
        else
        {
            int t = weight[make_pair(cycle[i - 1], cycle[i])];
            ans[cycle[i]] = ans[cycle[i - 1]] + (left - right) * t;
            while (right_dist(i, (index + 1) % m) < left_dist(i, (index + 1) % m))
            {
                index = (index + 1) % m;
                right += cp[index].total_size;
                left  -= cp[index].total_size;
                ans[cycle[i]] -= (left_dist(i, index) - right_dist(i, index)) * cp[index].total_size;
            }
        }
        right -= cp[i].total_size;
        left += cp[i].total_size;
        // printf("ans %d: %I64d\n", cycle[i], ans[cycle[i]]);
        // printf("%d %d %d\n", left, right, index);
    }
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < cp[i].child.size(); j++)
        {
            compute(cp[i].child[j], cycle[i]);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        printf("%I64d ", ans[i]);
    }
    fflush(stdin);
    getchar();
}


#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 <cstdio>
#include <iostream>
#include <cmath>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <complex>
#include <algorithm>
#include <functional>
#include <fstream>
#include <numeric>
#include <string>
#include <valarray>
#include <deque>
#define int long long

using namespace std;

typedef pair<int,int> Pair;

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

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

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

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

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

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

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

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

vector<int> d,zarib;

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

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

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

const int N4=400;

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

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

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

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

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

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

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

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

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

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


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

using namespace std;

typedef pair<int,int> Pair;

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

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

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

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

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

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

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

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

vector<int> d,zarib;

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

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

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

const int N4=500;

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

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

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

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

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

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

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

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

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

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


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

using namespace std;

typedef pair<int,int> Pair;

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

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

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

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

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

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

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

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

vector<int> d,zarib;

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

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

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

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

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

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

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

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

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

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

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

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

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


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

using namespace std;

typedef pair<int,int> Pair;

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

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

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

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

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

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

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

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

vector<int> d,zarib;

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

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

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

const int N4=400;

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

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

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

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

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

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

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

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

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

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


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

using namespace std;

typedef pair<int,int> Pair;

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

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

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

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

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

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

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

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

vector<int> d,zarib;

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

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

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

struct S2{
  vector<int> A,B;
  S2(vector<int> a,vector<int> b){
    A=a;
    B=b;
  }
  int get(int a,int b){
    int ad=0,pointer=a,sum=0;
    while (true){
      sum+=ad*B[pointer];
      ad+=A[pointer];
      if (pointer == b)
	break;
      pointer=(pointer+1)%A.size();
    }
    return(sum);
  }
};

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

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

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

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

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

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

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


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

using namespace std;

typedef pair<int,int> Pair;

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

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

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

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

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

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

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

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

vector<int> d,zarib;

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

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

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

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

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

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

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

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

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

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

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

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

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


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

using namespace std;

typedef pair<int,int> Pair;

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

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

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

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

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

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

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

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

deque<int> d,zarib;

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

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

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

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

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

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

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


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

using namespace std;

typedef pair<int,int> Pair;

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

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

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

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

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

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

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

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

vector<int> d,zarib;

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

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

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

struct S2{
  vector<int> A,B;
  S2(vector<int> a,vector<int> b){
    A=a;
    B=b;
  }
  int get(int a,int b){
    int ad=0,pointer=a,sum=0;
    while (true){
      sum+=ad*B[pointer];
      ad+=A[pointer];
      if (pointer == b)
	break;
      pointer=(pointer+1)%A.size();
    }
    return(sum);
  }
};

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

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

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

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

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

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

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


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

using namespace std;

typedef pair<int,int> Pair;

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

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

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

typedef tuple<int,int,int> triple;

const int MAX_N=200000+20;

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

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

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

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

deque<int> d,zarib;

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

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

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

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

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

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

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

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


#include <iostream>
using namespace std;
int main()
{
  int a,n,m,s1,j=0;
  cin>>a;
int ar[a];
  for (int i=0;i<a;++i)
  {
    cin>>n>>m;
    int arr[n];
    s1=0;
    for (int j=0;j<n;++j)
    {
    cin>>arr[i];
    s1+=arr[i];

    }
    if (s1>1000 || m>100000){ar[j]=0;}
    else {
      if (s1>=m){ar[j]=m;}
    else ar[j]=s1;
    j++;
    }
  }
  for (int i=0;i<a;++i)
  {
    cout<<ar[i]<<endl;
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    for(int tt = 0; tt < t; tt++)
    {
        int n, m, s = 0;
        cin >> n >> m;
        int a[n];
        for(int i = 0; i < n; i++)
        {cin >> a[i];
        s += a[i];}
        if(s >= m) cout << m << endl;
        else cout << s << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    for(int t;t = 0; tt < t; tt++)
    {
        int n, m, s = 0;
        cin >> n >> m;
        int a[n];
        for(int i = 0; i < n; i++)
        {cin >> a[i];
        s += a[i];}
        if(s >= m) cout << m << endl;
        else cout << s << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    for(int tt = 0; tt < t; tt++)
    {
        int n, m, s = 0;
        cin >> n >> m;
        int a[n];
        for(int i = 0; i < n; i++)
        {cin >> a[i];
        s += a[i];}
        if(s >= m) cout << m;
        else cout << s;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t
    for(int tt = 0; tt < t; tt++)
    {
        int n, m, s = 0;
        cin >> n >> m;
        int a[n];
        for(int i = 0; i < n; i++)
        {cin >> a[i];
        s += a[i];}
        if(s >= m) cout << m;
        else cout << s
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    for(int tt = 0; tt < t; tt++)
    {
        int n, m, s = 0;
        cin >> n >> m;
        int a[n];
        for(int i = 0; i < n; i++)
        {cin >> a[i]
        s += a[i];}
        if(s >= m) cout << m;
        else cout << s
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t
    for(int tt = 0; tt < t; tt++)
    {
        int n, m, s = 0;
        cin >> n >> m;
        int a[n];
        for(int i = 0; i < n; i++)
        {cin >> a[i]
        s += a[i];}
        if(s >= m) cout << m;
        else cout << s
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t
    for(int t;t = 0; tt < t; tt++)
    {
        int n, m, s = 0;
        cin >> n >> m;
        int a[n];
        for(int i = 0; i < n; i++)
        {cin >> a[i];
        s += a[i];}
        if(s >= m) cout << m << endl;
        else cout << s << endl;
    }
    return 0;
}


    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define d double
    ll t, n,a[101001], m, s;
    double mid;
    int main(){
        cin >> t;
        while(t--){
            cin >> n >> m;
            for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];}
            mid /= n;
            d ans = -9999999999;

            for(d i = m;i >= 0;i--){
                for(d j = 0;j <= (n - 1) * m;j++){
                    double x = (i + j) / n;
                    if(x == mid){
                        ans = max(ans, i);
                        break;
                    }
                }
            }

            cout << ans << endl;
            mid = 0;
            s = 0;

        }
    }


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll t, n,a[101001], m, s;
double mid;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];s += a[i];}
        mid /= n;
        ll ans;

        for(ll i = m;i > 0;i--){
            if(i / n <= mid){ans = i;break;}
        }

        cout << ans << endl;
        mid = 0;
        s = 0;

    }
}


    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define d double
    ll t, n,a[101001], m, s;
    double mid;
    int main(){
        cin >> t;
        while(t--){
            cin >> n >> m;
            for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];}
            mid /= n;
            d ans = -9999999999;

            for(d i = m;i >= 0;i--){
                if(mid * n - i <= (n - 1) * m){
                    ans = i;
                    break;
                }
            }

            cout << ans << endl;
            mid = 0;
            s = 0;

        }
    }


    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define d double
    ll t, n,a[101001], m, s;
    double mid;
    int main(){
        cin >> t;
        while(t--){
            cin >> n >> m;
            for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];s += a[i];}
            mid /= n;
            ll ans = -9999999999;

            for(ll i = m;i > 0;i--){
                for(ll j = 0;j < (n - 1) * m;j++){
                    double x = (i + j) / n;
                    if(x == mid){
                        ans = max(ans, i);
                        break;
                    }
                }
            }

            cout << ans << endl;
            mid = 0;
            s = 0;

        }
    }


        #include <bits/stdc++.h>
        using namespace std;
        #define ll long long
        #define d double
        ll t;
        double mid;
        int main(){
            cin >> t;
            while(t--){
                ll n,m;
                cin >> n >> m;
                ll a[n + 2];
                double mid = 0;
                for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];}
                mid /= n;
                double ans = -9999999999;

                for(double i = m;i >= 0;i--){
                    double j = m;
                    while(j / n != mid){
                        j++;
                    }

                    if(j - i <= (n - 1) * m)ans = max(ans, i);
                }

                cout << ans << endl;
            }
        }


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define d double
d t, n,a[101001], m, s;
double mid;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];s += a[i];}
        mid /= n;
        ll ans = -1;

        for(d i = m;i > 0;i--){
            for(d j = 0;j < (n - 1) * m;j++){
                if((i + j) / n == mid){
                    ll x = i;
                    ans = max(ans, x);
                }
            }
        }

        cout << ans << endl;
        mid = 0;
        s = 0;

    }
}


    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define d double
    ll t, n,a[101001], m, s;
    double mid;
    int main(){
        cin >> t;
        while(t--){
            cin >> n >> m;
            for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];}
            mid /= n;
            ll ans = -9999999999;

            for(ll i = m;i >= 0;i--){
                if(mid * n - i <= (n - 1) * m){
                    ans = max(ans,i);
                }
            }

            cout << ans << endl;
            mid = 0;
            s = 0;

        }
    }


    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define d double
    ll t, n,a[101001], m, s;
    double mid;
    int main(){
        cin >> t;
        while(t--){
            cin >> n >> m;
            for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];}
            mid /= n;
            d ans = -9999999999;

            for(d i = m;i >= 0;i--){
                if(mid * n - i <= (n - 1) * m){
                    ans = max(ans, i);
                }
            }

            cout << ans << endl;
            mid = 0;
            s = 0;

        }
    }


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll t;

int main(){
    cin >> t;
    while(t--){
        double n,m, mid = 0;
        cin >> n >> m;
        for(ll i = 1;i <= n;i++){
            ll x;
            cin >> x;
            mid += x;
        }
        //mid /= n;

        double ans = -99999999;

        for(double i = m;i >= 0;i--){
          if(mid >= i){ans = max(ans, i);break;}
        }

        cout << ans << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define d double
d t, n,a[101001], m, s;
double mid;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];s += a[i];}
        mid /= n;
        ll ans = -1;

        for(d i = m;i > 0;i--){
            if(mid * n - i <= m * (n - 1)){
                ans = i;
                break;
            }
        }

        cout << ans << endl;
        mid = 0;
        s = 0;

    }
}


        #include <bits/stdc++.h>
        using namespace std;
        #define ll long long
        #define d double
        ll t;
        double mid;
        int main(){
            cin >> t;
            while(t--){
                ll n,m;
                cin >> n >> m;
                ll a[n + 2];
                double mid = 0;
                for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];}
                mid /= n;
                double ans = -9999999999;

                for(double i = m;i >= 0;i--){
                    for(double j = 1;;j++){
                        if(mid * j >= i && mid * j - i <= m * (n - 1)){
                            ans = max(ans, i);
                            break;
                        }
                    }
                }

                cout << ans << endl;
            }
        }


    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define d double
    ll t, n,a[101001], m, s;
    double mid;
    int main(){
        cin >> t;
        while(t--){
            cin >> n >> m;
            for(ll i = 1;i <= n;i++){cin >> a[i];s += a[i];}
            //mid /= n;
            ll ans = -9999999999;

            for(ll i = m;i >= 0;i--){
                if(s - i <= (n - 1) * m){
                    ans = max(ans,i);
                }
            }

            cout << ans << endl;
            mid = 0;
            s = 0;

        }
    }


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll t, n,a[101001], m, s;
double mid;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];s += a[i];}
        mid /= n;
        ll ans = -1;

        for(ll i = m;i > 0;i--){
            if(mid * n - i <= m * (n - 1)){
                ans = i;
                break;
            }
        }

        cout << ans << endl;
        mid = 0;
        s = 0;

    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define d double
d t, n,a[101001], m, s;
double mid;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];s += a[i];}
        mid /= n;
        d ans = -1;

        for(d i = m;i > 0;i--){
            for(d j = 0;j < (n - 1) * m;j++){
                if((i + j) / n == mid){
                    ans = max(ans, i);
                }
            }
        }

        cout << ans << endl;
        mid = 0;
        s = 0;

    }
}


    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define d double
    ll t, n,a[101001], m, s;
    double mid;
    int main(){
        cin >> t;
        while(t--){
            cin >> n >> m;
            for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];}
            mid /= n;
            d ans = -9999999999;

            for(d i = m;i >= 0;i--){
                ll x = mid * n - i;
                if(mid * n - i <= (n - 1) * m && x % 1 == 0){
                    ans = i;
                    break;
                }
            }

            cout << ans << endl;
            mid = 0;
            s = 0;

        }
    }


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll t, n,a[101001], m, s;
double mid;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(ll i = 1;i <= n;i++){cin >> a[i];mid += a[i];s += a[i];}
        mid /= n;
        ll ans;

        for(ll i = m;i > 0;i--){
            double x = (i + (n - 1) * m) / n;
            if(x >= mid){ans = i;break;}
        }

        cout << ans << endl;
        mid = 0;
        s = 0;

    }
}


#include <bits/stdc++.h>
#include <time.h>
#define int int64_t
//template<typename T,typename T2>
//T Sum(T a ,T2 b)
//{
//    cout<<a*b;
//}
using namespace std;
const int N=1e3+10;
const int K=1e-6;
const int I=1e18+10;

int a[N];

int32_t main()
{
//    ios_base::sync_with_stdio(false);
//    cin.tie(0); cout.tie(0);
    srand(time(0));
    setlocale(LC_ALL, "ru");//setlocale(0, "");

    double k; cin>>k;
    double sa=0;
    for(int i=0;i<k;i++){
        double n,m; cin>>n>>m;
        for(int i=0;i<n;i++){
            cin>>a[i];
            sa+=a[i];
        }
        double sra=sa/n;
        if((m/n==sra)||(m/n<sra)) cout<<m<<"-----"<<endl;
        else if(m/n>sra){
            for(int j=1;j<=m;j++){
                if((m-j)/n==sra){
                    if(m-j<0) cout<<0<<"-----"<<endl;
                    else cout<<m-j<<"-----"<<endl;
                    j=m+1;
                }
            }
        }
    }
}


#include <bits/stdc++.h>
#include <time.h>
#define int int64_t
//template<typename T,typename T2>
//T Sum(T a ,T2 b)
//{
//    cout<<a*b;
//}
using namespace std;
const int N=1e3+10;
const int K=1e-6;
const int I=1e18+10;

int32_t main()
{
//    ios_base::sync_with_stdio(false);
//    cin.tie(0); cout.tie(0);
    srand(time(0));
    setlocale(LC_ALL, "ru");//setlocale(0, "");

    int q; cin>>q;
    while(q--){
        int n,m; cin>>n>>m;
        vector<int> v(n);
        int sum=0;
        for(int i=1;i<n;i++) sum+=v[i];
        cout<<min(v[0]+sum,m)<<endl;
    }
}


#include <bits/stdc++.h>
#include <time.h>
#define int int64_t
//template<typename T,typename T2>
//T Sum(T a ,T2 b)
//{
//    cout<<a*b;
//}
using namespace std;
const int N=1e3+10;
const int K=1e-6;
const int I=1e18+10;

int a[N];

int32_t main()
{
//    ios_base::sync_with_stdio(false);
//    cin.tie(0); cout.tie(0);
    srand(time(0));
    setlocale(LC_ALL, "ru");//setlocale(0, "");

    double k; cin>>k;
    double sa=0;
    for(int i=0;i<k;i++){
        double n,m; cin>>n>>m;
        for(int i=0;i<n;i++){
            cin>>a[i];
            sa+=a[i];
        }
        double sra=sa/n;
        if((m/n==sra)||(m/n<sra)) cout<<m<<endl;
        else if(m/n>sra){
            for(int j=1;j<=m;j++){
                if((m-j)/n==sra) j=m+1; cout<<m-j<<endl;
            }
        }
    }
}


#include <bits/stdc++.h>
#include <time.h>
#define int int64_t
//template<typename T,typename T2>
//T Sum(T a ,T2 b)
//{
//    cout<<a*b;
//}
using namespace std;
const int N=1e3+10;
const int K=1e-6;
const int I=1e18+10;

int a[N];

int32_t main()
{
//    ios_base::sync_with_stdio(false);
//    cin.tie(0); cout.tie(0);
    srand(time(0));
    setlocale(LC_ALL, "ru");//setlocale(0, "");

    double k; cin>>k;
    double sa=0;
    for(int i=0;i<k;i++){
        double n,m; cin>>n>>m;
        for(int i=0;i<n;i++){
            cin>>a[i];
            sa+=a[i];
        }
        double sra=sa/n;
        if((m/n==sra)||(m/n<sra)) cout<<m<<endl;
        else if(m/n>sra){
            for(int j=1;j<=m;j++){
                if((m-j)/n==sra){
                    if(m-j<0) cout<<0<<endl;
                    else cout<<m-j<<endl;
                    j=m+1;
                }
            }
        }
    }
}


#include <bits/stdc++.h>
#include <time.h>
#define int int64_t
//template<typename T,typename T2>
//T Sum(T a ,T2 b)
//{
//    cout<<a*b;
//}
using namespace std;
const int N=1e3+10;
const int K=1e-6;
const int I=1e18+10;

int a[N];

int32_t main()
{
//    ios_base::sync_with_stdio(false);
//    cin.tie(0); cout.tie(0);
    srand(time(0));
    setlocale(LC_ALL, "ru");//setlocale(0, "");

    int q; cin>>q;
    while(q--){
        int n,m; cin>>n>>m;
        vector<int> v(n);
        int sum=0;
        for(int i=1;i<n;i++) sum+=v[i];
        cout<<min(v[i]+sum,m)<<endl;
    }
}


#include <bits/stdc++.h>
#include <time.h>
#define int int64_t
//template<typename T,typename T2>
//T Sum(T a ,T2 b)
//{
//    cout<<a*b;
//}
using namespace std;
const int N=1e3+10;
const int K=1e-6;
const int I=1e18+10;

int32_t main()
{
//    ios_base::sync_with_stdio(false);
//    cin.tie(0); cout.tie(0);
    srand(time(0));
    setlocale(LC_ALL, "ru");//setlocale(0, "");

    int q; cin>>q;
    while(q--){
        int n,m; cin>>n>>m;
        vector<int> v(n);
        int sum=0;
        for(int i=0;i<n;i++) cin>>v[i];
        for(int i=1;i<n;i++) sum+=v[i];
        cout<<min(v[0]+sum,m)<<endl;
    }
}


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

#define ll long long
#define pb push_back
#define vb vector<bool>
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define MOD 1000000007
#define MAX_TIME 1e9
#define nl "\n"
#define sp " "
#define gcd __gcd
#define pf printf
#define sf scanf
#define se(a) a.begin(), a.end()
#define fill(a,n) for(int i = 0; i < n; i++) cin>>a[i]

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        vi a(n);
        fill(a,n);
        int my_score = a[0];
        ll restscore = 0;
        for(int i = 1; i < n; i++)restscore+= a[i];
        // accumulate(a.begin()+1,a.end(),restscore);
        int change = m - my_score;
        // cout<<restscore<<nl;
        if(restscore-change >= 0 && restscore > 0){
            cout<<min(my_score + (int)(restscore),m)<<nl;
        }
        else cout<<my_score<<nl;
    }
}


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

#define ll long long
#define pb push_back
#define vb vector<bool>
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define MOD 1000000007
#define MAX_TIME 1e9
#define nl "\n"
#define sp " "
#define gcd __gcd
#define pf printf
#define sf scanf
#define se(a) a.begin(), a.end()
#define fill(a,n) for(int i = 0; i < n; i++) cin>>a[i]

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        vi a(n);
        fill(a,n);
        int my_score = a[0];
        ll restscore = 0;
        for(int i = 1; i < n; i++)restscore+= a[i];
        // accumulate(a.begin()+1,a.end(),restscore);
        int change = m - my_score;
        // cout<<restscore<<nl;
        if(restscore < m){
            cout<<my_score+ restscore<<nl;
        }
        else {
            cout<<m<<nl;
        }
    }
}


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

#define ll long long
#define pb push_back
#define vb vector<bool>
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define MOD 1000000007
#define MAX_TIME 1e9
#define nl "\n"
#define sp " "
#define gcd __gcd
#define pf printf
#define sf scanf
#define se(a) a.begin(), a.end()
#define fill(a,n) for(int i = 0; i < n; i++) cin>>a[i]

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        vi a(n);
        fill(a,n);
        int my_score = a[0];
        ll restscore = 0;
        for(int i = 1; i < n; i++)restscore+= a[i];
        // accumulate(a.begin()+1,a.end(),restscore);
        int change = m - my_score;
        // cout<<restscore<<nl;
        if(restscore <= m){
            cout<<min(my_score+ restscore,(ll)m)<<nl;
        }
        else {
            cout<<m<<nl;
        }
    }
}


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

#define ll long long
#define pb push_back
#define vb vector<bool>
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define MOD 1000000007
#define MAX_TIME 1e9
#define nl "\n"
#define sp " "
#define gcd __gcd
#define pf printf
#define sf scanf
#define se(a) a.begin(), a.end()
#define fill(a,n) for(int i = 0; i < n; i++) cin>>a[i]

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        vi a(n);
        fill(a,n);
        int my_score = a[0];
        ll restscore = 0;
        for(int i = 1; i < n; i++)restscore+= a[i];
        // accumulate(a.begin()+1,a.end(),restscore);
        int change = m - my_score;
        // cout<<restscore<<nl;
        if(restscore <= m){
            cout<<my_score+ restscore<<nl;
        }
        else {
            cout<<m<<nl;
        }
    }
}


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

#define ll long long
#define pb push_back
#define vb vector<bool>
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define MOD 1000000007
#define MAX_TIME 1e9
#define nl "\n"
#define sp " "
#define gcd __gcd
#define pf printf
#define sf scanf
#define se(a) a.begin(), a.end()
#define fill(a,n) for(int i = 0; i < n; i++) cin>>a[i]

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        vi a(n);
        fill(a,n);
        int my_score = a[0];
        ll restscore = 0;
        for(int i = 1; i < n; i++)restscore+= a[i];
        // accumulate(a.begin()+1,a.end(),restscore);
        int change = m - my_score;
        // cout<<restscore<<nl;
        if(restscore-change >= 0){
            cout<<min(my_score + (int)(restscore),m)<<nl;
        }
        else cout<<my_score<<nl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int a,x,y,k=0;
    scanf("%d",&a);

    while(a--){
        k = 0;
        scanf("%d %d",&x,&y);
        int vec[x];
        for (int i = 0; i < x; i++){
            scanf("%d",&vec[i]);
            k+=vec[i];
        }
        if(vec[0] == y){
            printf("%d\n",y);
        }else if(k>y){
            printf("%d\n",y);
        }
        else{
            printf("%d\n",k);
        }

    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int a,x,y,k=0;
    scanf("%d",&a);

    while(a--){

        scanf("%d %d",&x,&y);
        int vec[x];
        for (int i = 0; i < x; i++){
            scanf("%d",&vec[i]);
            k+=vec[i];
        }
        if(k+vec[0]>=y){
            printf("%d\n",y);
        }else{
            printf("%d\n",k+vec[0]);
        }

    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int a,x,y,k=0;
    scanf("%d",&a);

    while(a--){
        k = 0;
        scanf("%d %d",&x,&y);
        int vec[x];
        for (int i = 0; i < x; i++){
            scanf("%d",&vec[i]);
            k+=vec[i];
        }
        if(vec[0] == y){
            printf("%d\n",y);
        }else if(k+vec[0]>y){
            printf("%d\n",y);
        }
        else{
            printf("%d\n",k);
        }

    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int a,x,y,k=0;
    scanf("%d",&a);

    while(a--){

        scanf("%d %d",&x,&y);
        int vec[x];
        for (int i = 0; i < x; i++){
            scanf("%d",&vec[i]);
            k+=vec[i];
        }
        if(k+vec[0]>=y){
            printf("%d\n",y);
        }else{
            printf("%d\n",k);
        }

    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int a,x,y,k=0;
    scanf("%d",&a);

    while(a--){

        scanf("%d %d",&x,&y);
        int vec[x];
        for (int i = 0; i < x; i++){
            scanf("%d",&vec[i]);
            k+=vec[i];
        }
        if(k+vec[0]>=y){
            printf("%d\n",y);
        }else{
            printf("%d\n",k-vec[0]);
        }

    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int a,x,y,k=0;
    scanf("%d",&a);

    while(a--){
        k = 0;
        scanf("%d %d",&x,&y);
        int vec[x];
        for (int i = 0; i < x; i++){
            scanf("%d",&vec[i]);
            k+=vec[i];
        }
        if(k+vec[0]>=y){
            printf("%d\n",y);
        }else{
            printf("%d\n",k-vec[0]);
        }

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,m,b=0,i,a[1005];
    cin>>t;
    while(t--){
        cin>>n>>m;
    for(i=1;i<=n;i++){
        cin>>a[i];
        b=b+a[i];
    }
    if(m<=b) cout<<m<<endl;
    else cout<<b<<endl;
    b=0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,m,b=0,i,c,d;
    cin>>t;
    while(t--){
        cin>>n>>m;
    int a[n];
    for(i=1;i<=n;i++){
        cin>>a[i];
        b=b+a[i];
    }

    while(n--){
        c=(double)b/n;
        d=(double)m/n;
        if(d<=c) break;
        else m--;
    }
    cout<<m<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,m,b=0,i;
    double c,d;
    cin>>t;
    while(t--){
        cin>>n>>m;
    int a[n];
    for(i=1;i<=n;i++){
        cin>>a[i];
        b=b+a[i];
    }
    for(i=1;i<=n;i++){
        if(m<=b) break;
        else m--;
    }
    if(m<0) m=0;
    cout<<m<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,m,b=0,i,a[1005];
    cin>>t;
    while(t--){
        cin>>n>>m;
    for(i=1;i<=n;i++){
        cin>>a[i];
        b=b+a[i];
    }
    for(i=1;i<=n;i++){
        if(m<=b) break;
        else m--;
    }
    if(m<0) m=0;
    cout<<m<<endl;
    b=0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,m,b=0,i;
    double c,d;
    cin>>t;
    while(t--){
        cin>>n>>m;
    int a[n];
    for(i=1;i<=n;i++){
        cin>>a[i];
        b=b+a[i];
    }

    while(n--){
        c=(double)b/n;
        d=(double)m/n;
        if(d<=c) break;
        else m--;
    }
    cout<<m<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,m,b=0,i;
    double c,d;
    cin>>t;
    while(t--){
        cin>>n>>m;
    int a[n];
    for(i=1;i<=n;i++){
        cin>>a[i];
        b=b+a[i];
    }

    for(i=1;i<=n;i++){
        c=(double)b/n;
        d=(double)m/n;
        if(d<=c) break;
        else m--;
    }
    cout<<m<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,m,b=0,i;
    double c,d;
    cin>>t;
    while(t--){
        cin>>n>>m;
    int a[n];
    for(i=1;i<=n;i++){
        cin>>a[i];
        b=b+a[i];
    }
    for(i=1;i<=n;i++){
        if(m<=b) break;
        else m--;
    }
    if(m<0) m=0;
    cout<<m<<endl;
    b=0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,m,b=0,i,a[1005];
    cin>>t;
    while(t--){
        cin>>n>>m;
    for(i=1;i<=n;i++){
        cin>>a[i];
        b=b+a[i];
    }
    for(i=1;i<=n;i++){
        if(m<=b) break;
        else m--;
    }
    if(m<a[1]) m=a[1];
    cout<<m<<endl;
    b=0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n,m,a,av=0,sup=0;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>n>>m;
        av=0;
        sup=0;
        for(int j=0;j<n;j++){
            cin>>a;
            av=av+a;
            if(a>sup){
                sup=a;
            }
        }
        av=av/n;
        if(n==1){
            cout<<min(m,a);
            cout<<'\n';
        }
        else{
            if(m/n<=av){
                cout<<min(sup,m);
                cout<<'\n';
            }
            else{
                if(av==0){
                    cout<<sup;
                    cout<<'\n';
                }
                else{
                    cout<<av;
                    cout<<'\n';
                }
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n,m,a,av=0;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>n>>m;
        av=0;
        for(int j=0;j<n;j++){
            cin>>a;
            av=av+a;
        }
        if(n==1){
            cout<<min(m,a);
            cout<<'\n';
        }
        else{
            if(m<=av){
                cout<<m;
                cout<<'\n';
            }
            else{
                cout<<av;
                cout<<'\n';
            }
    }
    }
    return 0;
}