/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 100005
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
ll up[maxv],down[maxv];
std::vector<int> edge[maxv];
int n;
void dfs(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        dfs(v,u);
        ll ans = (1+down[v])%MOD;
        down[u] = down[u]*ans%MOD;
    }
}
ll quick(ll a,int x){
    ll ans = 1;
    while(x){
        if(x&1)ans = ans*a%MOD;
        x >>= 1;
        a = a*a%MOD;
    }
    return ans;
}
void dfs1(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        ll tmp = (1ll+down[v])%MOD;
        //cout << u << v << edge[u].size() << endl;
        if(edge[u].size()>2 || (u==1 && edge[u].size()!=1))up[v] = (1ll+up[u]+down[u]%MOD*quick(tmp,MOD-2)%MOD+MOD)%MOD;
        else up[v] = (1ll+up[u]+MOD)%MOD;
        if(u == 1 && edge[u].size()==1)up[v] = (up[v]+1)%MOD;
        dfs1(v,u);
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n;
	for(int i=2;i<=n;i++){
        int a;cin >> a;
        edge[i].push_back(a);
        edge[a].push_back(i);
    }
    for(int i=1;i<=n;i++)down[i] = 1;
    dfs(1,-1);
    dfs1(1,-1);
    //for(int i=1;i<=n;i++)cout << down[i] << ' ';
    //cout << '\n';
    //for(int i=1;i<=n;i++)cout << up[i] << ' ';
    //cout << '\n';
    up[1] = 1;
    for(int i=1;i<=n;i++){
        cout << up[i]*down[i]%MOD << ' ';
    }
    return 0;
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 100005
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
ll up[maxv],down[maxv];
std::vector<int> edge[maxv];
int n;
void dfs(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        dfs(v,u);
        ll ans = (1+down[v])%MOD;
        down[u] = down[u]*ans%MOD;
    }
}
ll quick(ll a,int x){
    ll ans = 1;
    while(x){
        if(x&1)ans = ans*a%MOD;
        x >>= 1;
        a = a*a%MOD;
    }
    return ans;
}
void dfs1(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        ll tmp = (1ll+down[v])%MOD;
        if(u !=1 && edge[u].size()>2)up[v] = (1ll+up[u]%MOD*down[u]%MOD*quick(tmp,MOD-2)%MOD+MOD)%MOD;
        else if(u==1){
            up[v] = (1ll+down[u]%MOD*quick(tmp,MOD-2)%MOD+MOD)%MOD;
        }
        else up[v] = (1ll+up[u])%MOD;
        //if(u == 1 && edge[u].size()==1)up[v] = (up[v]+1)%MOD;
        dfs1(v,u);
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n;
	for(int i=2;i<=n;i++){
        int a;cin >> a;
        edge[i].push_back(a);
        edge[a].push_back(i);
    }
    up[1] = 1;
    for(int i=1;i<=n;i++)down[i] = 1;

    dfs(1,-1);
    dfs1(1,-1);

   // for(int i=1;i<=n;i++)cout << down[i] << ' ';
    //cout << '\n';
    //for(int i=1;i<=n;i++)cout << up[i] << ' ';
    //cout << '\n';
    for(int i=1;i<=n;i++){
        cout << up[i]*down[i]%MOD << ' ';
    }
    return 0;
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 200005
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
ll up[maxv],down[maxv];
std::vector<int> edge[maxv];
int n;
void dfs(int u){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        dfs(v);
        ll ans = (1+down[v])%MOD;
        down[u] = down[u]*ans%MOD;
    }
}
ll quick(ll a,int x){
    ll ans = 1;
    while(x){
        if(x&1)ans = ans*a%MOD;
        x >>= 1;
        a = a*a%MOD;
    }
    return ans;
}
void dfs1(int u){
    int m = edge[u].size();
    std::vector<ll> v(m+1),v1(m+1);
    for(int i=0;i<m;i++)v[i]=v1[i]=down[edge[u][i]]+1;
    for(int i=1;i<m;i++)v[i]=v[i]*v[i-1]%MOD;
    for(int i=m-2;i>=0;i--)v1[i] = v1[i]*v1[i+1]%MOD;
    for(int i=0;i<m;i++){
        int vv = edge[u][i];
        ll ans = 1;
        if(i>=1)ans = ans * v[i-1]%MOD;
        if(i<m-1)ans = ans*v1[i+1]%MOD;
        up[vv] = (ans*up[u]%MOD+1)%MOD;
        dfs1(vv);
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n;
	for(int i=2;i<=n;i++){
        int a;cin >> a;
        edge[a].push_back(i);
    }
    for(int i=1;i<=n;i++)down[i] = 1,up[i]=1;
    dfs(1);
    dfs1(1);
    for(int i=1;i<=n;i++){
        cout << up[i]*down[i]%MOD << ' ';
    }
    return 0;
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 100005
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
ll up[maxv],down[maxv];
std::vector<int> edge[maxv];
int n;
void dfs(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        dfs(v,u);
        ll ans = (1+down[v])%MOD;
        down[u] = down[u]*ans%MOD;
    }
}
ll quick(ll a,int x){
    ll ans = 1;
    while(x){
        if(x&1)ans = ans*a%MOD;
        x >>= 1;
        a = a*a%MOD;
    }
    return ans;
}
void dfs1(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        ll tmp = (1ll+down[v])%MOD;
        up[v] = (1ll+up[u]+down[u]%MOD*quick(tmp,MOD-2)%MOD+MOD)%MOD;
        if(edge[u].size()==1)up[v] = (up[v]+1)%MOD;
        dfs1(v,u);
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n;
    //cout << (1<<30)%MOD << endl;
	for(int i=2;i<=n;i++){
        int a;cin >> a;
        edge[i].push_back(a);
        edge[a].push_back(i);
    }
    for(int i=1;i<=n;i++)down[i] = 1;
    dfs(1,-1);
    dfs1(1,-1);
   // for(int i=1;i<=n;i++)cout << down[i] << endl;
    up[1] = 1;
    for(int i=1;i<=n;i++){
        cout << up[i]*down[i]%MOD << ' ';
    }
    return 0;
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 200005
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
ll up[maxv],down[maxv];
std::vector<int> edge[maxv];
int n;
void dfs(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        dfs(v,u);
        ll ans = (1+down[v])%MOD;
        down[u] = down[u]*ans%MOD;
    }
}
ll quick(ll a,int x){
    ll ans = 1;
    while(x){
        if(x&1)ans = ans*a%MOD;
        x >>= 1;
        a = a*a%MOD;
    }
    return ans;
}
void dfs1(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        ll tmp = (1ll+down[v])%MOD;
        if(u !=1 && edge[u].size()>2)up[v] = (1ll+up[u]%MOD*down[u]%MOD*quick(tmp,MOD-2)%MOD+MOD)%MOD;
        else if(u==1){
            up[v] = (1ll+down[u]%MOD*quick(tmp,MOD-2)%MOD+MOD)%MOD;
        }
        else up[v] = (1ll+up[u])%MOD;
        //if(u == 1 && edge[u].size()==1)up[v] = (up[v]+1)%MOD;
        dfs1(v,u);
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n;
	for(int i=2;i<=n;i++){
        int a;cin >> a;
        edge[i].push_back(a);
        edge[a].push_back(i);
    }
    up[1] = 1;
    for(int i=1;i<=n;i++)down[i] = 1;

    dfs(1,-1);
    dfs1(1,-1);

   // for(int i=1;i<=n;i++)cout << down[i] << ' ';
    //cout << '\n';
    //for(int i=1;i<=n;i++)cout << up[i] << ' ';
    //cout << '\n';
    for(int i=1;i<=n;i++){
        cout << up[i]*down[i]%MOD << ' ';
    }
    return 0;
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 100005
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
ll up[maxv],down[maxv];
std::vector<int> edge[maxv];
int n;
void dfs(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        dfs(v,u);
        ll ans = (1+down[v])%MOD;
        down[u] = down[u]*ans%MOD;
    }
}
void dfs1(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        ll tmp = (1+down[v])%MOD;
        up[v] = (1+up[u]+down[u]-tmp+MOD)%MOD;
        if(u==1 && edge[u].size()==1)up[v] = (up[v]+1)%MOD;
        dfs1(v,u);
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n;
	for(int i=2;i<=n;i++){
        int a;cin >> a;
        edge[i].push_back(a);
        edge[a].push_back(i);
    }
    for(int i=1;i<=n;i++)down[i] = 1;
    dfs(1,-1);
    dfs1(1,-1);
    //for(int i=1;i<=n;i++)cout << up[i] << endl;
    up[1] = 1;
    for(int i=1;i<=n;i++){
        //printf("%lld ",up[i]*down[i]%MOD);
        cout << up[i]*down[i]%MOD << ' ';
    }
    return 0;
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 200005
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
ll up[maxv],down[maxv];
std::vector<int> edge[maxv];
int n;
void dfs(int u){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        dfs(v);
        ll ans = (1+down[v])%MOD;
        down[u] = down[u]*ans%MOD;
    }
}
ll quick(ll a,int x){
    ll ans = 1;
    while(x){
        if(x&1)ans = ans*a%MOD;
        x >>= 1;
        a = a*a%MOD;
    }
    return ans;
}
void dfs1(int u){
    int m = edge[u].size();
    std::vector<ll> v(m+1),v1(m+1);
    for(int i=0;i<m;i++)v[i]=v1[i]=down[edge[u][i]]+1;
    for(int i=1;i<m;i++)v[i]=v[i]*v[i-1]%MOD;
    for(int i=m-2;i>=0;i--)v1[i] = v1[i]*v1[i+1]%MOD;
    for(int i=0;i<m;i++){
        int vv = edge[u][i];
        ll ans = 1;
        if(i>=1)ans = ans * v[i-1];
        if(i<m-1)ans = ans*v1[i+1];
        up[vv] = ans*up[u]+1;
        dfs1(vv);
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n;
	for(int i=2;i<=n;i++){
        int a;cin >> a;
        edge[a].push_back(i);
    }
    for(int i=1;i<=n;i++)down[i] = 1,up[i]=1;
    dfs(1);
    dfs1(1);
    for(int i=1;i<=n;i++){
        cout << up[i]*down[i]%MOD << ' ';
    }
    return 0;
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 100005
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
ll up[maxv],down[maxv];
std::vector<int> edge[maxv];
int n;
void dfs(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        dfs(v,u);
        ll ans = (1+down[v])%MOD;
        down[u] = down[u]*ans%MOD;
    }
}
ll quick(ll a,int x){
    ll ans = 1;
    while(x){
        if(x&1)ans = ans*a%MOD;
        x >>= 1;
        a = a*a%MOD;
    }
    return ans;
}
void dfs1(int u,int fa){
    for(int i=0;i<edge[u].size();i++){
        int v = edge[u][i];
        if(v == fa)continue;
        ll tmp = (1ll+down[v])%MOD;
        //cout << u << v<< endl;
        if(edge[u].size()>2 || (u==1 && edge[u].size()!=1))up[v] = (1ll+up[u]*down[u]%MOD*quick(tmp,MOD-2)%MOD+MOD)%MOD;
        else up[v] = (1ll+up[u])%MOD;
        if(u == 1 && edge[u].size()==1)up[v] = (up[v]+1)%MOD;
        dfs1(v,u);
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n;
	for(int i=2;i<=n;i++){
        int a;cin >> a;
        edge[i].push_back(a);
        edge[a].push_back(i);
    }
    for(int i=1;i<=n;i++)down[i] = 1,up[i]=1;
    dfs(1,-1);
    dfs1(1,-1);
    //for(int i=1;i<=n;i++)cout << down[i] << ' ';
    //cout << '\n';
    //for(int i=1;i<=n;i++)cout << up[i] << ' ';
    //cout << '\n';
   // up[1] = 1;
    for(int i=1;i<=n;i++){
        cout << up[i]*down[i]%MOD << ' ';
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define SZ(x) ((int)x.size())
#define DB(x) cout << #x << "=" << x << endl
#define endl '\n'

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const double EPS = 1e-7;
const int64 MOD = (int64)1e9 + 7;
const int MAXN = (int)2e5 + 10;

int64 dp[MAXN];
int zeros[MAXN];
vi ady[MAXN];
int64 ans[MAXN];

int64 fe(int64 v, int64 p){
	if (!p) return 1;
	int64 ans = fe(v * v % MOD, p / 2);
	if (p & 1) ans = ans * v % MOD;
	return ans;
}

void solve(int s){
	dp[s] = 1;
	for (auto child : ady[s]){
		solve(child);
		int64 val = 1 + dp[child];

		if (val == MOD) zeros[s]++;
		else {
			dp[s] *= (1 + dp[child]);
			dp[s] %= MOD;
		}
	}
}

int64 getdiv(int s, int child){
	// ans[s] / (1 + dp[child])

	//ans[s] * fe(1 + dp[child], MOD - 2) % MOD
	if (1 + dp[child] != MOD) return ans[s] * fe(1 + dp[child], MOD - 2) % MOD;
	if (zeros[s] > 1) return 0;
	return ans[s];
}

void solve2(int s){
	for (auto child : ady[s]){
		ans[child] = dp[child] * (1 + getdiv(s, child)) % MOD;
		solve2(child);
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;

	for (int i = 2; i <= n; ++i){
		int v; cin >> v;
		ady[v].push_back(i);
	}

	solve(1);
	ans[1] = dp[1];
	solve2(1);

	for (int i = 1; i <= n; ++i)
		cout << ans[i] << " ";
	cout << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define DB(x) cout << #x << " = " << x << endl << flush;
#define SZ(x) (int)((x).size())
#define MP make_pair

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int oo = 0x3f3f3f3f;
const double EPS = 1e-9;
const int MAXN = (int)2e5 + 10;
//const int64 MOD = (int64)3;
const int64 MOD = (int64)1e9 + 7;

int64 modexp(int64 val, int64 p){
	if (!p) return 1;
	int64 r = modexp(val * val % MOD, p / 2);
	if (p & 1) r = r * val % MOD;
	return r;
}

struct Z{
	int64 val;
	int tot;

	Z(){
		val = 0, tot = 1;
	}
	Z(int64 v){
		val = v % MOD, tot = 0;
		if (!val) val = 1, tot = 1;
		if (val < 0) val += MOD;
	}
	Z operator+(const Z &a){
		return Z(val + a.val);
	}
	Z operator-(const Z &a){
		return Z(val - a.val);
	}
	Z operator*(const Z &a){
		Z ans;
		ans.val = val * a.val % MOD;
		ans.tot = tot + a.tot;
		return ans;
	}
	Z operator/(const Z &a){
		Z ans;
		ans.val = val * modexp(a.val, MOD - 2);
		ans.tot = tot - a.tot;
		return ans;
	}
	void print(){
		if (tot) cout << 0;
		else cout << val;
	}
};

int p[MAXN];
Z dp[MAXN];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;
	dp[1] = Z(1);
	for (int i = 2; i <= n; ++i){
		dp[i] = Z(1);
		cin >> p[i];
	}

	for (int i = n; i >= 2; --i){
		dp[p[i]] = dp[p[i]] * (Z(1) + dp[i]);
	}

	for (int i = 2; i <= n; ++i){
//		cout << i << " " << dp[i].val << " " << dp[i].tot << endl;
		dp[i] = dp[i] * (Z(1) + dp[p[i]] / (Z(1) + dp[i]));
	}

	for (int i = 1; i <= n; ++i){
		dp[i].print();
		cout << " ";
	}
	cout << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define SZ(x) ((int)x.size())
#define DB(x) cout << #x << "=" << x << endl
#define endl '\n'

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const double EPS = 1e-7;
const int64 MOD = (int64)1e9 + 7;
const int MAXN = (int)2e5 + 10;

int64 dp[MAXN];
int zeros[MAXN];
vi ady[MAXN];
int64 ans[MAXN];

int64 fe(int64 v, int64 p){
	if (!p) return 1;
	int64 ans = fe(v * v % MOD, p / 2);
	if (p & 1) ans = ans * v % MOD;
	return ans;
}

void solve(int s){
	dp[s] = 1;
	for (auto child : ady[s]){
		solve(child);
		int64 val = 1 + dp[child];

		if (val == MOD) zeros[s]++;
		else {
			dp[s] *= (1 + dp[child]);
			dp[s] %= MOD;
		}
	}
}

int64 getdiv(int s, int child){
	// ans[s] / (1 + dp[child])

	//ans[s] * fe(1 + dp[child], MOD - 2) % MOD
	if (1 + dp[child] != MOD) return ans[s] * fe(1 + dp[child], MOD - 2) % MOD;
	if (zeros[s] > 1) return 0;
	return ans[s];
}

void solve2(int s){
	for (auto child : ady[s]){
		ans[child] = dp[child] * (1 + getdiv(s, child)) % MOD;
		solve2(child);
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;

	for (int i = 2; i <= n; ++i){
		int v; cin >> v;
		ady[v].push_back(i);
	}

	solve(1);
	ans[1] = dp[1];
	if (zeros[1]) ans[1] = 0;
	solve2(1);

	for (int i = 1; i <= n; ++i)
		cout << ans[i] << " ";
	cout << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define SZ(x) ((int)x.size())
#define DB(x) cout << #x << "=" << x << endl
#define endl '\n'

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const double EPS = 1e-7;
const int64 MOD = (int64)1e9 + 7;
const int MAXN = (int)2e5 + 10;

int64 dp[MAXN];
int zeros[MAXN];
vi ady[MAXN];
int64 ans[MAXN];

int64 fe(int64 v, int64 p){
	if (!p) return 1;
	int64 ans = fe(v * v % MOD, p / 2);
	if (p & 1) ans = ans * v % MOD;
	return ans;
}

void solve(int s){
	dp[s] = 1;
	for (auto child : ady[s]){
		solve(child);
		int64 val = 1 + dp[child];

		if (val == MOD) zeros[s]++;
		else {
			dp[s] *= (1 + dp[child]);
			dp[s] %= MOD;
		}
	}
}

int64 getdiv(int s, int child){
	// ans[s] / (1 + dp[child])

	//ans[s] * fe(1 + dp[child], MOD - 2) % MOD
	if (1 + dp[child] != MOD) return ans[s] * fe(1 + dp[child], MOD - 2) % MOD;
	if (zeros[s] > 1) return 0;
	return ans[s];
}

void solve2(int s){
	for (auto child : ady[s]){
		ans[child] = dp[child] * (1 + getdiv(s, child)) % MOD;
		solve2(child);
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;

	for (int i = 2; i <= n; ++i){
		int v; cin >> v;
		ady[v].push_back(i);
	}

	solve(1);
	ans[1] = dp[1];
	solve2(1);
	if (zeros[1]) ans[1] = 0;

	for (int i = 1; i <= n; ++i)
		cout << ans[i] << " ";
	cout << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define SZ(x) ((int)x.size())
#define DB(x) cout << #x << "=" << x << endl
#define endl '\n'

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const double EPS = 1e-7;
const int64 MOD = (int64)1e9 + 7;
const int MAXN = (int)2e5 + 10;

int64 dp[MAXN];
vi ady[MAXN];
int64 ans[MAXN];

int64 fe(int64 v, int64 p){
	if (!p) return 1;
	int64 ans = fe(v * v % MOD, p / 2);
	if (p & 1) ans = ans * v % MOD;
	return ans;
}

void solve(int s){
	dp[s] = 1;
	for (auto child : ady[s]){
		solve(child);
		dp[s] *= (1 + dp[child]);
		dp[s] %= MOD;
	}
}

void solve2(int s){
	for (auto child : ady[s]){
		ans[child] = dp[child] * (1 + ans[s] * fe(1 + dp[child], MOD - 2) % MOD) % MOD;
		solve2(child);
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;

	for (int i = 2; i <= n; ++i){
		int v; cin >> v;
		ady[v].push_back(i);
	}

	solve(1);
	ans[1] = dp[1];
	solve2(1);

	for (int i = 1; i <= n; ++i)
		cout << ans[i] << " ";
	cout << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define DB(x) cout << #x << " = " << x << endl << flush;
#define SZ(x) (int)((x).size())
#define MP make_pair

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int64> vi;

const int oo = 0x3f3f3f3f;
const double EPS = 1e-9;
const int MAXN = (int)2e5 + 10;
const int64 MOD = (int64)1e9 + 7;

int parent[MAXN];
vi ady[MAXN], P[MAXN], S[MAXN];
int64 dp[MAXN], top[MAXN];

void dfs1(int s){
	dp[s] = 1;
	P[s].push_back(1);
	for (auto child : ady[s]){
		dfs1(child);
		dp[s] *= 1 + dp[child];
		dp[s] %= MOD;
		P[s].push_back(dp[child] + 1);
		S[s].push_back(dp[child] + 1);
	}
	S[s].push_back(1);

	for (int i = 1; i < SZ(P[s]); ++i) P[s][i] = (P[s][i] * P[s][i - 1]) % MOD;
	for (int i = SZ(S[s]) - 2; i >= 0; --i) S[s][i] = (S[s][i] * S[s][i + 1]) % MOD;
}

void dfs2(int s){
	for (int i = 0; i < SZ(ady[s]); ++i){
		int child = ady[s][i];
		top[child] = P[s][i] * S[s][i + 1] % MOD * top[s] % MOD + 1;
		top[child] %= MOD;
		dp[child] = dp[child] * top[child] % MOD;
		dfs2(child);
	}
}

int main()
{
	int n; cin >> n;
	for (int i = 2; i <= n; ++i){
		cin >> parent[i];
		ady[parent[i]].push_back(i);
	}

	dfs1(1);
	top[1] = 1;
	dfs2(1);

	for (int i = 1; i <= n; ++i)
		cout << dp[i] << " ";
	cout << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define SZ(x) ((int)x.size())
#define DB(x) cout << #x << "=" << x << endl
#define endl '\n'

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const double EPS = 1e-7;
const int64 MOD = (int64)1e9 + 7;
const int MAXN = (int)2e5 + 10;

int64 dp[MAXN];
int zeros[MAXN];
vi ady[MAXN];
int64 ans[MAXN];

int64 fe(int64 v, int64 p){
	if (!p) return 1;
	int64 ans = fe(v * v % MOD, p / 2);
	if (p & 1) ans = ans * v % MOD;
	return ans;
}

int getDp(int child){
	if (zeros[child]) return 0;
	return dp[child];
}

void solve(int s){
	dp[s] = 1;
	for (auto child : ady[s]){
		solve(child);
		int64 val = 1 + getDp(child);

		if (val == MOD) zeros[s]++;
		else {
			dp[s] *= (1 + getDp(child));
			dp[s] %= MOD;
		}
	}
}

int64 getdiv(int s, int child){
	// ans[s] / (1 + dp[child])

	//ans[s] * fe(1 + dp[child], MOD - 2) % MOD
	if (1 + getDp(child) != MOD) return ans[s] * fe(1 + getDp(child), MOD - 2) % MOD;
	if (zeros[s] > 1) return 0;
	return ans[s];
}

void mult(int child, int64 v){
	if (v == MOD){
		zeros[child]++;
		ans[child] = getDp(child);
	}
	else{
		ans[child] = getDp(child) * v % MOD;
	}
}

void solve2(int s){
	for (auto child : ady[s]){
		mult(child, 1 + getdiv(s, child));
//		ans[child] = dp[child] * (1 + getdiv(s, child)) % MOD;
		solve2(child);
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;

	for (int i = 2; i <= n; ++i){
		int v; cin >> v;
		ady[v].push_back(i);
	}

	solve(1);
	ans[1] = getDp(1);
	solve2(1);

	for (int i = 1; i <= n; ++i){
		cout << ans[i] << " ";
//		if (zeros[i]) cout << 0 << " ";
//		else cout << ans[i] << " ";
	}
	cout << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define DB(x) cout << #x << " = " << x << endl << flush;
#define SZ(x) (int)((x).size())
#define MP make_pair

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int oo = 0x3f3f3f3f;
const double EPS = 1e-9;
const int MAXN = (int)1e5 + 10;
//const int64 MOD = (int64)3;
const int64 MOD = (int64)1e9 + 7;

int64 modexp(int64 val, int64 p){
	if (!p) return 1;
	int64 r = modexp(val * val % MOD, p / 2);
	if (p & 1) r = r * val % MOD;
	return r;
}

struct Z{
	int64 val;
	int tot;

	Z(){
		val = 0, tot = 1;
	}
	Z(int64 v){
		val = v % MOD, tot = 0;
		if (!val) val = 1, tot = 1;
		if (val < 0) val += MOD;
	}
	Z operator+(const Z &a){
		return Z(val + a.val);
	}
	Z operator-(const Z &a){
		return Z(val - a.val);
	}
	Z operator*(const Z &a){
		Z ans;
		ans.val = val * a.val % MOD;
		ans.tot = tot + a.tot;
		return ans;
	}
	Z operator/(const Z &a){
		Z ans;
		ans.val = val * modexp(a.val, MOD - 2);
		ans.tot = tot - a.tot;
		return ans;
	}
	void print(){
		if (tot) cout << 0;
		else cout << val;
	}
};

int p[MAXN];
Z dp[MAXN];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;
	dp[1] = Z(1);
	for (int i = 2; i <= n; ++i){
		dp[i] = Z(1);
		cin >> p[i];
	}

	for (int i = n; i >= 2; --i){
		dp[p[i]] = dp[p[i]] * (Z(1) + dp[i]);
	}

	for (int i = 2; i <= n; ++i){
//		cout << i << " " << dp[i].val << " " << dp[i].tot << endl;
		dp[i] = dp[i] * (Z(1) + dp[p[i]] / (Z(1) + dp[i]));
	}

	for (int i = 1; i <= n; ++i){
		dp[i].print();
		cout << " ";
	}
	cout << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define DB(x) cout << #x << " = " << x << endl << flush;
#define SZ(x) (int)((x).size())
#define MP make_pair

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int oo = 0x3f3f3f3f;
const double EPS = 1e-9;
const int MAXN = (int)2e5 + 10;
const int64 MOD = (int64)1e9 + 7;

int parent[MAXN];
vi ady[MAXN], P[MAXN], S[MAXN];
int64 dp[MAXN], top[MAXN];

void dfs1(int s){
	dp[s] = 1;
	P[s].push_back(1);
	for (auto child : ady[s]){
		dfs1(child);
		dp[s] *= 1 + dp[child];
		dp[s] %= MOD;
		P[s].push_back(dp[child] + 1);
		S[s].push_back(dp[child] + 1);
	}
	S[s].push_back(1);

	for (int i = 1; i < SZ(P[s]); ++i) P[s][i] = (P[s][i] * P[s][i - 1]) % MOD;
	for (int i = SZ(S[s]) - 2; i >= 0; --i) S[s][i] = (S[s][i] * S[s][i + 1]) % MOD;
}

void dfs2(int s){
	for (int i = 0; i < SZ(ady[s]); ++i){
		int child = ady[s][i];
		top[child] = P[s][i] * S[s][i + 1] % MOD * top[s] % MOD + 1;
		top[child] %= MOD;
		dp[child] = dp[child] * top[child] % MOD;
		dfs2(child);
	}
}

int main()
{
	int n; cin >> n;
	for (int i = 2; i <= n; ++i){
		cin >> parent[i];
		ady[parent[i]].push_back(i);
	}

	dfs1(1);
	top[1] = 1;
	dfs2(1);

	for (int i = 1; i <= n; ++i)
		cout << dp[i] << " ";
	cout << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define SZ(x) ((int)x.size())
#define DB(x) cout << #x << "=" << x << endl
#define endl '\n'

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const double EPS = 1e-7;
const int64 MOD = (int64)1e9 + 7;
const int MAXN = (int)2e5 + 10;

int64 dp[MAXN];
int zeros[MAXN];
vi ady[MAXN];
int64 ans[MAXN];

int64 fe(int64 v, int64 p){
	if (!p) return 1;
	int64 ans = fe(v * v % MOD, p / 2);
	if (p & 1) ans = ans * v % MOD;
	return ans;
}

void solve(int s){
	dp[s] = 1;
	for (auto child : ady[s]){
		solve(child);
		int64 val = 1 + dp[child];

		if (val == MOD) zeros[s]++;
		else {
			dp[s] *= (1 + dp[child]);
			dp[s] %= MOD;
		}
	}
}

int64 getdiv(int s, int child){
	// ans[s] / (1 + dp[child])

	//ans[s] * fe(1 + dp[child], MOD - 2) % MOD
	if (1 + dp[child] != MOD) return ans[s] * fe(1 + dp[child], MOD - 2) % MOD;
	if (zeros[s] > 1) return 0;
	return ans[s];
}

void mult(int child, int64 v){
	if (v == MOD){
		zeros[child]++;
		ans[child] = dp[child];
	}
	else{
		ans[child] = dp[child] * v % MOD;
	}
}

void solve2(int s){
	for (auto child : ady[s]){
		mult(child, 1 + getdiv(s, child));
//		ans[child] = dp[child] * (1 + getdiv(s, child)) % MOD;
		solve2(child);
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;

	for (int i = 2; i <= n; ++i){
		int v; cin >> v;
		ady[v].push_back(i);
	}

	solve(1);
	ans[1] = dp[1];
	solve2(1);

	for (int i = 1; i <= n; ++i){
		if (zeros[i]) cout << 0 << " ";
		else cout << ans[i] << " ";
	}
	cout << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<LL>      VL;
typedef vector<string>  VS;
typedef vector<PII>     VPII;
#define MM(a,x)  memset(a,x,sizeof(a));
#define ALL(x)   (x).begin(), (x).end()
#define P(x)	 cerr<<"{"#x<<" = "<<(x)<<"}"<<endl;
#define P2(x,y)	 cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<"}"<<endl;
#define P3(x,y,z)cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<", "#z" = "<<(z)<<"}"<<endl;
#define PP(x,i)	 cerr<<"{"#x"["<<i<<"] = "<<x[i]<<"}"<<endl;
#define TM(a,b)	 cerr<<"{"#a" -> "#b": "<<1000*(b-a)/CLOCKS_PER_SEC<<"ms}\n";
#define UN(v)    sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0); cin.tie(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> T convert(string s) {T x; stringstream(s) >> x; return x;}
template<class T> string tostring(T x, int w = 0, char c = ' ') {stringstream ss; ss << x; string r(ss.str()); return string(max(0, w - (int)r.size()), c) + r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w); if(a != b && (!n || ++c % n)) cout << s; else cout << "\n";}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 211111;

LL exp(LL x, LL n, int m = mod) {LL r = 1 % m; for(x = (x % m + m) % m; n > 0; x = x * x % m, n >>= 1) if(n & 1) r = r * x % m; return r;}
int inv(LL a, int m = mod) {return exp(a, m - 2);}

VI g[N];
int p[N];

int n;
LL fromBottom[N];
LL fromUp[N];
int zero[N];

void dfs(int u) {
	for(int v : g[u]) dfs(v);
	fromBottom[u] = 1;
	for(int v : g[u]) {
		if(zero[v] == 0 && fromBottom[v] + 1 == mod) {
			zero[u]++;
		} else {
			if(zero[v] == 0) {
				fromBottom[u] = fromBottom[u] * (fromBottom[v] + 1) % mod;
			}
		}
	}
}

void dfs1(int u) {
	fromUp[u] = 1;
	if(zero[p[u]] == 0) {
		fromUp[u] += fromBottom[p[u]] * inv(fromBottom[u] + 1) % mod * fromUp[p[u]] % mod;
	} else if(zero[p[u]] == 1 && (zero[u] == 0 && fromBottom[u] + 1 == mod)) {
		fromUp[u] += fromBottom[p[u]] * fromUp[p[u]] % mod;
	}
	fromUp[u] %= mod;
	for(int v : g[u]) dfs1(v);
}

int main() {
	cin >> n;
	for(int i = 2; i <= n; i++) {
		cin >> p[i];
		g[p[i]].pb(i);
	}

	dfs(1);
	dfs1(1);

	VI res;
	for(int i = 1; i <= n; i++) {
		if(zero[i] > 0) res.pb(0);
		else res.pb(fromBottom[i] * fromUp[i] % mod);
	}
	for(int i : res) cout << i << " ";
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<LL>      VL;
typedef vector<string>  VS;
typedef vector<PII>     VPII;
#define MM(a,x)  memset(a,x,sizeof(a));
#define ALL(x)   (x).begin(), (x).end()
#define P(x)	 cerr<<"{"#x<<" = "<<(x)<<"}"<<endl;
#define P2(x,y)	 cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<"}"<<endl;
#define P3(x,y,z)cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<", "#z" = "<<(z)<<"}"<<endl;
#define PP(x,i)	 cerr<<"{"#x"["<<i<<"] = "<<x[i]<<"}"<<endl;
#define TM(a,b)	 cerr<<"{"#a" -> "#b": "<<1000*(b-a)/CLOCKS_PER_SEC<<"ms}\n";
#define UN(v)    sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0); cin.tie(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> T convert(string s) {T x; stringstream(s) >> x; return x;}
template<class T> string tostring(T x, int w = 0, char c = ' ') {stringstream ss; ss << x; string r(ss.str()); return string(max(0, w - (int)r.size()), c) + r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w); if(a != b && (!n || ++c % n)) cout << s; else cout << "\n";}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 211111;

LL exp(LL x, LL n, int m = mod) {LL r = 1 % m; for(x = (x % m + m) % m; n > 0; x = x * x % m, n >>= 1) if(n & 1) r = r * x % m; return r;}
int inv(LL a, int m = mod) {return exp(a, m - 2);}

VI g[N];
int p[N];

int n;
LL fromBottom[N];
LL fromUp[N];
int zero[N];

void dfs(int u) {
	for(int v : g[u]) dfs(v);
	fromBottom[u] = 1;
	for(int v : g[u]) {
		if(zero[v] == 0 && fromBottom[v] + 1 == mod) {
			zero[u]++;
		} else {
			if(zero[v] == 0) {
				fromBottom[u] = fromBottom[u] * (fromBottom[v] + 1) % mod;
			}
		}
	}
}

void dfs1(int u) {
	fromUp[u] = 1;
	if(zero[p[u]] == 0) {
		fromUp[u] += fromBottom[p[u]] * inv(fromBottom[u] + 1) % mod * fromUp[p[u]] % mod;
	} else if(zero[p[u]] == 1 && (zero[u] != 0 || fromBottom[u] + 1 == mod)) {
		fromUp[u] += fromBottom[p[u]] * fromUp[p[u]] % mod;
	}

	fromUp[u] %= mod;
	for(int v : g[u]) dfs1(v);
}

int main() {
	cin >> n;
	for(int i = 2; i <= n; i++) {
		cin >> p[i];
		g[p[i]].pb(i);
	}

	dfs(1);
	dfs1(1);

	VI res;
	for(int i = 1; i <= n; i++) {
		if(zero[i] > 0) res.pb(0);
		else res.pb(fromBottom[i] * fromUp[i] % mod);
	}
	for(int i : res) cout << i << " ";
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<LL>      VL;
typedef vector<string>  VS;
typedef vector<PII>     VPII;
#define MM(a,x)  memset(a,x,sizeof(a));
#define ALL(x)   (x).begin(), (x).end()
#define P(x)	 cerr<<"{"#x<<" = "<<(x)<<"}"<<endl;
#define P2(x,y)	 cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<"}"<<endl;
#define P3(x,y,z)cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<", "#z" = "<<(z)<<"}"<<endl;
#define PP(x,i)	 cerr<<"{"#x"["<<i<<"] = "<<x[i]<<"}"<<endl;
#define TM(a,b)	 cerr<<"{"#a" -> "#b": "<<1000*(b-a)/CLOCKS_PER_SEC<<"ms}\n";
#define UN(v)    sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0); cin.tie(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> T convert(string s) {T x; stringstream(s) >> x; return x;}
template<class T> string tostring(T x, int w = 0, char c = ' ') {stringstream ss; ss << x; string r(ss.str()); return string(max(0, w - (int)r.size()), c) + r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w); if(a != b && (!n || ++c % n)) cout << s; else cout << "\n";}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 211111;

LL exp(LL x, LL n, int m = mod) {LL r = 1 % m; for(x = (x % m + m) % m; n > 0; x = x * x % m, n >>= 1) if(n & 1) r = r * x % m; return r;}
int inv(LL a, int m = mod) {return exp(a, m - 2);}

VI g[N];
int p[N];

int n;
int fromBottom[N];
int fromUp[N];

void dfs(int u) {
	for(int v : g[u]) dfs(v);
	fromBottom[u] = 1;
	for(int v : g[u]) fromBottom[u] = fromBottom[u] * (fromBottom[v] + 1) % mod;
}

void dfs1(int u) {
	fromUp[u] = 1;
	fromUp[u] += fromBottom[p[u]] * inv(fromBottom[u] + 1) % mod * fromUp[p[u]] % mod;
	fromUp[u] %= mod;
	for(int v : g[u]) dfs1(v);
}

int main() {
	cin >> n;
	for(int i = 2; i <= n; i++) {
		cin >> p[i];
		g[p[i]].pb(i);
	}

	dfs(1);
	dfs1(1);

	VI res;
	for(int i = 1; i <= n; i++) res.pb((LL) fromBottom[i] * fromUp[i] % mod);
	PV(ALL(res));
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<LL>      VL;
typedef vector<string>  VS;
typedef vector<PII>     VPII;
#define MM(a,x)  memset(a,x,sizeof(a));
#define ALL(x)   (x).begin(), (x).end()
#define P(x)	 cerr<<"{"#x<<" = "<<(x)<<"}"<<endl;
#define P2(x,y)	 cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<"}"<<endl;
#define P3(x,y,z)cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<", "#z" = "<<(z)<<"}"<<endl;
#define PP(x,i)	 cerr<<"{"#x"["<<i<<"] = "<<x[i]<<"}"<<endl;
#define TM(a,b)	 cerr<<"{"#a" -> "#b": "<<1000*(b-a)/CLOCKS_PER_SEC<<"ms}\n";
#define UN(v)    sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0); cin.tie(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> T convert(string s) {T x; stringstream(s) >> x; return x;}
template<class T> string tostring(T x, int w = 0, char c = ' ') {stringstream ss; ss << x; string r(ss.str()); return string(max(0, w - (int)r.size()), c) + r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w); if(a != b && (!n || ++c % n)) cout << s; else cout << "\n";}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 211111;

LL exp(LL x, LL n, int m = mod) {LL r = 1 % m; for(x = (x % m + m) % m; n > 0; x = x * x % m, n >>= 1) if(n & 1) r = r * x % m; return r;}
int inv(LL a, int m = mod) {return exp(a, m - 2);}

VI g[N];
int p[N];

int n;
LL fromBottom[N];
LL fromUp[N];
int zero[N];

void dfs(int u) {
	for(int v : g[u]) dfs(v);
	fromBottom[u] = 1;
	for(int v : g[u]) {
		if(fromBottom[v] + 1 == mod) {
			zero[u]++;
		} else {
			if(zero[v] == 0) {
				fromBottom[u] = fromBottom[u] * (fromBottom[v] + 1) % mod;
			}
		}
	}
}

void dfs1(int u) {
	fromUp[u] = 1;
	if(zero[p[u]] == 0) {
		fromUp[u] += fromBottom[p[u]] * inv(fromBottom[u] + 1) % mod * fromUp[p[u]] % mod;
	} else if(zero[p[u]] == 1 && fromBottom[u] + 1 == mod) {
		fromUp[u] += fromBottom[p[u]] * fromUp[p[u]] % mod;
	} else {
		fromUp[u] = 0;
	}

	fromUp[u] %= mod;
	for(int v : g[u]) dfs1(v);
}

int main() {
	cin >> n;
	for(int i = 2; i <= n; i++) {
		cin >> p[i];
		g[p[i]].pb(i);
	}

	dfs(1);
	dfs1(1);

	VI res;
	for(int i = 1; i <= n; i++) {
		if(zero[i] > 0) res.pb(0);
		else res.pb(fromBottom[i] * fromUp[i] % mod);
	}
	for(int i : res) cout << i << " ";
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<LL>      VL;
typedef vector<string>  VS;
typedef vector<PII>     VPII;
#define MM(a,x)  memset(a,x,sizeof(a));
#define ALL(x)   (x).begin(), (x).end()
#define P(x)	 cerr<<"{"#x<<" = "<<(x)<<"}"<<endl;
#define P2(x,y)	 cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<"}"<<endl;
#define P3(x,y,z)cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<", "#z" = "<<(z)<<"}"<<endl;
#define PP(x,i)	 cerr<<"{"#x"["<<i<<"] = "<<x[i]<<"}"<<endl;
#define TM(a,b)	 cerr<<"{"#a" -> "#b": "<<1000*(b-a)/CLOCKS_PER_SEC<<"ms}\n";
#define UN(v)    sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0); cin.tie(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> T convert(string s) {T x; stringstream(s) >> x; return x;}
template<class T> string tostring(T x, int w = 0, char c = ' ') {stringstream ss; ss << x; string r(ss.str()); return string(max(0, w - (int)r.size()), c) + r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w); if(a != b && (!n || ++c % n)) cout << s; else cout << "\n";}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 211111;

LL exp(LL x, LL n, int m = mod) {LL r = 1 % m; for(x = (x % m + m) % m; n > 0; x = x * x % m, n >>= 1) if(n & 1) r = r * x % m; return r;}
int inv(LL a, int m = mod) {return exp(a, m - 2);}

VI g[N];
int p[N];

int n;
LL fromBottom[N];
LL fromUp[N];
int zero[N];

void dfs(int u) {
	for(int v : g[u]) dfs(v);
	fromBottom[u] = 1;
	for(int v : g[u]) {
		if(zero[v] > 0 || fromBottom[v] + 1 == mod) {
			zero[u]++;
		} else {
			if(zero[v] == 0) {
				fromBottom[u] = fromBottom[u] * (fromBottom[v] + 1) % mod;
			}
		}
	}
}

void dfs1(int u) {
	fromUp[u] = 1;
	if(zero[p[u]] == 0) {
		fromUp[u] += fromBottom[p[u]] * inv(fromBottom[u] + 1) % mod * fromUp[p[u]] % mod;
	} else if(zero[p[u]] == 1 && fromBottom[u] + 1 == mod) {
		fromUp[u] += fromBottom[p[u]] * fromUp[p[u]] % mod;
	}
	fromUp[u] %= mod;
	for(int v : g[u]) dfs1(v);
}

int main() {
	cin >> n;
	for(int i = 2; i <= n; i++) {
		cin >> p[i];
		g[p[i]].pb(i);
	}

	dfs(1);
	dfs1(1);

	VI res;
	for(int i = 1; i <= n; i++) {
		if(zero[i] > 0) res.pb(0);
		else res.pb(fromBottom[i] * fromUp[i] % mod);
	}
	for(int i : res) cout << i << " ";
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<LL>      VL;
typedef vector<string>  VS;
typedef vector<PII>     VPII;
#define MM(a,x)  memset(a,x,sizeof(a));
#define ALL(x)   (x).begin(), (x).end()
#define P(x)	 cerr<<"{"#x<<" = "<<(x)<<"}"<<endl;
#define P2(x,y)	 cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<"}"<<endl;
#define P3(x,y,z)cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<", "#z" = "<<(z)<<"}"<<endl;
#define PP(x,i)	 cerr<<"{"#x"["<<i<<"] = "<<x[i]<<"}"<<endl;
#define TM(a,b)	 cerr<<"{"#a" -> "#b": "<<1000*(b-a)/CLOCKS_PER_SEC<<"ms}\n";
#define UN(v)    sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0); cin.tie(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> T convert(string s) {T x; stringstream(s) >> x; return x;}
template<class T> string tostring(T x, int w = 0, char c = ' ') {stringstream ss; ss << x; string r(ss.str()); return string(max(0, w - (int)r.size()), c) + r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w); if(a != b && (!n || ++c % n)) cout << s; else cout << "\n";}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 211111;

LL exp(LL x, LL n, int m = mod) {LL r = 1 % m; for(x = (x % m + m) % m; n > 0; x = x * x % m, n >>= 1) if(n & 1) r = r * x % m; return r;}
int inv(LL a, int m = mod) {return exp(a, m - 2);}

VI g[N];
int p[N];

int n;
LL fromBottom[N];
LL fromUp[N];
VI zero[N];

void dfs(int u) {
	for(int v : g[u]) dfs(v);
	fromBottom[u] = 1;
	for(int v : g[u]) {
		if(fromBottom[v] + 1 == mod) {
			zero[u].pb(v);
		} else {
			fromBottom[u] = fromBottom[u] * (fromBottom[v] + 1) % mod;
		}
	}
}

void dfs1(int u) {
	fromUp[u] = 1;
	if(zero[p[u]].size() < 2) {
		if(fromBottom[u] + 1 != mod) {
			fromUp[u] += fromBottom[p[u]] * inv(fromBottom[u] + 1) % mod * fromUp[p[u]] % mod;
		} else {
			fromUp[u] += fromBottom[p[u]] * fromUp[p[u]] % mod;
		}
	}
	fromUp[u] %= mod;
	for(int v : g[u]) dfs1(v);
}

int main() {
	cin >> n;
	for(int i = 2; i <= n; i++) {
		cin >> p[i];
		g[p[i]].pb(i);
	}

	dfs(1);
	dfs1(1);

	VI res;
	for(int i = 1; i <= n; i++) res.pb(fromBottom[i] * fromUp[i] % mod);
	for(int i : res) cout << i << " ";
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long       LL;
typedef pair<int, int>  PII;
typedef vector<int>     VI;
typedef vector<LL>      VL;
typedef vector<string>  VS;
typedef vector<PII>     VPII;
#define MM(a,x)  memset(a,x,sizeof(a));
#define ALL(x)   (x).begin(), (x).end()
#define P(x)	 cerr<<"{"#x<<" = "<<(x)<<"}"<<endl;
#define P2(x,y)	 cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<"}"<<endl;
#define P3(x,y,z)cerr<<"{"#x" = "<<(x)<<", "#y" = "<<(y)<<", "#z" = "<<(z)<<"}"<<endl;
#define PP(x,i)	 cerr<<"{"#x"["<<i<<"] = "<<x[i]<<"}"<<endl;
#define TM(a,b)	 cerr<<"{"#a" -> "#b": "<<1000*(b-a)/CLOCKS_PER_SEC<<"ms}\n";
#define UN(v)    sort(ALL(v)), v.resize(unique(ALL(v))-v.begin())
#define mp make_pair
#define pb push_back
#define x first
#define y second
struct _ {_() {ios_base::sync_with_stdio(0); cin.tie(0);}} _;
template<class A, class B> ostream& operator<<(ostream &o, pair<A, B> t) {o << "(" << t.x << ", " << t.y << ")"; return o;}
template<class T> T convert(string s) {T x; stringstream(s) >> x; return x;}
template<class T> string tostring(T x, int w = 0, char c = ' ') {stringstream ss; ss << x; string r(ss.str()); return string(max(0, w - (int)r.size()), c) + r;}
template<class T> void PV(T a, T b, int n = 0, int w = 0, string s = " ") {int c = 0; while(a != b) {cout << tostring(*a++, w); if(a != b && (!n || ++c % n)) cout << s; else cout << "\n";}}
template<class T> inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T> inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const LL linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 211111;

LL exp(LL x, LL n, int m = mod) {LL r = 1 % m; for(x = (x % m + m) % m; n > 0; x = x * x % m, n >>= 1) if(n & 1) r = r * x % m; return r;}
int inv(LL a, int m = mod) {return exp(a, m - 2);}

VI g[N];
int p[N];

int n;
LL fromBottom[N];
LL fromUp[N];
int zero[N];

void dfs(int u) {
	for(int v : g[u]) dfs(v);
	fromBottom[u] = 1;
	for(int v : g[u]) {
		if(fromBottom[v] + 1 == mod) {
			zero[u]++;
		} else {
			fromBottom[u] = fromBottom[u] * (fromBottom[v] + 1) % mod;
		}
	}
}

void dfs1(int u) {
	if(p[u] != 0) fromUp[u] = 1;
	if(zero[p[u]] == 0) {
		fromUp[u] += fromBottom[p[u]] * inv(fromBottom[u] + 1) % mod * fromUp[p[u]] % mod;
	} else if(zero[p[u]] == 1 && fromBottom[u] + 1 == mod) {
		fromUp[u] += fromBottom[p[u]] * fromUp[p[u]] % mod;
	}
	fromUp[u] %= mod;
	for(int v : g[u]) dfs1(v);
}

int main() {
	cin >> n;
	for(int i = 2; i <= n; i++) {
		cin >> p[i];
		g[p[i]].pb(i);
	}

	dfs(1);
	dfs1(1);

	VI res;
	for(int i = 1; i <= n; i++) {
		if(zero[i] > 0) res.pb(0);
		else res.pb(fromBottom[i] * fromUp[i] % mod);
	}
	for(int i : res) cout << i << " ";
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>

#define ll long long
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
#define sz size()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(s) s.begin(), s.end()
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
using namespace std;

const int maxn = (int)1e6 + 100;
const int mod = (int)1e9 + 7;

inline ll getint() {
  char _c = getchar();
  ll _x = 0, _s = 1;
  while (_c <= 32) _c = getchar();
  if (_c == '-') _s = -1, _c = getchar();
  while ('0' <= _c && _c <= '9') _x = _x * 10 + _c - '0', _c = getchar();
  return _x * _s;
}

int n, m, p[maxn], up[maxn][20], dp[maxn][20], ssz[maxn], lvl[maxn], u[maxn], v[maxn], w[maxn];
ll cost;
map<pair<int, int>, int> used;
vector<pair<int, pair<int, int> > > t;
vector<pair<int, int> > g[maxn];
inline void make (int v) {
	p[v] = v;
	ssz[v] = 0;
}

int find (int v) {
	if (v == p[v])
		return v;
	return p[v] = find(p[v]);
}

inline void add (int a, int b) {
	a = find (a);
	b = find(b);
	if (a != b) {
		if (ssz[a] < ssz[b])
			swap (a, b);
		p[b] = a;
		if (ssz[a] == ssz[b])
			++ssz[a];
	}
}
inline void build(){
	sort(all(t));
	for(int i = 1; i <= n; i++)
		make(i);
	for(int i = 0; i < t.sz; i++){
		int u = t[i].S.F, v = t[i].S.S, w = t[i].F;
		if(find(u) != find(v)){
			cost += (w * 1ll);
			g[u].pb(mp(v, w));
			g[v].pb(mp(u, w));
			add(u, v);
		}
	}
}
void dfs(int v, int p, int len) {
  if (p == -1) {
  	lvl[v] = 1;
    for (int i = 0; i < 20; i++)
      up[v][i] = v;
  }
  else{
  	lvl[v] = lvl[p] + 1;
    up[v][0] = p;
		dp[v][0] = len;
    for (int i = 1; i < 20; i++){
      up[v][i] = up[up[v][i - 1]][i - 1];
      dp[v][i] = max(dp[v][i - 1], dp[up[v][i - 1]][i - 1]);
   	}
  }
  for (int i = 0; i < g[v].sz; i++) {
  	int to = g[v][i].F, l = g[v][i].S;
  	if (to != p) {
      dfs(to, v, l);
    }
  }
}
inline int LCA(int u, int v){
	if (lvl[u] > lvl[v])
    swap(v, u);
  for (int i = 19; i >= 0; i--) {
    if (lvl[up[v][i]] >= lvl[u])
      v = up[v][i];
  }
  if(u == v)
  	return v;
  for (int i = 19; i >= 0; i--) {
    if (up[u][i] != up[v][i]){
      u = up[u][i];
      v = up[v][i];
    }
  }
  return up[u][0];
}
inline int getmax(int u, int v){
	if(u == v)
		return 0;
	int mx = 0;
	for(int i = 19; i >= 0; i--){
		if(lvl[up[u][i]] >= lvl[v]){
			mx = max(mx, dp[u][i]);
			u = up[u][i];
		}
	}
	return mx;
}
int main () {
	ios;
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		cin >> u[i] >> v[i] >> w[i];
		t.pb(mp(w[i], mp(u[i], v[i])));
	}
	build();
	dfs(1, -1, 0);
	for(int i = 1; i <= m; i++){
			int lca = LCA(u[i], v[i]);
			int mx1 = getmax(u[i], lca), mx2 = getmax(v[i], lca);
			cout << cost - max(mx1, mx2) + w[i] << endl;

	}
}


#include<bits/stdc++.h>

#define ll long long
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
#define sz size()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(s) s.begin(), s.end()
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
using namespace std;

const int maxn = (int)1e6 + 100;
const int mod = (int)1e9 + 7;

inline ll getint() {
  char _c = getchar();
  ll _x = 0, _s = 1;
  while (_c <= 32) _c = getchar();
  if (_c == '-') _s = -1, _c = getchar();
  while ('0' <= _c && _c <= '9') _x = _x * 10 + _c - '0', _c = getchar();
  return _x * _s;
}

int n, m, p[maxn], up[maxn][20], dp[maxn][20], lvl[maxn], u[maxn], v[maxn], w[maxn];
ll cost;
map<pair<int, int>, int> used;
vector<pair<int, pair<int, int> > > t;
vector<pair<int, int> > g[maxn];
inline void make(int v) {
	p[v] = v;
}

int find(int v) {
	if (v == p[v])
		return v;
	return find (p[v]);
}

inline void add(int a, int b) {
	a = find(a);
	b = find(b);
	if (a != b)
		p[b] = a;
}
inline void build(){
	sort(all(t));
	for(int i = 1; i <= n; i++)
		make(i);
	for(int i = 0; i < t.sz; i++){
		int u = t[i].S.F, v = t[i].S.S, w = t[i].F;
		if(find(u) != find(v)){
			cost += (w * 1ll);
			g[u].pb(mp(v, w));
			g[v].pb(mp(u, w));
			add(u, v);
		}
	}
}
void dfs(int v, int p, int len) {
  if (p == -1) {
  	lvl[v] = 1;
    for (int i = 0; i < 20; i++)
      up[v][i] = v;
  }
  else{
  	lvl[v] = lvl[p] + 1;
    up[v][0] = p;
		dp[v][0] = len;
    for (int i = 1; i < 20; i++){
      up[v][i] = up[up[v][i - 1]][i - 1];
      dp[v][i] = max(dp[v][i - 1], dp[up[v][i - 1]][i - 1]);
   	}
  }
  for (int i = 0; i < g[v].sz; i++) {
  	int to = g[v][i].F, l = g[v][i].S;
  	if (to != p) {
      dfs(to, v, l);
    }
  }
}
inline int LCA(int u, int v){
	if (lvl[u] > lvl[v])
    swap(v, u);
  for (int i = 19; i >= 0; i--) {
    if (lvl[up[v][i]] >= lvl[u])
      v = up[v][i];
  }
  if(u == v)
  	return v;
  for (int i = 19; i >= 0; i--) {
    if (up[u][i] != up[v][i]){
      u = up[u][i];
      v = up[v][i];
    }
  }
  return up[u][0];
}
inline int getmax(int u, int v){
	if(u == v)
		return 0;
	int mx = 0;
	for(int i = 19; i >= 0; i--){
		if(lvl[up[u][i]] >= lvl[v]){
			mx = max(mx, dp[u][i]);
			u = up[u][i];
		}
	}
	return mx;
}
int main () {
	ios;
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		cin >> u[i] >> v[i] >> w[i];
		t.pb(mp(w[i], mp(u[i], v[i])));
	}
	build();
	dfs(1, -1, 0);
	for(int i = 1; i <= m; i++){
			int lca = LCA(u[i], v[i]);
			int mx1 = getmax(u[i], lca), mx2 = getmax(v[i], lca);
			cout << cost - max(mx1, mx2) + w[i] << endl;

	}
}


#include<bits/stdc++.h>

#define ll long long
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
#define sz size()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(s) s.begin(), s.end()
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
using namespace std;

const int maxn = (int)1e6 + 100;
const int mod = (int)1e9 + 7;

inline ll getint() {
  char _c = getchar();
  ll _x = 0, _s = 1;
  while (_c <= 32) _c = getchar();
  if (_c == '-') _s = -1, _c = getchar();
  while ('0' <= _c && _c <= '9') _x = _x * 10 + _c - '0', _c = getchar();
  return _x * _s;
}

int n, m, p[maxn], up[maxn][20], dp[maxn][20], lvl[maxn], u[maxn], v[maxn], w[maxn];
ll cost;
map<pair<int, int>, int> used;
vector<pair<int, pair<int, int> > > t;
vector<pair<int, int> > g[maxn];
inline void make(int v) {
	p[v] = v;
}

int find(int v) {
	if (v == p[v])
		return v;
	return find (p[v]);
}

inline void add(int a, int b) {
	a = find(a);
	b = find(b);
	if (a != b)
		p[b] = a;
}
inline void build(){
	sort(all(t));
	for(int i = 1; i <= n; i++)
		make(i);
	for(int i = 0; i < t.sz; i++){
		int u = t[i].S.F, v = t[i].S.S, w = t[i].F;
		if(find(u) != find(v)){
			cost += (w * 1ll);
			g[u].pb(mp(v, w));
			g[v].pb(mp(u, w));
			used[mp(u, v)] = 1;
			add(u, v);
		}
	}
}
void dfs(int v, int p, int len) {
  if (p == -1) {
  	lvl[v] = 1;
    for (int i = 0; i < 20; i++)
      up[v][i] = v;
  }
  else{
  	lvl[v] = lvl[p] + 1;
    up[v][0] = p;
		dp[v][0] = len;
    for (int i = 1; i < 20; i++){
      up[v][i] = up[up[v][i - 1]][i - 1];
      dp[v][i] = max(dp[v][i - 1], dp[up[v][i - 1]][i - 1]);
   	}
  }
  for (int i = 0; i < g[v].sz; i++) {
  	int to = g[v][i].F, l = g[v][i].S;
  	if (to != p) {
      dfs(to, v, l);
    }
  }
}
inline int LCA(int u, int v){
	if (lvl[u] > lvl[v])
    swap(v, u);
  for (int i = 19; i >= 0; i--) {
    if (lvl[up[v][i]] >= lvl[u])
      v = up[v][i];
  }
  if(u == v)
  	return v;
  for (int i = 19; i >= 0; i--) {
    if (up[u][i] != up[v][i]){
      u = up[u][i];
      v = up[v][i];
    }
  }
  return up[u][0];
}
inline int getmax(int u, int v){
	if(u == v)
		return 0;
	int mx = 0;
	for(int i = 19; i >= 0; i--){
		if(lvl[up[u][i]] >= lvl[v]){
			mx = max(mx, dp[u][i]);
			u = up[u][i];
		}
	}
	return mx;
}
int main () {
	ios;
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		cin >> u[i] >> v[i] >> w[i];
		t.pb(mp(w[i], mp(u[i], v[i])));
	}
	build();
	dfs(1, -1, 0);
	for(int i = 1; i <= m; i++){
		if(used[mp(u[i], v[i])]){
			cout << cost << endl;
		}else{
			int lca = LCA(u[i], v[i]);
			int mx1 = getmax(u[i], lca), mx2 = getmax(v[i], lca);
			cout << cost - max(mx1, mx2) + w[i] << endl;
		}
	}
}


#include<bits/stdc++.h>

#define ll long long
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
#define sz size()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(s) s.begin(), s.end()
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
using namespace std;

const int maxn = (int)2e6 + 100;
const int mod = (int)1e9 + 7;

inline ll getint() {
  char _c = getchar();
  ll _x = 0, _s = 1;
  while (_c <= 32) _c = getchar();
  if (_c == '-') _s = -1, _c = getchar();
  while ('0' <= _c && _c <= '9') _x = _x * 10 + _c - '0', _c = getchar();
  return _x * _s;
}

int n, m, p[maxn], up[maxn][20], dp[maxn][20], ssz[maxn], lvl[maxn], u[maxn], v[maxn], w[maxn];
ll cost;
map<pair<int, int>, int> used;
vector<pair<int, pair<int, int> > > t;
vector<pair<int, int> > g[maxn];
inline void make (int v) {
	p[v] = v;
	ssz[v] = 0;
}

int find (int v) {
	if (v == p[v])
		return v;
	return p[v] = find(p[v]);
}

inline void add (int a, int b) {
	a = find (a);
	b = find(b);
	if (a != b) {
		if (ssz[a] < ssz[b])
			swap (a, b);
		p[b] = a;
		if (ssz[a] == ssz[b])
			++ssz[a];
	}
}
inline void build(){
	sort(all(t));
	for(int i = 1; i <= n; i++)
		make(i);
	for(int i = 0; i < t.sz; i++){
		int u = t[i].S.F, v = t[i].S.S, w = t[i].F;
		if(find(u) != find(v)){
			cost += (w * 1ll);
			g[u].pb(mp(v, w));
			g[v].pb(mp(u, w));
			add(u, v);
		}
	}
}
void dfs(int v, int p, int len) {
  if (p == -1) {
  	lvl[v] = 1;
    for (int i = 0; i < 20; i++)
      up[v][i] = v;
  }
  else{
  	lvl[v] = lvl[p] + 1;
    up[v][0] = p;
		dp[v][0] = len;
    for (int i = 1; i < 20; i++){
      up[v][i] = up[up[v][i - 1]][i - 1];
      dp[v][i] = max(dp[v][i - 1], dp[up[v][i - 1]][i - 1]);
   	}
  }
  for (int i = 0; i < g[v].sz; i++) {
  	int to = g[v][i].F, l = g[v][i].S;
  	if (to != p) {
      dfs(to, v, l);
    }
  }
}
inline int LCA(int u, int v){
	if (lvl[u] > lvl[v])
    swap(v, u);
  for (int i = 19; i >= 0; i--) {
    if (lvl[up[v][i]] >= lvl[u])
      v = up[v][i];
  }
  if(u == v)
  	return v;
  for (int i = 19; i >= 0; i--) {
    if (up[u][i] != up[v][i]){
      u = up[u][i];
      v = up[v][i];
    }
  }
  return up[u][0];
}
inline int getmax(int u, int v){
	if(u == v)
		return 0;
	int mx = 0;
	for(int i = 19; i >= 0; i--){
		if(lvl[up[u][i]] >= lvl[v]){
			mx = max(mx, dp[u][i]);
			u = up[u][i];
		}
	}
	return mx;
}
int main () {
	ios;
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		cin >> u[i] >> v[i] >> w[i];
		t.pb(mp(w[i], mp(u[i], v[i])));
	}
	build();
	dfs(1, -1, 0);
	for(int i = 1; i <= m; i++){
			int lca = LCA(u[i], v[i]);
			int mx1 = getmax(u[i], lca), mx2 = getmax(v[i], lca);
			cout << cost - max(mx1, mx2) + w[i] << endl;

	}
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define rfor(i,ii,iii) for(int i=ii-1;i>=iii;i--)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

typedef pair<ll,pair<int,int>> edge;
/*
struct edge {
  ll cst;
  int u,v,id;
  edge(int u,int v,ll cst,int id):u(u),v(v),cst(cst),id(id){}
  bool operator<(const edge &b)const{return cst<b.cst;}
};
*/
vector<edge>edges,e2;
vector<par>g[N];

int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    int u=it.snd.fst,v=it.snd.snd;
    if(join(u,v)) {
      g[u].pb({it.fst,v});
      g[v].pb({it.fst,u});
      res+=it.fst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,n)if(pa[i-1][x]!=-1){
    pa[i][x]=pa[i-1][pa[i-1][x]];
    c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  rfor(i,K,0)if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  if(x==y)return res;
  rfor(i,K,0)if(pa[i][x]!=pa[i][y]){
    res=max({res,c[i][x],c[i][y]});
    x=pa[i][x],y=pa[i][y];
  }
  res=max({res,c[0][x],c[0][y]});
  return res;
}

int main() {
  auto cl=clock();
  auto timer=cl;
  scanf("%d%d",&n,&m);
  fore(i,0,m) {
    int u,v;ll w;
    scanf("%d%d%lld",&u,&v,&w);u--,v--;
    edges.pb({w,{u,v}});
    e2.pb({w,{u,v}});
  }
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>1.6) {
    printf("DEMETRIO 1\n");
    return 0;
  }
  fore(i,0,n)ufPa[i]=i;
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>1.6) {
    printf("DEMETRIO 2\n");
    return 0;
  }
  t=kruskal();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>1.6) {
    printf("DEMETRIO 3\n");
    return 0;
  }
  lca_init();
  // debug();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>1.6) {
    printf("DEMETRIO 4\n");
    return 0;
  }
  for(auto it:e2){
    printf("%lld\n",t+it.fst-query(it.snd.fst,it.snd.snd));
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>1.6) {
      printf("DEMETRIO 5\n");
      return 0;
    }
  }
  return 0;
  // printf("%f\n",(clock()-cl)*1./CLOCKS_PER_SEC);
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

typedef pair<ll,pair<int,int>> edge;
vector<edge>edges,e2;
vector<par>g[N];
double cl,timer;
int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>1.5) {
      printf("4FANTASTICOS\n");
      exit(0);
    }
    int u=it.snd.fst,v=it.snd.snd;
    if(join(u,v)) {
      g[u].pb({it.fst,v});
      g[v].pb({it.fst,u});
      res+=it.fst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,n)if(pa[i-1][x]!=-1){
    pa[i][x]=pa[i-1][pa[i-1][x]];
    c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  for(int i=K-1;i>=0;i--)if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  if(x==y)return res;
  for(int i=K-1;i>=0;i--)if(pa[i][x]!=pa[i][y]){
    res=max({res,c[i][x],c[i][y]});
    x=pa[i][x],y=pa[i][y];
  }
  res=max({res,c[0][x],c[0][y]});
  return res;
}

int main() {
  cl=clock();
  scanf("%d%d",&n,&m);
  fore(i,0,m) {
    int u,v;ll w;
    scanf("%d%d%lld",&u,&v,&w);u--,v--;
    edges.pb({w,{u,v}});
    e2.pb({w,{u,v}});
  }
  fore(i,0,n)ufPa[i]=i,ufSz[i]=1;
  t=kruskal();
  lca_init();
  // debug();
  for(auto it:e2)printf("%lld\n",t+it.fst-query(it.snd.fst,it.snd.snd));
  // printf("%f\n",(clock()-cl)*1./CLOCKS_PER_SEC);
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

typedef pair<ll,pair<int,int>> edge;
vector<edge>edges,e2;
vector<par>g[N];
double cl,timer;
int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    int u=it.snd.fst,v=it.snd.snd;
    if(join(u,v)) {
      g[u].pb({it.fst,v});
      g[v].pb({it.fst,u});
      res+=it.fst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,n){
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 9\n");
      exit(0);
    }
    if(pa[i-1][x]!=-1){
      pa[i][x]=pa[i-1][pa[i-1][x]];
      c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
    }
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 7\n");
      exit(0);
    }
    assert(x>=0 && y>=0);
    if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  }
  if(x==y)return res;
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 8\n");
      exit(0);
    }
    if(pa[i][x]!=pa[i][y]){
      res=max({res,c[i][x],c[i][y]});
      assert(x>=0 && y>=0);
      x=pa[i][x],y=pa[i][y];
    }
  }
  assert(x>=0 && y>=0);
  res=max({res,c[0][x],c[0][y]});
  return res;
}

int main() {
  cl=clock();
  scanf("%d%d",&n,&m);
  fore(i,0,m) {
    int u,v;ll w;
    scanf("%d%d%lld",&u,&v,&w);u--,v--;
    edges.pb({w,{u,v}});
    e2.pb({w,{u,v}});
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 6\n");
      return 0;
    }
  }
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 1\n");
    return 0;
  }
  fore(i,0,n)ufPa[i]=i;
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 2\n");
    return 0;
  }
  t=kruskal();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 3\n");
    return 0;
  }
  lca_init();
  // debug();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.01) {
    printf("DEMETRIO 4\n");
    return 0;
  }
  for(auto it:e2){
    printf("%lld\n",t+it.fst-query(it.snd.fst,it.snd.snd));
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 5\n");
      return 0;
    }
  }
  return 0;
  // printf("%f\n",(clock()-cl)*1./CLOCKS_PER_SEC);
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define rfor(i,ii,iii) for(int i=ii-1;i>=iii;i--)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

struct edge {
  ll cst;
  int u,v,id;
  bool in=0;
  edge(int u,int v,ll cst,int id):u(u),v(v),cst(cst),id(id){}
  bool operator<(const edge &b)const{return cst<b.cst;}
};
vector<edge>edges;
vector<par>g[N];

int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    if(join(it.u,it.v)) {
      g[it.u].pb({it.cst,it.v});
      g[it.v].pb({it.cst,it.u});
      if(it.u>it.v)swap(it.u,it.v);
      (&it)->in=1;
      res+=it.cst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,N)if(pa[i-1][x]!=-1){
    pa[i][x]=pa[i-1][pa[i-1][x]];
    if(c[i-1][pa[i-1][x]])c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  rfor(i,K,0)if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  if(x==y)return res;
  rfor(i,K,0)if(pa[i][x]!=pa[i][y]){
    res=max({res,c[i][x],c[i][y]});
    x=pa[i][x],y=pa[i][y];
  }
  res=max({res,c[0][x],c[0][y]});
  return res;
}
void debug() {
  cout<<"pa: "<<endl;
  fore(i,0,n){cout<<i+1<<" : ";fore(j,0,n)cout<<pa[j][i]<<" ";cout<<endl;}
  cout<<"cost: "<<endl;
  fore(i,0,n){cout<<i+1<<" : ";fore(j,0,n)cout<<c[j][i]<<" ";cout<<endl;}
  cout<<"t: "<<t<<endl;
  fore(i,0,n){cout<<i+1<<" : ";for(auto j:g[i])cout<<"{"<<j.snd+1<<","<<j.fst<<"} ";cout<<endl;}
  cout<<"edges: "<<endl;for(auto it:edges)cout<<it.u<<" "<<it.v<<" "<<it.cst<<" "<<it.id<<" "<<it.in<<endl;
}

int main() {
  // auto cl=clock();freopen("out","w",stdout);
  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  cin>>n>>m;
  fore(i,0,m) {
    int u,v;ll w;
    cin>>u>>v>>w;u--,v--;
    edges.pb(edge(u,v,w,i));
  }
  fore(i,0,N)ufPa[i]=i;
  t=kruskal();
  lca_init();
  // debug();
  for(auto it:edges) {
    int x=it.u,y=it.v;
    if(x>y)swap(x,y);
    ll res=t;
    if(!it.in) {
      res+=it.cst;
      res-=query(x,y);
    }
    // cout<<"{"<<it.snd.fst+1<<","<<it.snd.snd+1<<"} ("<<it.fst<<"): "<<t<<" "<<mx<<" : "<<res<<" "<<res<<endl;
    r[it.id]=res;
  }
  fore(i,0,m)cout<<r[i]<<"\n";
  // cout<<"time: "<<(clock()-cl)*1./CLOCKS_PER_SEC<<endl;
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

typedef pair<ll,pair<int,int>> edge;
vector<edge>edges,e2;
vector<par>g[N];
double cl,timer;
int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    int u=it.snd.fst,v=it.snd.snd;
    if(join(u,v)) {
      g[u].pb({it.fst,v});
      g[v].pb({it.fst,u});
      res+=it.fst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,n){
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 9\n");
      exit(0);
    }
    if(pa[i-1][x]!=-1){
      pa[i][x]=pa[i-1][pa[i-1][x]];
      c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
    }
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 7\n");
      exit(0);
    }
    assert(x>=0 && y>=0);
    if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  }
  if(x==y)return res;
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 8\n");
      exit(0);
    }
    if(pa[i][x]!=pa[i][y]){
      res=max({res,c[i][x],c[i][y]});
      assert(x>=0 && y>=0);
      x=pa[i][x],y=pa[i][y];
    }
  }
  assert(x>=0 && y>=0);
  res=max({res,c[0][x],c[0][y]});
  return res;
}

