import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

alias Edge = Tuple!(int, "to", int, "cost");

immutable long inf = 10L^^16;

void main() {
    int n;
    scan(n);

    auto d = new long[](n + 1);
    auto dp = new long[](n + 1);
    auto cnt = new int[](n + 1);
    auto adj = new Edge[][](n + 1, 0);

    bool c2 = 1;
    long ans;

    foreach (i ; 0 .. n - 1) {
        int ai, bi, ci;
        scan(ai, bi, ci);
        if (ci > 2) c2 = 0;
        adj[ai] ~= Edge(bi, ci);
        adj[bi] ~= Edge(ai, ci);
    }

    
    void dfs(int s, int v, int p, long mine) {
        if (s != v) {
            ans += mine;
        }

        foreach (e ; adj[v]) {
            if (e.to != p) {
                dfs(s, e.to, v, min(mine, e.cost));
            }
        }
    }

    int node_count(int v, int p) {
        cnt[v] = 1;

        foreach (e ; adj[v]) {
            if (e.to != p) cnt[v] += node_count(e.to, v);
        }

        return cnt[v];
    }

    long dfsUp(int v, int p) {
        d[v] = 0;

        foreach (e ; adj[v]) {
            if (e.to != p) {
                if (e.cost == 1) {
                    d[v] += cnt[e.to];
                    dfsUp(e.to, v);
                }
                else {
                    d[v] += dfsUp(e.to, v) + 2;
                }
            }
        }

        return d[v];
    }

    void dfsDown(int v, int p, long dpar) {
        dp[v] = dpar + d[v];

        foreach (e ; adj[v]) {
            if (e.to != p) {
                if (e.cost == 1) {
                    dfsDown(e.to, v, n - cnt[e.to]);
                }
                else {
                    dfsDown(e.to, v, dp[v] - d[e.to]);
                }
            }
        }
    }

    if (c2) {
        node_count(1, 0);
        dfsUp(1, 0);
        dfsDown(1, 0, 0);

        writefln("%(%s\n%)", dp[1 .. n + 1]);
    }
    else if (n <= 2000) {
        foreach (i ; 1 .. n + 1) {
            ans = 0;

            dfs(i, i, 0, inf);

            writeln(ans);
        }
    }
}



void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}