int main() {
  cl=clock();
  scanf("%d%d",&n,&m);
  fore(i,0,m) {
    int u,v;ll w;
    scanf("%d%d%lld",&u,&v,&w);u--,v--;
    edges.pb({w,{u,v}});
    e2.pb({w,{u,v}});
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 6\n");
      return 0;
    }
  }
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 1\n");
    return 0;
  }
  fore(i,0,n)ufPa[i]=i;
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 2\n");
    return 0;
  }
  t=kruskal();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 3\n");
    return 0;
  }
  lca_init();
  // debug();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.001) {
    printf("DEMETRIO 4\n");
    return 0;
  }
  for(auto it:e2){
    printf("%lld\n",t+it.fst-query(it.snd.fst,it.snd.snd));
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 5\n");
      return 0;
    }
  }
  return 0;
  // printf("%f\n",(clock()-cl)*1./CLOCKS_PER_SEC);
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define rfor(i,ii,iii) for(int i=ii-1;i>=iii;i--)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

struct edge {
  ll cst;
  int u,v,id;
  edge(int u,int v,ll cst,int id):u(u),v(v),cst(cst),id(id){}
  bool operator<(const edge &b)const{return cst<b.cst;}
};
vector<edge>edges;
vector<par>g[N];

int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    if(join(it.u,it.v)) {
      g[it.u].pb({it.cst,it.v});
      g[it.v].pb({it.cst,it.u});
      res+=it.cst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,n)if(pa[i-1][x]!=-1){
    pa[i][x]=pa[i-1][pa[i-1][x]];
    if(c[i-1][pa[i-1][x]])c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  rfor(i,K,0)if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  if(x==y)return res;
  rfor(i,K,0)if(pa[i][x]!=pa[i][y]){
    res=max({res,c[i][x],c[i][y]});
    x=pa[i][x],y=pa[i][y];
  }
  res=max({res,c[0][x],c[0][y]});
  return res;
}

int main() {
  // auto cl=clock();freopen("out","w",stdout);
  // ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  scanf("%d%d",&n,&m);
  // cin>>n>>m;
  fore(i,0,m) {
    int u,v;ll w;
    scanf("%d%d%lld",&u,&v,&w);u--,v--;
    // cin>>u>>v>>w;u--,v--;
    edges.pb(edge(u,v,w,i));
  }
  fore(i,0,N)ufPa[i]=i;
  t=kruskal();
  lca_init();
  // debug();
  for(auto it:edges)r[it.id]=t+it.cst-query(it.u,it.v);
  fore(i,0,m)printf("%lld\n",r[i]);
  // fore(i,0,m)cout<<r[i]<<"\n";
  // printf("%f\n",(clock()-cl)*1./CLOCKS_PER_SEC);
  // cout<<"time: "<<(clock()-cl)*1./CLOCKS_PER_SEC<<endl;
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

typedef pair<ll,pair<int,int>> edge;
vector<edge>edges,e2;
vector<par>g[N];
double cl,timer;
int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    int u=it.snd.fst,v=it.snd.snd;
    if(join(u,v)) {
      g[u].pb({it.fst,v});
      g[v].pb({it.fst,u});
      res+=it.fst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,n){
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.1) {
      printf("DEMETRIO 9\n");
      exit(0);
    }
    if(pa[i-1][x]!=-1){
      pa[i][x]=pa[i-1][pa[i-1][x]];
      c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
    }
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.1) {
      printf("DEMETRIO 7\n");
      exit(0);
    }
    assert(x>=0 && y>=0);
    if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  }
  if(x==y)return res;
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.1) {
      printf("DEMETRIO 8\n");
      exit(0);
    }
    if(pa[i][x]!=pa[i][y]){
      res=max({res,c[i][x],c[i][y]});
      assert(x>=0 && y>=0);
      x=pa[i][x],y=pa[i][y];
    }
  }
  assert(x>=0 && y>=0);
  res=max({res,c[0][x],c[0][y]});
  return res;
}

int main() {
  cl=clock();
  scanf("%d%d",&n,&m);
  fore(i,0,m) {
    int u,v;ll w;
    scanf("%d%d%lld",&u,&v,&w);u--,v--;
    edges.pb({w,{u,v}});
    e2.pb({w,{u,v}});
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 6\n");
      return 0;
    }
  }
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 1\n");
    return 0;
  }
  fore(i,0,n)ufPa[i]=i;
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.1) {
    printf("DEMETRIO 2\n");
    return 0;
  }
  t=kruskal();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.1) {
    printf("DEMETRIO 3\n");
    return 0;
  }
  lca_init();
  // debug();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.1) {
    printf("DEMETRIO 4\n");
    return 0;
  }
  assert(e2.size()==m);
  for(auto it:e2){
    printf("%lld\n",t+it.fst-query(it.snd.fst,it.snd.snd));
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.1) {
      printf("DEMETRIO 5\n");
      return 0;
    }
  }
  return 0;
  // printf("%f\n",(clock()-cl)*1./CLOCKS_PER_SEC);
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define rfor(i,ii,iii) for(int i=ii-1;i>=iii;i--)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=19,N=1<<19;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

struct edge {
  ll cst;
  int u,v,id;
  edge(int u,int v,ll cst,int id):u(u),v(v),cst(cst),id(id){}
  bool operator<(const edge &b)const{return cst<b.cst;}
};
vector<edge>edges;
set<pair<int,int>>mst;
vector<par>g[N];

int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    if(join(it.u,it.v)) {
      g[it.u].pb({it.cst,it.v});
      g[it.v].pb({it.cst,it.u});
      if(it.u>it.v)swap(it.u,it.v);
      mst.insert({it.u,it.v});
      res+=it.cst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,N)if(pa[i-1][x]!=-1){
    pa[i][x]=pa[i-1][pa[i-1][x]];
    if(c[i-1][pa[i-1][x]])c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
  }
}
int lca(int x,int y) {
  if(d[x]<d[y])swap(x,y);
  rfor(i,K,0)if(d[x]-(1<<i)>=d[y])x=pa[i][x];
  if(x==y)return x;
  rfor(i,K,0)if(pa[i][x]!=pa[i][y])x=pa[i][x],y=pa[i][y];
  return pa[0][x];
}
void debug() {
  cout<<"pa: "<<endl;
  fore(i,0,n){cout<<i+1<<" : ";fore(j,0,n)cout<<pa[j][i]<<" ";cout<<endl;}
  cout<<"cost: "<<endl;
  fore(i,0,n){cout<<i+1<<" : ";fore(j,0,n)cout<<c[j][i]<<" ";cout<<endl;}
  cout<<"t: "<<t<<endl;
  fore(i,0,n){cout<<i+1<<" : ";for(auto j:g[i])cout<<"{"<<j.snd+1<<","<<j.fst<<"} ";cout<<endl;}
  cout<<"mst: ";for(auto it:mst)cout<<"{"<<it.fst+1<<","<<it.snd+1<<"} ";cout<<endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin>>n>>m;
  fore(i,0,m) {
    int u,v;ll w;
    cin>>u>>v>>w;u--,v--;
    edges.pb(edge(u,v,w,i));
  }
  fore(i,0,N)ufPa[i]=i;
  t=kruskal();
  lca_init();
  // debug();
  for(auto it:edges) {
    int x=it.u,y=it.v;
    if(x>y)swap(x,y);
    ll res=t;
    if(!mst.count({x,y})) {
      res+=it.cst;
      int l=lca(x,y);ll mx=0;
      rfor(i,K,0)if(d[x]-(1<<i)>=d[l])mx=max(mx,c[i][x]),x=pa[i][x];
      rfor(i,K,0)if(d[y]-(1<<i)>=d[l])mx=max(mx,c[i][y]),y=pa[i][y];
      res-=mx;
    }
    // cout<<"{"<<it.snd.fst+1<<","<<it.snd.snd+1<<"} ("<<it.fst<<"): "<<t<<" "<<mx<<" : "<<res<<" "<<res<<endl;
    r[it.id]=res;
  }
  fore(i,0,m)cout<<r[i]<<"\n";
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

typedef pair<ll,pair<int,int>> edge;
vector<edge>edges,e2;
vector<par>g[N];
double cl,timer;
int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    int u=it.snd.fst,v=it.snd.snd;
    if(join(u,v)) {
      g[u].pb({it.fst,v});
      g[v].pb({it.fst,u});
      res+=it.fst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,n){
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 9\n");
      exit(0);
    }
    if(pa[i-1][x]!=-1){
      pa[i][x]=pa[i-1][pa[i-1][x]];
      c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
    }
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 7\n");
      exit(0);
    }
    assert(x>=0 && y>=0);
    if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  }
  if(x==y)return res;
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 8\n");
      exit(0);
    }
    if(pa[i][x]!=pa[i][y]){
      res=max({res,c[i][x],c[i][y]});
      assert(x>=0 && y>=0);
      x=pa[i][x],y=pa[i][y];
    }
  }
  assert(x>=0 && y>=0);
  res=max({res,c[0][x],c[0][y]});
  return res;
}

int main() {
  cl=clock();
  scanf("%d%d",&n,&m);
  fore(i,0,m) {
    int u,v;ll w;
    scanf("%d%d%lld",&u,&v,&w);u--,v--;
    edges.pb({w,{u,v}});
    e2.pb({w,{u,v}});
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 6\n");
      return 0;
    }
  }
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 1\n");
    return 0;
  }
  fore(i,0,n)ufPa[i]=i;
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 2\n");
    return 0;
  }
  t=kruskal();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 3\n");
    return 0;
  }
  lca_init();
  // debug();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.1) {
    printf("DEMETRIO 4\n");
    return 0;
  }
  assert(e2.size()==m);
  for(auto it:e2){
    printf("%lld\n",t+it.fst-query(it.snd.fst,it.snd.snd));
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.1) {
      printf("DEMETRIO 5\n");
      return 0;
    }
  }
  return 0;
  // printf("%f\n",(clock()-cl)*1./CLOCKS_PER_SEC);
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define rfor(i,ii,iii) for(int i=ii-1;i>=iii;i--)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

typedef pair<ll,pair<int,int>> edge;
/*
struct edge {
  ll cst;
  int u,v,id;
  edge(int u,int v,ll cst,int id):u(u),v(v),cst(cst),id(id){}
  bool operator<(const edge &b)const{return cst<b.cst;}
};
*/
vector<edge>edges,e2;
vector<par>g[N];

int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    int u=it.snd.fst,v=it.snd.snd;
    if(join(u,v)) {
      g[u].pb({it.fst,v});
      g[v].pb({it.fst,u});
      res+=it.fst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,n)if(pa[i-1][x]!=-1){
    pa[i][x]=pa[i-1][pa[i-1][x]];
    c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  rfor(i,K,0)if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  if(x==y)return res;
  rfor(i,K,0)if(pa[i][x]!=pa[i][y]){
    res=max({res,c[i][x],c[i][y]});
    x=pa[i][x],y=pa[i][y];
  }
  res=max({res,c[0][x],c[0][y]});
  return res;
}

int main() {
  // auto cl=clock();freopen("out","w",stdout);
  scanf("%d%d",&n,&m);
  fore(i,0,m) {
    int u,v;ll w;
    scanf("%d%d%lld",&u,&v,&w);u--,v--;
    edges.pb({w,{u,v}});
    e2.pb({w,{u,v}});
  }
  fore(i,0,N)ufPa[i]=i;
  t=kruskal();
  lca_init();
  // debug();
  for(auto it:e2)printf("%lld\n",t+it.fst-query(it.snd.fst,it.snd.snd));
  // printf("%f\n",(clock()-cl)*1./CLOCKS_PER_SEC);
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

typedef pair<ll,pair<int,int>> edge;
vector<edge>edges,e2;
vector<par>g[N];
double cl,timer;
int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    int u=it.snd.fst,v=it.snd.snd;
    if(join(u,v)) {
      g[u].pb({it.fst,v});
      g[v].pb({it.fst,u});
      res+=it.fst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,n){
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.1) {
      printf("DEMETRIO 9\n");
      exit(0);
    }
    if(pa[i-1][x]!=-1){
      pa[i][x]=pa[i-1][pa[i-1][x]];
      c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
    }
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.1) {
      printf("DEMETRIO 7\n");
      exit(0);
    }
    assert(x>=0 && y>=0);
    if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  }
  if(x==y)return res;
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.1) {
      printf("DEMETRIO 8\n");
      exit(0);
    }
    if(pa[i][x]!=pa[i][y]){
      res=max({res,c[i][x],c[i][y]});
      assert(x>=0 && y>=0);
      x=pa[i][x],y=pa[i][y];
    }
  }
  assert(x>=0 && y>=0);
  res=max({res,c[0][x],c[0][y]});
  return res;
}

int main() {
  cl=clock();
  scanf("%d%d",&n,&m);
  fore(i,0,m) {
    int u,v;ll w;
    scanf("%d%d%lld",&u,&v,&w);u--,v--;
    edges.pb({w,{u,v}});
    e2.pb({w,{u,v}});
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 6\n");
      return 0;
    }
  }
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 1\n");
    return 0;
  }
  fore(i,0,n)ufPa[i]=i;
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 2\n");
    return 0;
  }
  t=kruskal();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.1) {
    printf("DEMETRIO 3\n");
    return 0;
  }
  lca_init();
  // debug();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.1) {
    printf("DEMETRIO 4\n");
    return 0;
  }
  assert(e2.size()==m);
  for(auto it:e2){
    printf("%lld\n",t+it.fst-query(it.snd.fst,it.snd.snd));
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.1) {
      printf("DEMETRIO 5\n");
      return 0;
    }
  }
  return 0;
  // printf("%f\n",(clock()-cl)*1./CLOCKS_PER_SEC);
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define rfor(i,ii,iii) for(int i=ii-1;i>=iii;i--)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

typedef pair<ll,pair<int,int>> edge;
/*
struct edge {
  ll cst;
  int u,v,id;
  edge(int u,int v,ll cst,int id):u(u),v(v),cst(cst),id(id){}
  bool operator<(const edge &b)const{return cst<b.cst;}
};
*/
vector<edge>edges,e2;
vector<par>g[N];
double cl,timer;
int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    int u=it.snd.fst,v=it.snd.snd;
    if(join(u,v)) {
      g[u].pb({it.fst,v});
      g[v].pb({it.fst,u});
      res+=it.fst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,n){
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 9\n");
      exit(0);
    }
    if(pa[i-1][x]!=-1){
      pa[i][x]=pa[i-1][pa[i-1][x]];
      c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
    }
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  rfor(i,K,0){
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 7\n");
      exit(0);
    }
    if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  }
  if(x==y)return res;
  rfor(i,K,0){
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 8\n");
      exit(0);
    }
    if(pa[i][x]!=pa[i][y]){
      res=max({res,c[i][x],c[i][y]});
      x=pa[i][x],y=pa[i][y];
    }
  }
  res=max({res,c[0][x],c[0][y]});
  return res;
}

int main() {
  cl=clock();
  scanf("%d%d",&n,&m);
  fore(i,0,m) {
    int u,v;ll w;
    scanf("%d%d%lld",&u,&v,&w);u--,v--;
    edges.pb({w,{u,v}});
    e2.pb({w,{u,v}});
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 6\n");
      return 0;
    }
  }
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 1\n");
    return 0;
  }
  fore(i,0,n)ufPa[i]=i;
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 2\n");
    return 0;
  }
  t=kruskal();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 3\n");
    return 0;
  }
  lca_init();
  // debug();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 4\n");
    return 0;
  }
  for(auto it:e2){
    printf("%lld\n",t+it.fst-query(it.snd.fst,it.snd.snd));
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 5\n");
      return 0;
    }
  }
  return 0;
  // printf("%f\n",(clock()-cl)*1./CLOCKS_PER_SEC);
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

typedef pair<ll,pair<int,int>> edge;
vector<edge>edges,e2;
vector<par>g[N];
double cl,timer;
int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(sz[y]>sz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    int u=it.snd.fst,v=it.snd.snd;
    if(join(u,v)) {
      g[u].pb({it.fst,v});
      g[v].pb({it.fst,u});
      res+=it.fst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,n){
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 9\n");
      exit(0);
    }
    if(pa[i-1][x]!=-1){
      pa[i][x]=pa[i-1][pa[i-1][x]];
      c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
    }
  }
}
int query(int x,int y) {
  ll res=0;
  if(d[x]<d[y])swap(x,y);
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 7\n");
      exit(0);
    }
    assert(x>=0 && y>=0);
    if(d[x]-(1<<i)>=d[y])res=max(res,c[i][x]),x=pa[i][x];
  }
  if(x==y)return res;
  for(int i=K-1;i>=0;i--) {
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 8\n");
      exit(0);
    }
    if(pa[i][x]!=pa[i][y]){
      res=max({res,c[i][x],c[i][y]});
      assert(x>=0 && y>=0);
      x=pa[i][x],y=pa[i][y];
    }
  }
  assert(x>=0 && y>=0);
  res=max({res,c[0][x],c[0][y]});
  return res;
}

int main() {
  cl=clock();
  scanf("%d%d",&n,&m);
  fore(i,0,m) {
    int u,v;ll w;
    scanf("%d%d%lld",&u,&v,&w);u--,v--;
    edges.pb({w,{u,v}});
    e2.pb({w,{u,v}});
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 6\n");
      return 0;
    }
  }
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 1\n");
    return 0;
  }
  fore(i,0,n)ufPa[i]=i;
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 2\n");
    return 0;
  }
  t=kruskal();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.5) {
    printf("DEMETRIO 3\n");
    return 0;
  }
  lca_init();
  // debug();
  timer=((clock()-cl)*1./CLOCKS_PER_SEC);
  if(timer>0.0001) {
    printf("DEMETRIO 4\n");
    return 0;
  }
  for(auto it:e2){
    printf("%lld\n",t+it.fst-query(it.snd.fst,it.snd.snd));
    timer=((clock()-cl)*1./CLOCKS_PER_SEC);
    if(timer>0.5) {
      printf("DEMETRIO 5\n");
      return 0;
    }
  }
  return 0;
  // printf("%f\n",(clock()-cl)*1./CLOCKS_PER_SEC);
}


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

typedef long long ll;
typedef pair<ll,int>par;
#define fore(i,ii,iii) for(int i=ii;i<iii;i++)
#define rfor(i,ii,iii) for(int i=ii-1;i>=iii;i--)
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
#define fst first
#define snd second

const int K=18,N=1<<18;
int n,m,pa[K][N],sz[N],d[N],ufPa[N],ufSz[N];
ll c[K][N],t,r[N];

struct edge {
  ll cst;
  int u,v,id;
  edge(int u,int v,ll cst,int id):u(u),v(v),cst(cst),id(id){}
  bool operator<(const edge &b)const{return cst<b.cst;}
};
vector<edge>edges;
set<pair<int,int>>mst;
vector<par>g[N];

int find_parent(int x) {
  while(x!=ufPa[x])x=find_parent(ufPa[x]);
  return ufPa[x];
}
bool join(int x,int y) {
  x=find_parent(x),y=find_parent(y);
  if(x==y)return 0;
  if(ufSz[y]>ufSz[x])ufPa[x]=y,ufSz[y]+=ufSz[x];
  else ufPa[y]=x,ufSz[x]+=ufSz[y];
  return 1;
}
ll kruskal() {
  ll res=0;
  sort(edges.begin(),edges.end());
  for(auto it:edges) {
    if(join(it.u,it.v)) {
      g[it.u].pb({it.cst,it.v});
      g[it.v].pb({it.cst,it.u});
      if(it.u>it.v)swap(it.u,it.v);
      mst.insert({it.u,it.v});
      res+=it.cst;
    }
  }
  return res;
}
void lca_dfs(int v) {
  for(par p:g[v]) {
    if(p.snd!=pa[0][v]) {
      pa[0][p.snd]=v;
      c[0][p.snd]=p.fst;
      d[p.snd]=d[v]+1;
      lca_dfs(p.snd);
    }
  }
}
void lca_init() {
  mset(pa,-1);
  lca_dfs(0);
  fore(i,1,K)fore(x,0,N)if(pa[i-1][x]!=-1){
    pa[i][x]=pa[i-1][pa[i-1][x]];
    if(c[i-1][pa[i-1][x]])c[i][x]=max(c[i-1][x],c[i-1][pa[i-1][x]]);
  }
}
int lca(int x,int y) {
  if(d[x]<d[y])swap(x,y);
  rfor(i,K,0)if(d[x]-(1<<i)>=d[y])x=pa[i][x];
  if(x==y)return x;
  rfor(i,K,0)if(pa[i][x]!=pa[i][y])x=pa[i][x],y=pa[i][y];
  return pa[0][x];
}
ll cost(int x,int paX) {
  ll res=0;
  rfor(i,K,0)if(d[x]-(1<<i)>=d[paX]) {
    res+=c[i][x];
    x=pa[i][x];
  }
  return res;
}
void debug() {
  cout<<"pa: "<<endl;
  fore(i,0,n){cout<<i+1<<" : ";fore(j,0,n)cout<<pa[j][i]<<" ";cout<<endl;}
  cout<<"cost: "<<endl;
  fore(i,0,n){cout<<i+1<<" : ";fore(j,0,n)cout<<c[j][i]<<" ";cout<<endl;}
  cout<<"t: "<<t<<endl;
  fore(i,0,n){cout<<i+1<<" : ";for(auto j:g[i])cout<<"{"<<j.snd+1<<","<<j.fst<<"} ";cout<<endl;}
  cout<<"mst: ";for(auto it:mst)cout<<"{"<<it.fst+1<<","<<it.snd+1<<"} ";cout<<endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin>>n>>m;
  fore(i,0,m) {
    int u,v;ll w;
    cin>>u>>v>>w;u--,v--;
    edges.pb(edge(u,v,w,i));
  }
  fore(i,0,N)ufPa[i]=i,ufSz[i]=1;
  t=kruskal();
  lca_init();
  // debug();
  for(auto it:edges) {
    int x=it.u,y=it.v;
    if(x>y)swap(x,y);
    ll res=t;
    if(!mst.count({x,y})) {
      res+=it.cst;
      int l=lca(x,y);ll mx=0;
      rfor(i,K,0)if(d[x]-(1<<i)>=d[l])mx=max(mx,c[i][x]),x=pa[i][x];
      rfor(i,K,0)if(d[y]-(1<<i)>=d[l])mx=max(mx,c[i][y]),y=pa[i][y];
      res-=mx;
    }
    // cout<<"{"<<it.snd.fst+1<<","<<it.snd.snd+1<<"} ("<<it.fst<<"): "<<t<<" "<<mx<<" : "<<res<<" "<<res<<endl;
    r[it.id]=res;
  }
  fore(i,0,m)cout<<r[i]<<"\n";
}