#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>

using namespace std;
typedef long long ll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) ((int)(x).size())
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) (x).begin(), (x).end()
typedef tuple<int, int, int> t3;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ldouble;
typedef pair<double, double> pdd;

int n, m;
struct edge{
	edge(){}
	edge(int x, int y, int l, int c):x(x), y(y), l(l), c(c){}
	int x, y, l, c;
	bool operator<(const edge &rhs) const{
		return l < rhs.l;
	}
} E[200020];
int ans[200020];
int p[200020];
int Find(int x) { return p[x] == x ? x : p[x] = Find(p[x]); }

vector <t3> T[200020];	//des, l, idx

int dep[200020];
int up[200020][20];
int upl[200020][20];

void dfs1(int x, int fa) {
	for(t3 e : T[x]) {
		int u = get<0>(e);
		if(u == fa) continue;
		up[u][0] = x; upl[u][0] = get<1>(e);
		for(int i=1;i<20;i++) {
			up[u][i] = up[ up[u][i-1] ][i-1];
			upl[u][i] = min(upl[u][i-1], upl[ up[u][i-1] ][i-1]);
		}
		dep[u] = dep[x] + 1;
		dfs1(u, x);

	}
}

int lca(int x, int y) {
	if(dep[x] < dep[y]) swap(x, y);
	rep(i, 20) if(1<<i & (dep[x] - dep[y])) x = up[x][i];
	for(int i=19;i>=0;i--) if(up[x][i] != up[y][i]) x = up[x][i], y = up[y][i];
	return x == y ? x : up[x][0];
}

vector <int> VI[200020], VD[200020];

int get_min(int x, int y, int L) {
	int lc = lca(x, y);
	VI[x].pb(L);
	VI[y].pb(L);
	VD[lc].pb(L);
	VD[lc].pb(L);
	int res = 2e9;
	rep(i, 20) if(1<<i & (dep[x] - dep[lc])) res = min(res, upl[x][i]), x = up[x][i];
	rep(i, 20) if(1<<i & (dep[y] - dep[lc])) res = min(res, upl[y][i]), y = up[y][i];
	return res;
}

int is_t[200020];
multiset <int> S[200020]; int idx[200020];

void dfs2(int x, int fa, int ue) {
	for(t3 e : T[x]) {
		int u = get<0>(e);
		if(u == fa) continue;
		dfs2(u, x, get<2>(e));
		if(sz(S[idx[x]]) > sz(S[idx[u]])) {
			for(int e : S[idx[u]]) S[idx[x]].insert(e);
		}
		else {
			for(int e : S[idx[x]]) S[idx[u]].insert(e);
			idx[x] = idx[u];
		}
	}
	for(int e : VI[x]) {
		S[idx[x]].insert(e);
	}
	for(int e : VD[x]) {
		S[idx[x]].erase(S[idx[x]].find(e));
	}
	if(sz(S[idx[x]])) {
		ans[ue] = *S[idx[x]].begin() - 1;
	}
}

void solve(){
	scanf("%d%d", &n, &m);
	rep(i, m) {
		int x, y, z; scanf("%d%d%d", &x, &y, &z);
		E[i] = edge(x, y, z, i);
	}

	sort(E, E+m);
	for(int i=1;i<=n;i++) p[i] = i;
	rep(i, m) {
		int x = E[i].x, y = E[i].y;
		x = Find(x), y = Find(y);
		if(x != y) {
			p[x] = y;
			T[E[i].x].pb(t3(E[i].y, E[i].l, E[i].c));
			T[E[i].y].pb(t3(E[i].x, E[i].l, E[i].c));
			is_t[E[i].c] = 1;
		}
	}

	dfs1(1, -1);
	rep(i, m) {
		if(is_t[E[i].c] == 0) {
			int x = E[i].x, y = E[i].y;
			ans[E[i].c] = get_min(x, y, E[i].l) - 1;
		}
		else ans[E[i].c] = -1;
	}
	for(int i=1;i<=n;i++) idx[i] = i;
	dfs2(1, -1, -1);

	rep(i, m) printf("%d ", ans[i]); puts("");
}

int main(){
	int Tc = 1; // scanf("%d\n", &Tc);
	for(int tc=1;tc<=Tc;tc++){
		// printf("Case #%d: ", tc);
		solve();
	}
	return 0;
};


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>

using namespace std;
typedef long long ll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) ((int)(x).size())
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) (x).begin(), (x).end()
typedef tuple<int, int, int> t3;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ldouble;
typedef pair<double, double> pdd;

int n, m;
struct edge{
	edge(){}
	edge(int x, int y, int l, int c):x(x), y(y), l(l), c(c){}
	int x, y, l, c;
	bool operator<(const edge &rhs) const{
		return l < rhs.l;
	}
} E[200020];
int ans[200020];
int p[200020];
int Find(int x) { return p[x] == x ? x : p[x] = Find(p[x]); }

vector <t3> T[200020];	//des, l, idx

int dep[200020];
int up[200020][20];
int upl[200020][20];

void dfs1(int x, int fa) {
	for(t3 e : T[x]) {
		int u = get<0>(e);
		if(u == fa) continue;
		up[u][0] = x; upl[u][0] = get<1>(e);
		for(int i=1;i<20;i++) {
			up[u][i] = up[ up[u][i-1] ][i-1];
			upl[u][i] = min(upl[u][i-1], upl[ up[u][i-1] ][i-1]);
		}
		dep[u] = dep[x] + 1;
		dfs1(u, x);

	}
}

int lca(int x, int y) {
	if(dep[x] < dep[y]) swap(x, y);
	rep(i, 20) if(1<<i & (dep[x] - dep[y])) x = up[x][i];
	for(int i=19;i>=0;i--) if(up[x][i] != up[y][i]) x = up[x][i], y = up[y][i];
	return x == y ? x : up[x][0];
}

vector <int> VI[200020], VD[200020];

int get_min(int x, int y, int L) {
	int lc = lca(x, y);
	VI[x].pb(L);
	VI[y].pb(L);
	VD[lc].pb(L);
	VD[lc].pb(L);
	int res = 2e9;
	rep(i, 20) if(1<<i & (dep[x] - dep[lc])) res = min(res, upl[x][i]), x = up[x][i];
	rep(i, 20) if(1<<i & (dep[y] - dep[lc])) res = min(res, upl[y][i]), x = up[y][i];
	return res;
}

int is_t[200020];
multiset <int> S[200020]; int idx[200020];

void dfs2(int x, int fa, int ue) {
	for(t3 e : T[x]) {
		int u = get<0>(e);
		if(u == fa) continue;
		dfs2(u, x, get<2>(e));
		if(sz(S[idx[x]]) > sz(S[idx[u]])) {
			for(int e : S[idx[u]]) S[idx[x]].insert(e);
		}
		else {
			for(int e : S[idx[x]]) S[idx[u]].insert(e);
			idx[x] = idx[u];
		}
	}
	for(int e : VI[x]) {
		S[idx[x]].insert(e);
	}
	for(int e : VD[x]) {
		S[idx[x]].erase(S[idx[x]].find(e));
	}
	if(sz(S[idx[x]])) {
		ans[ue] = *S[idx[x]].begin() - 1;
	}
}

void solve(){
	scanf("%d%d", &n, &m);
	rep(i, m) {
		int x, y, z; scanf("%d%d%d", &x, &y, &z);
		E[i] = edge(x, y, z, i);
	}

	sort(E, E+m);
	for(int i=1;i<=n;i++) p[i] = i;
	rep(i, m) {
		int x = E[i].x, y = E[i].y;
		x = Find(x), y = Find(y);
		if(x != y) {
			p[x] = y;
			T[E[i].x].pb(t3(E[i].y, E[i].l, E[i].c));
			T[E[i].y].pb(t3(E[i].x, E[i].l, E[i].c));
			is_t[E[i].c] = 1;
		}
	}

	dfs1(1, -1);
	rep(i, m) {
		if(is_t[E[i].c] == 0) {
			int x = E[i].x, y = E[i].y;
			ans[E[i].c] = get_min(x, y, E[i].l) - 1;
		}
		else ans[E[i].c] = -1;
	}
	for(int i=1;i<=n;i++) idx[i] = i;
	dfs2(1, -1, -1);

	rep(i, m) printf("%d ", ans[i]); puts("");
}

int main(){
	int Tc = 1; // scanf("%d\n", &Tc);
	for(int tc=1;tc<=Tc;tc++){
		// printf("Case #%d: ", tc);
		solve();
	}
	return 0;
};


//              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              \\

#include <bits/stdc++.h>

#define F first
#define S second
#define _sz(x) (int)x.size()
#define pb push_back

#define error(x) cout << #x << " = " << x << endl

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;

const int N = 2e5 + 20, LG = 18;
int n, m;
vector <int> g[N];

struct edge {
	int id;
	int u, v, w;
	bool mark;
	int res;
} e[N];

inline bool by_w (edge a, edge b) { return a.w < b.w; }
inline bool by_id (edge a, edge b) { return a.id < b.id; }

int dpar[N];

int root (int v) { return dpar[v] < 0 ? v : dpar[v] = root(dpar[v]); }

inline bool merge (int u, int v)
{
	if ((u = root(u)) == (v = root(v))) return 0;
	dpar[u] = v;
	return 1;
}

int h[N], par[LG][N], mx[LG][N];

inline pii get (int u, int v)
{
	int res_mx = 0;

	if (h[u] > h[v]) swap(u, v);

	for (int i = 0; i < LG; i++)
		if ((h[v] - h[u]) >> i & 1)
		{
			res_mx = max(res_mx, mx[i][v]);
			v = par[i][v];
		}

	if (u == v) return pii(u, res_mx);

	for (int i = LG - 1; i >= 0; i--)
		if (par[i][u] != par[i][v])
		{
			res_mx = max({res_mx, mx[i][u], mx[i][v]});
			u = par[i][u];
			v = par[i][v];
		}

	res_mx = max({res_mx, mx[0][u], mx[0][v]});

	return pii(par[0][u], res_mx);
}

void pre_dfs (int v)
{
	for (int i = 1; i < LG; i++)
	{
		par[i][v] = par[i - 1][par[i - 1][v]];
		mx[i][v] = max(mx[i - 1][v], mx[i - 1][par[i - 1][v]]);
	}

	for (int ed : g[v])
	{
		int u = e[ed].u ^ e[ed].v ^ v;

		if (!e[ed].mark || u == par[0][v]) continue;

		h[u] = h[v] + 1;
		par[0][u] = v;
		mx[0][u] = e[ed].w;
		pre_dfs(u);
	}
}

set <pii> st1[N], st2[N];

void dfs (int v)
{
	for (int ed : g[v])
	{
		int u = e[ed].u ^ e[ed].v ^ v;

		if (u == par[0][v]) continue;

		if (!e[ed].mark)
		{
			int l = get(u, v).F;

			if (l == v) continue;

			st1[v].insert({h[l], e[ed].w});
			st2[v].insert({e[ed].w, h[l]});
		}
		else
		{
			dfs(u);

			if (_sz(st2[u])) e[ed].res = st2[u].begin()->F - 1;
			else e[ed].res = -1;

			while (_sz(st1[u]) && st1[u].rbegin()->F == h[v])
			{
				pii p = *st1[u].rbegin();
				st1[u].erase(p);
				st2[u].erase({p.S, p.F});
			}

			if (_sz(st1[u]) > _sz(st1[v]))
			{
				st1[v].swap(st1[u]);
				st2[v].swap(st2[u]);
			}

			while (_sz(st1[u]))
			{
				pii p = *st1[u].begin();

				st1[v].insert(p);
				st2[v].insert({p.S, p.F});

				st1[u].erase(p);
				st2[u].erase({p.S, p.F});
			}
		}
	}
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

	cin >> n >> m;

	for (int i = 0; i < m; i++)
	{
		e[i].id = i;
		cin >> e[i].u >> e[i].v >> e[i].w;
		e[i].u--, e[i].v--;

		g[e[i].u].pb(i);
		g[e[i].v].pb(i);
	}

	sort(e, e + m, by_w);

	memset(dpar, -1, sizeof dpar);

	for (int i = 0; i < m; i++)
	{
		int u = e[i].u, v = e[i].v;
		if (merge(u, v)) e[i].mark = 1;
	}

	sort(e, e + m, by_id);

	pre_dfs(0);

	for (int i = 0; i < m; i++)
		if (!e[i].mark)
			e[i].res = get(e[i].u, e[i].v).S - 1;

	dfs(0);

	for (int i = 0; i < m; i++) cout << e[i].res << ' ';
	cout << '\n';
}


//              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              \\

// estimated time: 30 min, start: 10:43

#include <bits/stdc++.h>

#define F first
#define S second
#define _sz(x) (int)x.size()
#define pb push_back

#define error(x) cout << #x << " = " << x << endl

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;

const int N = 2e5 + 20, LG = 18;
int n, m;
vector <int> g[N];

struct edge {
	int id;
	int u, v, w;
	bool mark;
	int res;
} e[N];

inline bool by_w (edge a, edge b) { return a.w < b.w; }
inline bool by_id (edge a, edge b) { return a.id < b.id; }

int dpar[N];

int root (int v) { return dpar[v] < 0 ? v : dpar[v] = root(dpar[v]); }

inline bool merge (int u, int v)
{
	if ((u = root(u)) == (v = root(v))) return 0;
	dpar[u] = v;
	return 1;
}

int h[N], par[LG][N], mx[LG][N];

inline pii get (int u, int v)
{
	int res_mx = 0;

	if (h[u] > h[v]) swap(u, v);

	for (int i = 0; i < LG; i++)
		if ((h[v] - h[u]) >> i & 1)
		{
			res_mx = max(res_mx, mx[i][v]);
			v = par[i][v];
		}

	if (u == v) return pii(u, res_mx);

	for (int i = LG - 1; i >= 0; i--)
		if (par[i][u] != par[i][v])
		{
			res_mx = max({res_mx, mx[i][u], mx[i][v]});
			u = par[i][u];
			v = par[i][v];
		}

	res_mx = max({res_mx, mx[0][u], mx[0][v]});

	return pii(par[0][u], res_mx);
}

void pre_dfs (int v)
{
	for (int i = 1; i < LG; i++)
	{
		par[i][v] = par[i - 1][par[i - 1][v]];
		mx[i][v] = max(mx[i - 1][v], mx[i - 1][par[i - 1][v]]);
	}

	for (int ed : g[v])
	{
		int u = e[ed].u ^ e[ed].v ^ v;

		if (!e[ed].mark || u == par[0][v]) continue;

		h[u] = h[v] + 1;
		par[0][u] = v;
		mx[0][u] = e[ed].w;
		pre_dfs(u);
	}
}

set <pii> st1[N], st2[N];

void dfs (int v)
{
	for (int ed : g[v])
	{
		int u = e[ed].u ^ e[ed].v ^ v;

		if (u == par[0][v]) continue;

		if (!e[ed].mark)
		{
			int l = get(u, v).F;

			if (l == v) continue;

			st1[v].insert({h[l], e[ed].w});
			st2[v].insert({e[ed].w, h[l]});
		}
		else
		{
			dfs(u);

			if (_sz(st2[u])) e[ed].res = st2[u].begin()->F - 1;
			else e[ed].res = -1;

			while (_sz(st1[u]) && st1[u].begin()->F == h[v])
			{
				pii p = *st1[u].begin();
				st1[u].erase(p);
				st2[u].erase({p.S, p.F});
			}

			if (_sz(st1[u]) > _sz(st1[v]))
			{
				st1[v].swap(st1[u]);
				st2[v].swap(st2[u]);
			}

			while (_sz(st1[u]))
			{
				pii p = *st1[u].begin();

				st1[v].insert(p);
				st2[v].insert({p.S, p.F});

				st1[u].erase(p);
				st2[u].erase({p.S, p.S});
			}
		}
	}
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

	cin >> n >> m;

	for (int i = 0; i < m; i++)
	{
		e[i].id = i;
		cin >> e[i].u >> e[i].v >> e[i].w;
		e[i].u--, e[i].v--;

		g[e[i].u].pb(i);
		g[e[i].v].pb(i);
	}

	sort(e, e + m, by_w);

	memset(dpar, -1, sizeof dpar);

	for (int i = 0; i < m; i++)
	{
		int u = e[i].u, v = e[i].v;
		if (merge(u, v)) e[i].mark = 1;
	}

	sort(e, e + m, by_id);

	pre_dfs(0);

	for (int i = 0; i < m; i++)
		if (!e[i].mark)
			e[i].res = get(e[i].u, e[i].v).S - 1;

	dfs(0);

	for (int i = 0; i < m; i++) cout << e[i].res << ' ';
	cout << '\n';
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=4e5+5;
const int inf=1e9+5;
struct LCT{
    struct node{
        bool rev;
        int mn,val;
		int laz;
        node *f,*c[2];
        bool d(){return this==f->c[1];}
        bool rt(){return !f||(f->c[0]!=this&&f->c[1]!=this);}
        void sets(node *x,int d){pd();if(x)x->f=this;c[d]=x;rz();}
        void makerv(){rev^=1;swap(c[0],c[1]);}

		void make(int w){
			mn=val=laz=w;
		}

        void pd(){
            if(rev){
                if(c[0])c[0]->makerv();
                if(c[1])c[1]->makerv();
                rev=0;
            }
			if(laz){
                if(c[0])c[0]->make(laz);
                if(c[1])c[1]->make(laz);
				laz=0;
			}
        }
        void rz(){
            mn=val;
            if(c[0])mn=min(mn,c[0]->mn);
            if(c[1])mn=min(mn,c[1]->mn);
        }
    }nd[maxn];
    void rot(node *x){
        node *y=x->f;if(!y->rt())y->f->pd();
        y->pd();x->pd();bool d=x->d();
        y->sets(x->c[!d],d);
        if(y->rt())x->f=y->f;
        else y->f->sets(x,y->d());
        x->sets(y,!d);
    }
    void splay(node *x){
        while(!x->rt())
            if(x->f->rt())rot(x);
            else if(x->d()==x->f->d())rot(x->f),rot(x);
            else rot(x),rot(x);
    }
    node* access(node *x){
        node *y=0;
        for(;x;x=x->f){
            splay(x);
            x->sets(y,1);y=x;
        }return y;
    }
    void makert(node *x){
        access(x)->makerv();
        splay(x);
    }
    void link(node *x,node *y){
        makert(x);
        x->f=y;
        access(x);
    }
    void cut(node *x,node *y){
        makert(x);access(y);splay(y);
        y->c[0]=x->f=0;
        y->rz();
    }

	bool con(node *x,node *y){
		makert(x);
		access(y);
		splay(y);
		while(y&&y->c[0]){
			y=y->c[0];
		}
		return x==y;
	}
	int Qmin(node *x,node *y){
		makert(x);
		access(y);
		splay(y);
		return y->mn;
	}
	void color(node *x,node *y,int w){
		makert(x);
		access(y);
		splay(y);
		y->make(w);
	}

    void link(int x,int y){link(nd+x,nd+y);}
    void cut(int x,int y){cut(nd+x,nd+y);}
	bool con(int x,int y){
		return con(nd+x,nd+y);
	}
	int Qmin(int x,int y){
		return Qmin(nd+x,nd+y);
	}
	void color(int x,int y,int w){
		color(nd+x,nd+y,w);
	}

}T;
int n,m;
struct edge{
	int u,v,w,id;
	bool tree;
	bool operator<(const edge &oth)const{
		return w<oth.w;
	}
}E[maxn];
int fa[maxn];
int find(int x){return fa[x]==x ?  x : fa[x]=find(fa[x]);}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		fa[i]=i;
		T.nd[i].val=inf;
		T.nd[i].rz();
	}
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w);
		E[i].id=i;
		T.nd[i+n].val=E[i].w;
		T.nd[i+n].rz();
	}
	sort(E+1,E+1+m);
	for(int i=1;i<=m;i++){
		int u=E[i].u,v=E[i].v;
		if(find(u)!=find(v)){
			E[i].tree=1;
			fa[find(u)]=find(v);
			T.link(u,n+E[i].id);
			T.link(n+E[i].id,v);
		}
	}
	static int ans[maxn];
	for(int i=1;i<=m;i++)if(!E[i].tree){
		ans[E[i].id]=T.Qmin(E[i].u,E[i].v)-1;
	}

	for(int i=1;i<=m;i++){
		T.color(i+n,i+n,inf);
	}

	for(int i=m;i>=1;i--)if(!E[i].tree){
		T.color(E[i].u,E[i].v,E[i].w);
	}

	for(int i=1;i<=m;i++)if(E[i].tree){
		int w=T.Qmin(E[i].u,E[i].v);
		if(w==inf)
			ans[E[i].id]=-1;
		else
			ans[E[i].id]=w-1;
	}

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


#include<bits/stdc++.h>
using namespace std;
const int maxn=4e5+5;
const int inf=1e9+5;
struct LCT{
    struct node{
        bool rev;
        int mn,val;
		int laz;
		bool isv;
        node *f,*c[2];
        bool d(){return this==f->c[1];}
        bool rt(){return !f||(f->c[0]!=this&&f->c[1]!=this);}
        void sets(node *x,int d){pd();if(x)x->f=this;c[d]=x;rz();}
        void makerv(){rev^=1;swap(c[0],c[1]);}

		void make(int w){
			if(isv){
				mn=laz=w;
			}else
				mn=val=laz=w;
		}

        void pd(){
            if(rev){
                if(c[0])c[0]->makerv();
                if(c[1])c[1]->makerv();
                rev=0;
            }
			if(laz){
                if(c[0])c[0]->make(laz);
                if(c[1])c[1]->make(laz);
				laz=0;
			}
        }
        void rz(){
            mn=val;
            if(c[0])mn=min(mn,c[0]->mn);
            if(c[1])mn=min(mn,c[1]->mn);
        }
    }nd[maxn];
    void rot(node *x){
        node *y=x->f;if(!y->rt())y->f->pd();
        y->pd();x->pd();bool d=x->d();
        y->sets(x->c[!d],d);
        if(y->rt())x->f=y->f;
        else y->f->sets(x,y->d());
        x->sets(y,!d);
    }
    void splay(node *x){
        while(!x->rt())
            if(x->f->rt())rot(x);
            else if(x->d()==x->f->d())rot(x->f),rot(x);
            else rot(x),rot(x);
    }
    node* access(node *x){
        node *y=0;
        for(;x;x=x->f){
            splay(x);
            x->sets(y,1);y=x;
        }return y;
    }
    void makert(node *x){
        access(x)->makerv();
        splay(x);
    }
    void link(node *x,node *y){
        makert(x);
        x->f=y;
        access(x);
    }
    void cut(node *x,node *y){
        makert(x);access(y);splay(y);
        y->c[0]=x->f=0;
        y->rz();
    }

	bool con(node *x,node *y){
		makert(x);
		access(y);
		splay(y);
		while(y&&y->c[0]){
			y=y->c[0];
		}
		return x==y;
	}
	int Qmin(node *x,node *y){
		makert(x);
		access(y);
		splay(y);
		return y->mn;
	}
	void color(node *x,node *y,int w){
		makert(x);
		access(y);
		splay(y);
		y->make(w);
		y->pd();
	}

    void link(int x,int y){link(nd+x,nd+y);}
    void cut(int x,int y){cut(nd+x,nd+y);}
	bool con(int x,int y){
		return con(nd+x,nd+y);
	}
	int Qmin(int x,int y){
		return Qmin(nd+x,nd+y);
	}
	void color(int x,int y,int w){
		color(nd+x,nd+y,w);
	}

}T;
int n,m;
struct edge{
	int u,v,w,id;
	bool tree;
	bool operator<(const edge &oth)const{
		return w<oth.w;
	}
}E[maxn];
int fa[maxn];
int find(int x){return fa[x]==x ?  x : fa[x]=find(fa[x]);}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		fa[i]=i;
		T.nd[i].val=inf;
		T.nd[i].rz();
		T.nd[i].isv=1;
	}
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w);
		E[i].id=i;
		T.nd[i+n].val=E[i].w;
		T.nd[i+n].rz();
	}
	sort(E+1,E+1+m);
	for(int i=1;i<=m;i++){
		int u=E[i].u,v=E[i].v;
		if(find(u)!=find(v)){
			E[i].tree=1;
			fa[find(u)]=find(v);
			T.link(u,n+E[i].id);
			T.link(n+E[i].id,v);
		}
	}
	static int ans[maxn];
	for(int i=1;i<=m;i++)if(!E[i].tree){
		ans[E[i].id]=T.Qmin(E[i].u,E[i].v)-1;
	}

	for(int i=1;i<=m;i++){
		T.color(i+n,i+n,inf);
	}

	for(int i=m;i>=1;i--)if(!E[i].tree){
		T.color(E[i].u,E[i].v,E[i].w);
	}

	for(int i=1;i<=m;i++)if(E[i].tree){
		int w=T.Qmin(n+E[i].id,n+E[i].id);
		if(w==inf)
			ans[E[i].id]=-1;
		else
			ans[E[i].id]=w-1;
	}

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


#include<bits/stdc++.h>
using namespace std;
const int maxn=4e5+5;
const int inf=1e9+5;
struct LCT{
    struct node{
        bool rev;
        int mn,val;
		int laz;
		bool isv;
        node *f,*c[2];
        bool d(){return this==f->c[1];}
        bool rt(){return !f||(f->c[0]!=this&&f->c[1]!=this);}
        void sets(node *x,int d){pd();if(x)x->f=this;c[d]=x;rz();}
        void makerv(){rev^=1;swap(c[0],c[1]);}

		void make(int w){
			if(isv){
				mn=laz=w;
			}else
				mn=val=laz=w;
		}

        void pd(){
            if(rev){
                if(c[0])c[0]->makerv();
                if(c[1])c[1]->makerv();
                rev=0;
            }
			if(laz){
                if(c[0])c[0]->make(laz);
                if(c[1])c[1]->make(laz);
				laz=0;
			}
        }
        void rz(){
            mn=val;
            if(c[0])mn=min(mn,c[0]->mn);
            if(c[1])mn=min(mn,c[1]->mn);
        }
    }nd[maxn];
    void rot(node *x){
        node *y=x->f;if(!y->rt())y->f->pd();
        y->pd();x->pd();bool d=x->d();
        y->sets(x->c[!d],d);
        if(y->rt())x->f=y->f;
        else y->f->sets(x,y->d());
        x->sets(y,!d);
    }
    void splay(node *x){
        while(!x->rt())
            if(x->f->rt())rot(x);
            else if(x->d()==x->f->d())rot(x->f),rot(x);
            else rot(x),rot(x);
    }
    node* access(node *x){
        node *y=0;
        for(;x;x=x->f){
            splay(x);
            x->sets(y,1);y=x;
        }return y;
    }
    void makert(node *x){
        access(x)->makerv();
        splay(x);
    }
    void link(node *x,node *y){
        makert(x);
        x->f=y;
        access(x);
    }
    void cut(node *x,node *y){
        makert(x);access(y);splay(y);
        y->c[0]=x->f=0;
        y->rz();
    }

	bool con(node *x,node *y){
		makert(x);
		access(y);
		splay(y);
		while(y&&y->c[0]){
			y=y->c[0];
		}
		return x==y;
	}
	int Qmin(node *x,node *y){
		makert(x);
		access(y);
		splay(y);
		return y->mn;
	}
	void color(node *x,node *y,int w){
		makert(x);
		access(y);
		splay(y);
		y->make(w);
	}

    void link(int x,int y){link(nd+x,nd+y);}
    void cut(int x,int y){cut(nd+x,nd+y);}
	bool con(int x,int y){
		return con(nd+x,nd+y);
	}
	int Qmin(int x,int y){
		return Qmin(nd+x,nd+y);
	}
	void color(int x,int y,int w){
		color(nd+x,nd+y,w);
	}

}T;
int n,m;
struct edge{
	int u,v,w,id;
	bool tree;
	bool operator<(const edge &oth)const{
		return w<oth.w;
	}
}E[maxn];
int fa[maxn];
int find(int x){return fa[x]==x ?  x : fa[x]=find(fa[x]);}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		fa[i]=i;
		T.nd[i].val=inf;
		T.nd[i].rz();
		T.nd[i].isv=1;
	}
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w);
		E[i].id=i;
		T.nd[i+n].val=E[i].w;
		T.nd[i+n].rz();
	}
	sort(E+1,E+1+m);
	for(int i=1;i<=m;i++){
		int u=E[i].u,v=E[i].v;
		if(find(u)!=find(v)){
			E[i].tree=1;
			fa[find(u)]=find(v);
			T.link(u,n+E[i].id);
			T.link(n+E[i].id,v);
		}
	}
	static int ans[maxn];
	for(int i=1;i<=m;i++)if(!E[i].tree){
		ans[E[i].id]=T.Qmin(E[i].u,E[i].v)-1;
	}

	for(int i=1;i<=m;i++){
		T.color(i+n,i+n,inf);
	}

	for(int i=m;i>=1;i--)if(!E[i].tree){
		T.color(E[i].u,E[i].v,E[i].w);
	}

	for(int i=1;i<=m;i++)if(E[i].tree){
		int w=T.Qmin(E[i].u,E[i].v);
		if(w==inf)
			ans[E[i].id]=-1;
		else
			ans[E[i].id]=w-1;
	}

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


#include<bits/stdc++.h>
using namespace std;
const int maxn=4e5+5;
const int inf=1e9+5;
struct LCT{
    struct node{
        bool rev;
        int mn,val,mx;
		int laz;
		bool isv;
        node *f,*c[2];
        bool d(){return this==f->c[1];}
        bool rt(){return !f||(f->c[0]!=this&&f->c[1]!=this);}
        void sets(node *x,int d){pd();if(x)x->f=this;c[d]=x;rz();}
        void makerv(){rev^=1;swap(c[0],c[1]);}

		void make(int w){
			if(isv){
				mn=laz=mx=w;
			}else
				mn=val=laz=mx=w;
		}

        void pd(){
            if(rev){
                if(c[0])c[0]->makerv();
                if(c[1])c[1]->makerv();
                rev=0;
            }
			if(laz){
                if(c[0])c[0]->make(laz);
                if(c[1])c[1]->make(laz);
				laz=0;
			}
        }
        void rz(){
            mn=mx=val;
			if(val==inf)mx=-inf;
            if(c[0]){
				mn=min(mn,c[0]->mn);
				mx=max(mx,c[0]->mx);
			}
            if(c[1]){
				mn=min(mn,c[1]->mn);
				mx=max(mx,c[1]->mx);
			}
        }
    }nd[maxn];
    void rot(node *x){
        node *y=x->f;if(!y->rt())y->f->pd();
        y->pd();x->pd();bool d=x->d();
        y->sets(x->c[!d],d);
        if(y->rt())x->f=y->f;
        else y->f->sets(x,y->d());
        x->sets(y,!d);
    }
    void splay(node *x){
        while(!x->rt())
            if(x->f->rt())rot(x);
            else if(x->d()==x->f->d())rot(x->f),rot(x);
            else rot(x),rot(x);
    }
    node* access(node *x){
        node *y=0;
        for(;x;x=x->f){
            splay(x);
            x->sets(y,1);y=x;
        }return y;
    }
    void makert(node *x){
        access(x)->makerv();
        splay(x);
    }
    void link(node *x,node *y){
        makert(x);
        x->f=y;
        access(x);
    }
    void cut(node *x,node *y){
        makert(x);access(y);splay(y);
        y->c[0]=x->f=0;
        y->rz();
    }

	bool con(node *x,node *y){
		makert(x);
		access(y);
		splay(y);
		while(y&&y->c[0]){
			y=y->c[0];
		}
		return x==y;
	}
	int Qmin(node *x,node *y){
		makert(x);
		access(y);
		splay(y);
		return y->mn;
	}
	int Qmax(node *x,node *y){
		makert(x);
		access(y);
		splay(y);
		return y->mx;
	}
	void color(node *x,node *y,int w){
		makert(x);
		access(y);
		splay(y);
		y->make(w);
		y->pd();
	}

    void link(int x,int y){link(nd+x,nd+y);}
    void cut(int x,int y){cut(nd+x,nd+y);}
	bool con(int x,int y){
		return con(nd+x,nd+y);
	}
	int Qmax(int x,int y){
		return Qmax(nd+x,nd+y);
	}
	int Qmin(int x,int y){
		return Qmin(nd+x,nd+y);
	}
	void color(int x,int y,int w){
		color(nd+x,nd+y,w);
	}

}T;
int n,m;
struct edge{
	int u,v,w,id;
	bool tree;
	bool operator<(const edge &oth)const{
		return w<oth.w;
	}
}E[maxn];
int fa[maxn];
int find(int x){return fa[x]==x ?  x : fa[x]=find(fa[x]);}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		fa[i]=i;
		T.nd[i].val=inf;
		T.nd[i].rz();
		T.nd[i].isv=1;
	}
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w);
		E[i].id=i;
		T.nd[i+n].val=E[i].w;
		T.nd[i+n].rz();
	}
	sort(E+1,E+1+m);
	for(int i=1;i<=m;i++){
		int u=E[i].u,v=E[i].v;
		if(find(u)!=find(v)){
			E[i].tree=1;
			fa[find(u)]=find(v);
			T.link(u,n+E[i].id);
			T.link(n+E[i].id,v);
		}
	}
	static int ans[maxn];
	for(int i=1;i<=m;i++)if(!E[i].tree){
		ans[E[i].id]=T.Qmax(E[i].u,E[i].v)-1;
	}

	for(int i=1;i<=m;i++){
		T.color(i+n,i+n,inf);
	}

	for(int i=m;i>=1;i--)if(!E[i].tree){
		T.color(E[i].u,E[i].v,E[i].w);
	}

	for(int i=1;i<=m;i++)if(E[i].tree){
		int w=T.Qmin(n+E[i].id,n+E[i].id);
		if(w==inf)
			ans[E[i].id]=-1;
		else
			ans[E[i].id]=w-1;
	}

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


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int LOG = 20;
int n, m, vis[N], ans[N], f[N];
int dep[N], p[N][20], mx[N][20], eid[N];
struct Edge {
    int u, v, w, id;
    void read() {
        scanf("%d%d%d", &u, &v, &w);
    }
    bool operator < (const Edge &rhs) const {
        return w < rhs.w;
    }
}e[N];
vector<Edge> G[N];
int find(int x) {
    return ~f[x] ? f[x] = find(f[x]) : x;
}
void dfs(int u, int pre) {
    for(auto e : G[u]) {
        int v = e.v, w = e.w;
        if(v == pre) continue;
        eid[v] = e.id;
        dep[v] = dep[u] + 1;
        p[v][0] = u;
        mx[v][0] = w;
        for(int i = 1; i < LOG; i++) {
            p[v][i] = p[p[v][i-1]][i-1];
            mx[v][i] = max(mx[v][i-1], mx[p[v][i-1]][i-1]);
        }
        dfs(v, u);
    }
}
int query(int u, int v) {
    int ans = 0;
    if(dep[u] < dep[v]) swap(u, v);
    for(int i = 0; i < LOG; i++) {
        if((dep[u] - dep[v])>>i&1) {
            ans = max(ans, mx[u][i]);
            u = p[u][i];
        }
    }
    for(int i = LOG - 1; ~i; i--) {
        if(p[u][i] != p[v][i]) {
            ans = max(ans, max(mx[u][i], mx[v][i]));
            u = p[u][i];
            v = p[v][i];
        }
    }
    return max(ans, max(mx[u][0], mx[v][0]));
}
void calc(int u, int v, int w) {
    u = find(u); v = find(v);
    //cout << u << ' ' << v << endl;
    while(u != v) {
        if(dep[u] < dep[v]) swap(u, v);
        //cout << u << " -> " << eid[u] << ' ' << w << endl;
        ans[eid[u]] = min(ans[eid[u]], w - 1);
        f[u] = p[u][0];
        u = p[u][0];
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++) {
        e[i].read();
        e[i].id = i;
    }
    sort(e, e+m);
    memset(f, -1, sizeof(f));
    for(int i = 0; i < m; i++) {
        int u = e[i].u, v = e[i].v, w = e[i].w;
        if(find(u) != find(v)) {
            vis[i] = 1;
            f[find(u)] = find(v);
            G[u].push_back((Edge){u, v, w, i});
            G[v].push_back((Edge){v, u, w, i});
        }
    }
    dfs(1, 0);
    memset(ans, 0x3f, sizeof(ans));
    memset(f, -1, sizeof(f));
    for(int i = 0; i < m; i++) {
        if(vis[i]) continue;
        int u = e[i].u, v = e[i].v, w = e[i].w;
        ans[e[i].id] = query(u, v) - 1;
        calc(u, v, w);
    }
    for(int i = 0; i < m; i++) {
        printf("%d%c", ans[i] > 1e9 ? -1 : ans[i], " \n"[i+1==m]);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int LOG = 20;
int n, m, vis[N], ans[N], f[N];
int dep[N], p[N][20], mx[N][20], eid[N];
struct Edge {
    int u, v, w, id;
    void read() {
        scanf("%d%d%d", &u, &v, &w);
    }
    bool operator < (const Edge &rhs) const {
        return w < rhs.w;
    }
}e[N];
vector<Edge> G[N];
int find(int x) {
    return ~f[x] ? f[x] = find(f[x]) : x;
}
void dfs(int u, int pre) {
    for(auto e : G[u]) {
        int v = e.v, w = e.w;
        if(v == pre) continue;
        eid[v] = e.id;
        dep[v] = dep[u] + 1;
        p[v][0] = u;
        mx[v][0] = w;
        for(int i = 1; i < LOG; i++) {
            p[v][i] = p[p[v][i-1]][i-1];
            mx[v][i] = max(mx[v][i-1], mx[p[v][i-1]][i-1]);
        }
        dfs(v, u);
    }
}
int query(int u, int v) {
    int ans = 0;
    if(dep[u] < dep[v]) swap(u, v);
    for(int i = 0; i < LOG; i++) {
        if((dep[u] - dep[v])>>i&1) {
            ans = max(ans, mx[u][i]);
            u = p[u][i];
        }
    }
    for(int i = LOG - 1; ~i; i--) {
        if(p[u][i] != p[v][i]) {
            ans = max(ans, max(mx[u][i], mx[v][i]));
            u = p[u][i];
            v = p[v][i];
        }
    }
    return max(ans, max(mx[u][0], mx[v][0]));
}
void calc(int u, int v, int w) {
    while(find(u) != find(v)) {
        if(dep[find(u)] < dep[find(v)]) swap(u, v);
        int fu = find(u);
        ans[eid[fu]] = min(ans[eid[fu]], w - 1);
        f[fu] = p[fu][0];
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++) {
        e[i].read();
        e[i].id = i;
    }
    sort(e, e+m);
    memset(f, -1, sizeof(f));
    for(int i = 0; i < m; i++) {
        int u = e[i].u, v = e[i].v, w = e[i].w;
        if(find(u) != find(v)) {
            vis[i] = 1;
            f[find(u)] = find(v);
            G[u].push_back((Edge){u, v, w, e[i].id});
            G[v].push_back((Edge){v, u, w, e[i].id});
        }
    }
    dfs(1, 0);
    memset(ans, 0x3f, sizeof(ans));
    memset(f, -1, sizeof(f));
    for(int i = 0; i < m; i++) {
        if(vis[i]) continue;
        int u = e[i].u, v = e[i].v, w = e[i].w;
        ans[e[i].id] = query(u, v) - 1;
        calc(u, v, w);
    }
    for(int i = 0; i < m; i++) {
        printf("%d%c", ans[i] > 1e9 ? -1 : ans[i], " \n"[i+1==m]);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int LOG = 20;
int n, m, vis[N], ans[N], f[N];
int dep[N], p[N][20], mx[N][20], eid[N];
struct Edge {
    int u, v, w, id;
    void read() {
        scanf("%d%d%d", &u, &v, &w);
    }
    bool operator < (const Edge &rhs) const {
        return w < rhs.w;
    }
}e[N];
vector<Edge> G[N];
int find(int x) {
    return ~f[x] ? f[x] = find(f[x]) : x;
}
void dfs(int u, int pre) {
    for(auto e : G[u]) {
        int v = e.v, w = e.w;
        if(v == pre) continue;
        eid[v] = e.id;
        dep[v] = dep[u] + 1;
        p[v][0] = u;
        mx[v][0] = w;
        for(int i = 1; i < LOG; i++) {
            p[v][i] = p[p[v][i-1]][i-1];
            mx[v][i] = max(mx[v][i-1], mx[p[v][i-1]][i-1]);
        }
        dfs(v, u);
    }
}
int query(int u, int v) {
    int ans = 0;
    if(dep[u] < dep[v]) swap(u, v);
    for(int i = 0; i < LOG; i++) {
        if((dep[u] - dep[v])>>i&1) {
            ans = max(ans, mx[u][i]);
            u = p[u][i];
        }
    }
    if(u == v) return ans;
    for(int i = LOG - 1; ~i; i--) {
        if(p[u][i] != p[v][i]) {
            ans = max(ans, max(mx[u][i], mx[v][i]));
            u = p[u][i];
            v = p[v][i];
        }
    }
    return max(ans, max(mx[u][0], mx[v][0]));
}
void calc(int u, int v, int w) {
    while(find(u) != find(v)) {
        if(dep[find(u)] < dep[find(v)]) swap(u, v);
        int fu = find(u);
        ans[eid[fu]] = min(ans[eid[fu]], w - 1);
        f[fu] = p[fu][0];
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++) {
        e[i].read();
        e[i].id = i;
    }
    sort(e, e+m);
    memset(f, -1, sizeof(f));
    for(int i = 0; i < m; i++) {
        int u = e[i].u, v = e[i].v, w = e[i].w;
        if(find(u) != find(v)) {
            vis[i] = 1;
            f[find(u)] = find(v);
            G[u].push_back((Edge){u, v, w, e[i].id});
            G[v].push_back((Edge){v, u, w, e[i].id});
        }
    }
    dfs(1, 0);
    memset(ans, 0x3f, sizeof(ans));
    memset(f, -1, sizeof(f));
    for(int i = 0; i < m; i++) {
        if(vis[i]) continue;
        int u = e[i].u, v = e[i].v, w = e[i].w;
        ans[e[i].id] = query(u, v) - 1;
        calc(u, v, w);
    }
    for(int i = 0; i < m; i++) {
        printf("%d%c", ans[i] > 1e9 ? -1 : ans[i], " \n"[i+1==m]);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int LOG = 20;
int n, m, vis[N], ans[N], f[N];
int dep[N], p[N][20], mx[N][20], eid[N];
struct Edge {
    int u, v, w, id;
    void read() {
        scanf("%d%d%d", &u, &v, &w);
    }
    bool operator < (const Edge &rhs) const {
        return w < rhs.w;
    }
}e[N];
vector<Edge> G[N];
int find(int x) {
    return ~f[x] ? f[x] = find(f[x]) : x;
}
void dfs(int u, int pre) {
    for(auto e : G[u]) {
        int v = e.v, w = e.w;
        if(v == pre) continue;
        eid[v] = e.id;
        dep[v] = dep[u] + 1;
        p[v][0] = u;
        mx[v][0] = w;
        for(int i = 1; i < LOG; i++) {
            p[v][i] = p[p[v][i-1]][i-1];
            mx[v][i] = max(mx[v][i-1], mx[p[v][i-1]][i-1]);
        }
        dfs(v, u);
    }
}
int query(int u, int v) {
    int ans = 0;
    if(dep[u] < dep[v]) swap(u, v);
    for(int i = 0; i < LOG; i++) {
        if((dep[u] - dep[v])>>i&1) {
            ans = max(ans, mx[u][i]);
            u = p[u][i];
        }
    }
    for(int i = LOG - 1; ~i; i--) {
        if(p[u][i] != p[v][i]) {
            ans = max(ans, max(mx[u][i], mx[v][i]));
            u = p[u][i];
            v = p[v][i];
        }
    }
    return max(ans, max(mx[u][0], mx[v][0]));
}
void calc(int u, int v, int w) {
    u = find(u); v = find(v);
    while(u != v) {
        if(dep[u] < dep[v]) swap(u, v);
        ans[eid[u]] = min(ans[eid[u]], w - 1);
        f[u] = p[u][0];
        u = p[u][0];
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++) {
        e[i].read();
        e[i].id = i;
    }
    sort(e, e+m);
    memset(f, -1, sizeof(f));
    for(int i = 0; i < m; i++) {
        int u = e[i].u, v = e[i].v, w = e[i].w;
        if(find(u) != find(v)) {
            vis[i] = 1;
            f[find(u)] = find(v);
            G[u].push_back((Edge){u, v, w, e[i].id});
            G[v].push_back((Edge){v, u, w, e[i].id});
        }
    }
    dfs(1, 0);
    memset(ans, 0x3f, sizeof(ans));
    memset(f, -1, sizeof(f));
    for(int i = 0; i < m; i++) {
        if(vis[i]) continue;
        int u = e[i].u, v = e[i].v, w = e[i].w;
        ans[e[i].id] = query(u, v) - 1;
        calc(u, v, w);
    }
    for(int i = 0; i < m; i++) {
        printf("%d%c", ans[i] > 1e9 ? -1 : ans[i], " \n"[i+1==m]);
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
const int inf=(1<<30);
const int N=201000;
int n,m,f[N],u,v,w,dep[N],ret[N];
vector<VI> E,Ew;
vector<PII> e[N];
int mark[N],p[N][20],pe[N][20],pid[N];
void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	for (auto w:e[u]) {
		int v=w.fi;
		if (v==f) continue;
		p[v][0]=u;
		pid[v]=w.se;
		pe[v][0]=Ew[pid[v]][0];
		dfs(v,u);
	}
}
int find(int x) {
	return f[x]==x?x:f[x]=find(f[x]);
}
#define LOGN 18
int lca(int u,int v) {
	if (dep[u]>dep[v]) swap(u,v);
	int mv=0;
	per(i,0,LOGN) if (dep[p[v][i]]>=dep[u]) mv=max(mv,pe[v][i]),v=p[v][i];
	if (u==v) return mv;
	per(i,0,LOGN) if (p[v][i]!=p[u][i]) mv=max(mv,max(pe[u][i],pe[v][i])),u=p[u][i],v=p[v][i];
	return max(mv,max(pe[u][0],pe[v][0]));
}
int main() {
	scanf("%d%d",&n,&m);
	rep(i,1,n+1) f[i]=i;
	rep(i,0,m) {
		scanf("%d%d%d",&u,&v,&w);
		E.pb(VI{w,u,v,i});
		Ew.pb(VI{w,u,v,i});
	}
	sort(all(E));
	rep(i,0,m) {
		int id=E[i][3],u=E[i][2],v=E[i][1];
		if (find(u)!=find(v)) {
			mark[id]=1;
			e[u].pb(mp(v,id)); e[v].pb(mp(u,id));
			f[find(u)]=find(v);
		}
	}
	dfs(1,0);
	rep(j,1,LOGN) {
		rep(i,1,n+1) {
			p[i][j]=p[p[i][j-1]][j-1];
			pe[i][j]=max(pe[i][j-1],pe[p[i][j-1]][j-1]);
		}
	}
	rep(i,1,n+1) f[i]=i;
	rep(i,0,m) ret[i]=inf;
	rep(i,0,m) {
		int id=E[i][3],u=E[i][2],v=E[i][1];
		if (!mark[id]) {
			ret[id]=lca(u,v);
			while (1) {
				u=find(u);
				v=find(v);
				if (u==v) break;
				if (dep[v]>dep[u]) swap(u,v);
				ret[pid[u]]=E[i][0];
				f[u]=find(p[u][0]);
			}
		}
	}
	rep(i,0,m) {
		if (ret[i]==inf) ret[i]=0;
		printf("%d\n",ret[i]-1);
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
const int inf=(1<<30);
const int N=201000;
int n,m,f[N],u,v,w,dep[N],ret[N];
vector<VI> E,Ew;
vector<PII> e[N];
int mark[N],p[N][20],pe[N][20],pid[N];
void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	for(auto w:e[u]) {
		int v=w.fi;
		if (v==f) continue;
		p[v][0]=u;
		pid[v]=w.se;
		pe[v][0]=Ew[pid[v]][0];
		dfs(v,u);
	}
}
int find(int x) {
	return f[x]==x?x:f[x]=find(f[x]);
}
#define LOGN 18
int lca(int u,int v) {
	if (dep[u]>dep[v]) swap(u,v);
	int mv=0;
	per(i,0,LOGN) if (dep[p[v][i]]>=dep[u]) mv=max(mv,pe[v][i]),v=p[v][i];
	if (u==v) return mv;
	per(i,0,LOGN) if (p[v][i]!=p[u][i]) mv=max(mv,max(pe[u][i],pe[v][i])),u=p[u][i],v=p[v][i];
	return max(mv,max(pe[u][0],pe[v][0]));
}
int main() {
	scanf("%d%d",&n,&m);
	rep(i,1,n+1) f[i]=i;
	rep(i,0,m) {
		scanf("%d%d%d",&u,&v,&w);
		E.pb(VI{w,u,v,i});
		Ew.pb(VI{w,u,v,i});
	}
	sort(all(E));
	rep(i,0,m) {
		int id=E[i][3],u=E[i][2],v=E[i][1];
		if (find(u)!=find(v)) {
			mark[id]=1;
			e[u].pb(mp(v,id)); e[v].pb(mp(u,id));
			f[find(u)]=find(v);
		}
	}
	dfs(1,0);
	rep(j,1,LOGN) {
		rep(i,1,n+1) {
			p[i][j]=p[p[i][j-1]][j-1];
			pe[i][j]=max(pe[i][j-1],pe[p[i][j-1]][j-1]);
		}
	}
	rep(i,1,n+1) f[i]=i;
	rep(i,0,m) ret[i]=inf;
	rep(i,0,m) {
		int id=E[i][3],u=E[i][2],v=E[i][1];
		if (!mark[id]) {
			ret[id]=lca(u,v);
			while (1) {
				u=find(u);
				v=find(v);
				if (u==v) break;
				if (dep[v]>dep[u]) swap(u,v);
				ret[pid[u]]=E[i][0];
				f[u]=find(p[u][0]);
			}
		}
	}
	rep(i,0,m)
	{
		if (ret[i]==inf) ret[i]=0;
		printf("%d\n",ret[i]-1);
		return 0;
}


#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#define N 210000
#define maxd 18
using namespace std;
struct node{int x,y,d,nex,id;}e[N],a[2*N];
struct node1{int par[20],f[20],dep;}t[N];
int n,fir[N],len,m,fa[N],r[N],ans[N],inf=(1e9)+10;
bool b[N];
bool cmp(node x,node y)
{
	if(x.d<y.d) return 1;
	return 0;
}
void ins(int x,int y,int d,int id)
{
	a[++len].y=y;a[len].d=d;a[len].id=id;a[len].nex=fir[x];fir[x]=len;
}
int find(int x)
{
	if(fa[x]!=x) fa[x]=find(fa[x]);
	return fa[x];
}
void dfs(int x,int fa,int d)
{
	t[x].dep=t[fa].dep+1;t[x].par[0]=fa;t[x].f[0]=d;
	for(int i=1;i<=maxd;i++)
	{
		t[x].par[i]=t[t[x].par[i-1]].par[i-1];
		t[x].f[i]=max(t[x].f[i-1],t[t[x].par[i-1]].f[i-1]);
	}
	for(int k=fir[x];k;k=a[k].nex)
	{
		int y=a[k].y;
		if(y==fa) continue;
		r[y]=a[k].id;
		dfs(y,x,a[k].d);
	}
}
int lca(int x,int y,int &d)
{
	d=0;
	if(t[x].dep<t[y].dep) swap(x,y);
	for(int i=maxd;i>=0;i--) if(t[t[x].par[i]].dep>=t[y].dep) {d=max(d,t[x].f[i]);x=t[x].par[i];}
	if(x==y) return x;
	for(int i=maxd;i>=0;i--)
		if(t[x].par[i]!=t[y].par[i])
		{
			d=max(d,max(t[x].f[i],t[y].f[i]));
			x=t[x].par[i];y=t[y].par[i];
		}
	d=max(d,max(t[x].f[0],t[y].f[0]));
	return t[x].par[0];
}
void solve(int x,int u,int d)
{
	x=find(x);
	while(t[x].dep>t[u].dep)
	{
		ans[r[x]]=min(ans[r[x]],d);
		int y=find(t[x].par[0]);
		fa[x]=y;
		x=find(x);
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].d),e[i].id=i;
	sort(e+1,e+m+1,cmp);
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=1;i<=m;i++)
	{
		int x=find(e[i].x),y=find(e[i].y);
		if(x!=y)
		{
			b[i]=1;
			fa[x]=y;
			ins(e[i].x,e[i].y,e[i].d,e[i].id);ins(e[i].y,e[i].x,e[i].d,e[i].id);
		}
	}
	dfs(1,0,0);
	memset(ans,63,sizeof(ans));
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=1;i<=m;i++)
		if(b[i]==0)
		{
			int x=e[i].x,y=e[i].y,u=lca(x,y,ans[e[i].id]);
			ans[e[i].id]--;
			solve(x,u,e[i].d-1);solve(y,u,e[i].d-1);
		}
	for(int i=1;i<=m;i++) if(ans[i]>inf) printf("-1 ");
	else printf("%d ",ans[i]);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
const int inf=(1<<30);
const int N=201000;
int n,m,f[N],u,v,w,dep[N],ret[N];
vector<VI> E,Ew;
vector<PII> e[N];
int mark[N],p[N][20],pe[N][20],pid[N];
void dfs(int u,int f) {
	dep[u]=dep[f]+1;
	for (auto w:e[u]) {
		int v=w.fi;
		if (v==f) continue;
		p[v][0]=u;
		pid[v]=w.se;
		pe[v][0]=Ew[pid[v]][0];
		dfs(v,u);
	}
}
int find(int x) {
	return f[x]==x?x:f[x]=find(f[x]);
}
#define LOGN 18
int lca(int u,int v) {
	if (dep[u]>dep[v]) swap(u,v);
	int mv=0;
	per(i,0,LOGN) if (dep[p[v][i]]>=dep[u]) mv=max(mv,pe[v][i]),v=p[v][i];
	if (u==v) return mv;
	per(i,0,LOGN) if (p[v][i]!=p[u][i]) mv=max(mv,max(pe[u][i],pe[v][i])),u=p[u][i],v=p[v][i];
	return max(mv,max(pe[u][0],pe[v][0]));
}
int main() {
	scanf("%d%d",&n,&m);
	rep(i,1,n+1) f[i]=i;
	rep(i,0,m) {
		scanf("%d%d%d",&u,&v,&w);
		E.pb(VI{w,u,v,i});
		Ew.pb(VI{w,u,v,i});
	}
	sort(all(E));
	rep(i,0,m) {
		int id=E[i][3],u=E[i][2],v=E[i][1];
		if (find(u)!=find(v)) {
			mark[id]=1;
			e[u].pb(mp(v,id)); e[v].pb(mp(u,id));
			f[find(u)]=find(v);
		}
	}
	dfs(1,0);
	rep(j,1,LOGN) {
		rep(i,1,n+1) {
			p[i][j]=p[p[i][j-1]][j-1];
			pe[i][j]=max(pe[i][j-1],pe[p[i][j-1]][j-1]);
		}
	}
	rep(i,1,n+1) f[i]=i;
	rep(i,0,m) ret[i]=inf;
	rep(i,0,m) {
		int id=E[i][3],u=E[i][2],v=E[i][1];
		if (!mark[id]) {
			ret[id]=lca(u,v);
			while (1) {
				u=find(u);
				v=find(v);
				if (u==v) break;
				if (dep[v]>dep[u]) swap(u,v);
				ret[pid[u]]=E[i][0];
				f[u]=find(p[u][0]);
			}
		}
	}
	rep(i,0,m) {
		if (ret[i]==inf) ret[i]=0;
		printf("%d\n",ret[i]-1);
	}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <bitset>
#include <cstdio>
#include <climits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;
typedef long long ll;
#define ln '\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define drep(i,n) int i = 0;for(;i<(n);++i)
#define FOR(i,s,e) for(int i=s;i<=(e);++i)
#define FORR(i,s,e) for(int i=s;i>=(e);--i)
#define fore(a, x) for(auto& a : x)
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ft first
#define sd second
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define Fill(a,b) memset(a,b,sizeof(a))
template<typename T1, typename T2> bool Max(T1& a, T2 b) { return a<b ? a = b, 1 : 0; }
template<typename T1, typename T2> bool Min(T1& a, T2 b) { return a>b ? a = b, 1 : 0; }

struct UnionFind {
  vector<int> data;
  void init(int n) { data.assign(n, -1); }
  bool un(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

const int N = 200001;
const int M = 18, inf = 1e9;
struct edge { int u, v, c, i ,in; bool operator<(const edge& e)const { return c<e.c; } };
struct node { int v, c, i; };
edge e[N];
vector<node> g[N];
int dep[N], par[M][N], mxv[M][N], lazy[M][N];
int ans[N], fa[N], ve[N];

void dfs(int i, int p) {
  fore(j, g[i]) if (j.v != p) {
    dep[j.v] = dep[i] + 1;
    par[0][j.v] = i;
    mxv[0][j.v] = j.c;
    ve[j.v] = j.i;
    lazy[0][j.v] = inf;
    dfs(j.v, i);
  }

}

int lca(int u, int v) {
  if (dep[u] < dep[v])swap(u, v);
  FORR(i, M - 1, 0) if (dep[par[i][u]] >= dep[v]) u = par[i][u];
  if (u == v)return u;
  FORR(i, M - 1, 0)if (par[i][u] != par[i][v])u = par[i][u], v = par[i][v];
  return par[0][u];
}

int getmx(int u, int p) {
  int ret = 0;
  FORR(i, M - 1, 0)if (dep[par[i][u]] >= dep[p]) {
    Max(ret, mxv[i][u]);
    u = par[i][u];
  }
  return ret;
}

int update(int u, int p, int c) {
  /*if (dep[u] <= dep[p]) return u;
  if (fa[u] == u) {
    Min(ans[ve[u]], c);
    fa[u] = par[0][u];
  }
  return fa[u] = update(fa[u], p, c);*/
  FORR(i, M - 1, 0)if (dep[par[i][u]] >= dep[p]) {
    Min(lazy[i][u], c);
    u = par[i][u];
  }
  return 0;
}

void solve() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    cin >> e[i].u >> e[i].v >> e[i].c;
    e[i].i = i;
    ans[i] = 1e9;
  }
  sort(e, e + m);
  UnionFind uf;
  uf.init(n + 1);
  rep(i, m) {
    if (uf.un(e[i].u, e[i].v)) {
      g[e[i].u].push_back({ e[i].v,e[i].c,e[i].i });
      g[e[i].v].push_back({ e[i].u,e[i].c,e[i].i });
      e[i].in = 1;
    }
  }
  //par[0][1] = 1;
  dfs(1, 0);

  rep(i, M - 1)FOR(j,1, n) {
    par[i + 1][j] = par[i][par[i][j]];
    mxv[i + 1][j] = max(mxv[i][j], mxv[i][par[i][j]]);
    lazy[i + 1][j] = inf;
  }
  //FOR(i, 1, n) fa[i] = i;

  rep(i, m) if(!e[i].in) {
    int u = e[i].u, v = e[i].v, c = e[i].c;
    int p = lca(u, v);
    ans[e[i].i] = max(getmx(u, p), getmx(v, p)) - 1;
    update(u, p, c - 1);
    update(v, p, c - 1);
  }
  FORR(i, M - 2, 0)FOR(j, 1, n) {
    Min(lazy[i][j], lazy[i + 1][j]);
    Min(lazy[i][par[i][j]], lazy[i + 1][j]);
  }
  FOR(i, 2, n) ans[ve[i]] = lazy[0][i];
  rep(i, m) {
    cout << ((ans[i] == 1e9) ? -1 : ans[i]) << ' ';
  }
  cout << ln;

}

void init() {

}

int main(void) {
  ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
  //freopen("C:\\Users\\LENOVO\\Documents\\Visual Studio 2015\\Projects\\Test\\Input\\in.in", "r", stdin);

  init();
  int TC = 1;
#if 0
  cin >> TC; cin.get();
#endif
  FOR(TI, 1, TC) {
    //cout << "Case #" << TI << ": ";
    solve();
  }

  return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <bitset>
#include <cstdio>
#include <climits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;
typedef long long ll;
#define ln '\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define drep(i,n) int i = 0;for(;i<(n);++i)
#define FOR(i,s,e) for(int i=s;i<=(e);++i)
#define FORR(i,s,e) for(int i=s;i>=(e);--i)
#define fore(a, x) for(auto& a : x)
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ft first
#define sd second
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define Fill(a,b) memset(a,b,sizeof(a))
template<typename T1, typename T2> bool Max(T1& a, T2 b) { return a<b ? a = b, 1 : 0; }
template<typename T1, typename T2> bool Min(T1& a, T2 b) { return a>b ? a = b, 1 : 0; }

struct UnionFind {
  vector<int> data;
  void init(int n) { data.assign(n, -1); }
  bool un(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

const int N = 200001;
const int M = 18, inf = 1e9;
struct edge { int u, v, c, i ,in; bool operator<(const edge& e)const { return c<e.c; } };
struct node { int v, c, i; };
edge e[N];
vector<node> g[N];
int dep[N], par[M][N], mxv[M][N], lazy[M][N];
int ans[N], fa[N], ve[N];

void dfs(int i, int p) {
  fore(j, g[i]) if (j.v != p) {
    dep[j.v] = dep[i] + 1;
    par[0][j.v] = i;
    mxv[0][j.v] = j.c;
    ve[j.v] = j.i;
    lazy[0][j.v] = inf;
    dfs(j.v, i);
  }

}

int lca(int u, int v) {
  if (dep[u] < dep[v])swap(u, v);
  FORR(i, M - 1, 0) if (dep[par[i][u]] >= dep[v]) u = par[i][u];
  if (u == v)return u;
  FORR(i, M - 1, 0)if (par[i][u] != par[i][v])u = par[i][u], v = par[i][v];
  return par[0][u];
}

int getmx(int u, int p) {
  int ret = 0;
  FORR(i, M - 1, 0)if (dep[par[i][u]] >= dep[p]) {
    Max(ret, mxv[i][u]);
    u = par[i][u];
  }
  return ret;
}

int update(int u, int p, int c) {
  /*if (dep[u] <= dep[p]) return u;
  if (fa[u] == u) {
    Min(ans[ve[u]], c);
    fa[u] = par[0][u];
  }
  return fa[u] = update(fa[u], p, c);*/
  FORR(i, M - 1, 0)if (dep[par[i][u]] >= dep[p]) {
    Min(lazy[i][u], c);
    u = par[i][u];
  }
  return 0;
}

void solve() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    cin >> e[i].u >> e[i].v >> e[i].c;
    e[i].i = i;
    ans[i] = 1e9;
  }
  sort(e, e + m);
  UnionFind uf;
  uf.init(n + 1);
  rep(i, m) {
    if (uf.un(e[i].u, e[i].v)) {
      g[e[i].u].push_back({ e[i].v,e[i].c,e[i].i });
      g[e[i].v].push_back({ e[i].u,e[i].c,e[i].i });
      e[i].in = 1;
    }
  }
  par[0][1] = 1;
  dfs(1, 1);

  rep(i, M - 1)FOR(j,1, n) {
    par[i + 1][j] = par[i][par[i][j]];
    mxv[i + 1][j] = max(mxv[i][j], mxv[i][par[i][j]]);
    lazy[i + 1][j] = inf;
  }
  //FOR(i, 1, n) fa[i] = i;

  rep(i, m) if(!e[i].in) {
    int u = e[i].u, v = e[i].v, c = e[i].c;
    int p = lca(u, v);
    ans[e[i].i] = max(getmx(u, p), getmx(v, p)) - 1;
    update(u, p, c - 1);
    update(v, p, c - 1);
  }
  FORR(i, M - 2, 0)FOR(j, 1, n) {
    Min(lazy[i][j], lazy[i + 1][j]);
    Min(lazy[i][par[i][j]], lazy[i + 1][j]);
  }
  FOR(i, 2, n) ans[ve[i]] = lazy[0][i];
  rep(i, m) {
    cout << ((ans[i] == 1e9) ? -1 : ans[i]) << ' ';
  }
  cout << ln;

}

void init() {

}

int main(void) {
  ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
  //freopen("C:\\Users\\LENOVO\\Documents\\Visual Studio 2015\\Projects\\Test\\Input\\in.in", "r", stdin);

  init();
  int TC = 1;
#if 0
  cin >> TC; cin.get();
#endif
  FOR(TI, 1, TC) {
    //cout << "Case #" << TI << ": ";
    solve();
  }

  return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <bitset>
#include <cstdio>
#include <climits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;
typedef long long ll;
#define ln '\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define drep(i,n) int i = 0;for(;i<(n);++i)
#define FOR(i,s,e) for(int i=s;i<=(e);++i)
#define FORR(i,s,e) for(int i=s;i>=(e);--i)
#define fore(a, x) for(auto& a : x)
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ft first
#define sd second
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define Fill(a,b) memset(a,b,sizeof(a))
template<typename T1, typename T2> bool Max(T1& a, T2 b) { return a<b ? a = b, 1 : 0; }
template<typename T1, typename T2> bool Min(T1& a, T2 b) { return a>b ? a = b, 1 : 0; }

struct UnionFind {
  vector<int> data;
  void init(int n) { data.assign(n, -1); }
  bool un(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

const int N = 200001;
const int M = 18;
struct edge { int u, v, c, i ,in; bool operator<(const edge& e)const { return c<e.c; } };
struct node { int v, c, i; };
edge e[N];
vector<node> g[N];
int dep[N], par[M][N], mxv[M][N];
int ans[N], fa[N], ve[N];

void dfs(int i, int p) {
  fore(j, g[i]) if (j.v != p) {
    dep[j.v] = dep[i] + 1;
    par[0][j.v] = i;
    mxv[0][j.v] = j.c;
    ve[j.v] = j.i;
    dfs(j.v, i);
  }

}

int lca(int u, int v) {
  if (dep[u] < dep[v])swap(u, v);
  FORR(i, M - 1, 0) if (dep[par[i][u]] >= dep[v]) u = par[i][u];
  if (u == v)return u;
  FORR(i, M - 1, 0)if (par[i][u] != par[i][v])u = par[i][u], v = par[i][v];
  return par[0][u];
}

int getmx(int u, int p) {
  int ret = 0;
  FORR(i, M - 1, 0)if (dep[par[i][u]] >= dep[p]) {
    Max(ret, mxv[i][u]);
    u = par[i][u];
  }
  return ret;
}

int update(int u, int p, int c) {
  if (dep[u] <= dep[p]) return u;
  if (fa[u] == u) {
    Min(ans[ve[u]], c);
    fa[u] = par[0][u];
  }
  return fa[u] = update(fa[u], p, c);

}

void solve() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    cin >> e[i].u >> e[i].v >> e[i].c;
    e[i].i = i;
    ans[i] = 1e9;
  }
  sort(e, e + m);
  UnionFind uf;
  uf.init(n + 1);
  rep(i, m) {
    if (uf.un(e[i].u, e[i].v)) {
      g[e[i].u].push_back({ e[i].v,e[i].c,e[i].i });
      g[e[i].v].push_back({ e[i].u,e[i].c,e[i].i });
      e[i].in = 1;
    }
  }
  par[0][1] = 1;
  dfs(1, 1);

  rep(i, M - 1)FOR(j,1, n) {
    par[i + 1][j] = par[i][par[i][j]];
    mxv[i + 1][j] = max(mxv[i][j], mxv[i][par[i][j]]);
  }
  FOR(i, 1, n) fa[i] = i;

  rep(i, m) if(!e[i].in) {
    int u = e[i].u, v = e[i].v, c = e[i].c;
    int p = lca(u, v);
    ans[e[i].i] = max(getmx(u, p), getmx(v, p)) - 1;
    update(u, p, c - 1);
    update(v, p, c - 1);
  }
  rep(i, m) {
    cout << ((ans[i] == 1e9) ? -1 : ans[i]) << ' ';
  }
  cout << ln;

}

void init() {

}

int main(void) {
  ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
  //freopen("C:\\Users\\LENOVO\\Documents\\Visual Studio 2015\\Projects\\Test\\Input\\in.in", "r", stdin);

  init();
  int TC = 1;
#if 0
  cin >> TC; cin.get();
#endif
  FOR(TI, 1, TC) {
    //cout << "Case #" << TI << ": ";
    solve();
  }

  return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <bitset>
#include <cstdio>
#include <climits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;
typedef long long ll;
#define ln '\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define drep(i,n) int i = 0;for(;i<(n);++i)
#define FOR(i,s,e) for(int i=s;i<=(e);++i)
#define FORR(i,s,e) for(int i=s;i>=(e);--i)
#define fore(a, x) for(auto& a : x)
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ft first
#define sd second
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define Fill(a,b) memset(a,b,sizeof(a))
template<typename T1, typename T2> bool Max(T1& a, T2 b) { return a<b ? a = b, 1 : 0; }
template<typename T1, typename T2> bool Min(T1& a, T2 b) { return a>b ? a = b, 1 : 0; }

struct UnionFind {
  vector<int> data;
  void init(int n) { data.assign(n, -1); }
  bool un(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

const int N = 200001;
const int M = 18, inf = 1e9;
struct edge { int u, v, c, i ,in; bool operator<(const edge& e)const { return c<e.c; } };
struct node { int v, c, i; };
edge e[N];
vector<node> g[N];
int dep[N], par[M][N], mxv[M][N], lazy[M][N];
int ans[N], fa[N], ve[N];

void dfs(int i, int p) {
  fore(j, g[i]) if (j.v != p) {
    dep[j.v] = dep[i] + 1;
    par[0][j.v] = i;
    mxv[0][j.v] = j.c;
    ve[j.v] = j.i;
    lazy[0][j.v] = inf;
    dfs(j.v, i);
  }

}

int lca(int u, int v) {
  if (dep[u] < dep[v])swap(u, v);
  FORR(i, M - 1, 0) if (dep[par[i][u]] >= dep[v]) u = par[i][u];
  if (u == v)return u;
  FORR(i, M - 1, 0)if (par[i][u] != par[i][v])u = par[i][u], v = par[i][v];
  return par[0][u];
}

int getmx(int u, int p) {
  int ret = 0;
  FORR(i, M - 1, 0)if (dep[par[i][u]] >= dep[p]) {
    Max(ret, mxv[i][u]);
    u = par[i][u];
  }
  return ret;
}

int update(int u, int p, int c) {
  /*if (dep[u] <= dep[p]) return u;
  if (fa[u] == u) {
    Min(ans[ve[u]], c);
    fa[u] = par[0][u];
  }
  return fa[u] = update(fa[u], p, c);*/
  FORR(i, M - 1, 0)if (dep[par[i][u]] >= dep[p]) {
    Min(lazy[i][u], c);
  }
  return 0;
}

void solve() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    cin >> e[i].u >> e[i].v >> e[i].c;
    e[i].i = i;
    ans[i] = 1e9;
  }
  sort(e, e + m);
  UnionFind uf;
  uf.init(n + 1);
  rep(i, m) {
    if (uf.un(e[i].u, e[i].v)) {
      g[e[i].u].push_back({ e[i].v,e[i].c,e[i].i });
      g[e[i].v].push_back({ e[i].u,e[i].c,e[i].i });
      e[i].in = 1;
    }
  }
  par[0][1] = 1;
  dfs(1, 1);

  rep(i, M - 1)FOR(j,1, n) {
    par[i + 1][j] = par[i][par[i][j]];
    mxv[i + 1][j] = max(mxv[i][j], mxv[i][par[i][j]]);
    lazy[i + 1][j] = inf;
  }
  //FOR(i, 1, n) fa[i] = i;

  rep(i, m) if(!e[i].in) {
    int u = e[i].u, v = e[i].v, c = e[i].c;
    int p = lca(u, v);
    ans[e[i].i] = max(getmx(u, p), getmx(v, p)) - 1;
    update(u, p, c - 1);
    update(v, p, c - 1);
  }
  FORR(i, M - 2, 0)FOR(j, 1, n) {
    Min(lazy[i][j], lazy[i + 1][j]);
    Min(lazy[i][par[i][j]], lazy[i + 1][j]);
  }
  FOR(i, 2, n) ans[ve[i]] = lazy[0][i];
  rep(i, m) {
    cout << ((ans[i] == 1e9) ? -1 : ans[i]) << ' ';
  }
  cout << ln;

}

void init() {

}

int main(void) {
  ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
  //freopen("C:\\Users\\LENOVO\\Documents\\Visual Studio 2015\\Projects\\Test\\Input\\in.in", "r", stdin);

  init();
  int TC = 1;
#if 0
  cin >> TC; cin.get();
#endif
  FOR(TI, 1, TC) {
    //cout << "Case #" << TI << ": ";
    solve();
  }

  return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <bitset>
#include <cstdio>
#include <climits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;
typedef long long ll;
#define ln '\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define drep(i,n) int i = 0;for(;i<(n);++i)
#define FOR(i,s,e) for(int i=s;i<=(e);++i)
#define FORR(i,s,e) for(int i=s;i>=(e);--i)
#define fore(a, x) for(auto& a : x)
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ft first
#define sd second
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define Fill(a,b) memset(a,b,sizeof(a))
template<typename T1, typename T2> bool Max(T1& a, T2 b) { return a<b ? a = b, 1 : 0; }
template<typename T1, typename T2> bool Min(T1& a, T2 b) { return a>b ? a = b, 1 : 0; }

struct UnionFind {
  vector<int> data;
  void init(int n) { data.assign(n, -1); }
  bool un(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

const int N = 200001;
const int M = 18, inf = 1e9;
struct edge { int u, v, c, i ,in; bool operator<(const edge& e)const { return c<e.c; } };
struct node { int v, c, i; };
edge e[N];
vector<node> g[N];
int dep[N], par[M][N], mxv[M][N], lazy[M][N];
int ans[N], fa[N], ve[N];

void dfs(int i, int p) {
  fore(j, g[i]) if (j.v != p) {
    dep[j.v] = dep[i] + 1;
    par[0][j.v] = i;
    mxv[0][j.v] = j.c;
    ve[j.v] = j.i;
    lazy[0][j.v] = inf;
    dfs(j.v, i);
  }

}

int lca(int u, int v) {
  if (dep[u] < dep[v])swap(u, v);
  FORR(i, M - 1, 0) if (dep[par[i][u]] >= dep[v]) u = par[i][u];
  if (u == v)return u;
  FORR(i, M - 1, 0)if (par[i][u] != par[i][v])u = par[i][u], v = par[i][v];
  return par[0][u];
}

int getmx(int u, int p) {
  int ret = 0;
  FORR(i, M - 1, 0)if (dep[par[i][u]] >= dep[p]) {
    Max(ret, mxv[i][u]);
    u = par[i][u];
  }
  return ret;
}

int update(int u, int p, int c) {
  /*if (dep[u] <= dep[p]) return u;
  if (fa[u] == u) {
    Min(ans[ve[u]], c);
    fa[u] = par[0][u];
  }
  return fa[u] = update(fa[u], p, c);*/
  FORR(i, M - 1, 0)if (dep[par[i][u]] >= dep[p]) {
    Min(lazy[i][u], c);
  }
  return 0;
}

void solve() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    cin >> e[i].u >> e[i].v >> e[i].c;
    e[i].i = i;
    ans[i] = 1e9;
  }
  sort(e, e + m);
  UnionFind uf;
  uf.init(n + 1);
  rep(i, m) {
    if (uf.un(e[i].u, e[i].v)) {
      g[e[i].u].push_back({ e[i].v,e[i].c,e[i].i });
      g[e[i].v].push_back({ e[i].u,e[i].c,e[i].i });
      e[i].in = 1;
    }
  }
  //par[0][1] = 1;
  dfs(1, 0);

  rep(i, M - 1)FOR(j,1, n) {
    par[i + 1][j] = par[i][par[i][j]];
    mxv[i + 1][j] = max(mxv[i][j], mxv[i][par[i][j]]);
    lazy[i + 1][j] = inf;
  }
  //FOR(i, 1, n) fa[i] = i;

  rep(i, m) if(!e[i].in) {
    int u = e[i].u, v = e[i].v, c = e[i].c;
    int p = lca(u, v);
    ans[e[i].i] = max(getmx(u, p), getmx(v, p)) - 1;
    update(u, p, c - 1);
    update(v, p, c - 1);
  }
  FORR(i, M - 2, 0)FOR(j, 1, n) {
    Min(lazy[i][j], lazy[i + 1][j]);
    Min(lazy[i][par[i][j]], lazy[i + 1][j]);
  }
  FOR(i, 2, n) ans[ve[i]] = lazy[0][i];
  rep(i, m) {
    cout << ((ans[i] == 1e9) ? -1 : ans[i]) << ' ';
  }
  cout << ln;

}

void init() {

}

int main(void) {
  ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
  //freopen("C:\\Users\\LENOVO\\Documents\\Visual Studio 2015\\Projects\\Test\\Input\\in.in", "r", stdin);

  init();
  int TC = 1;
#if 0
  cin >> TC; cin.get();
#endif
  FOR(TI, 1, TC) {
    //cout << "Case #" << TI << ": ";
    solve();
  }

  return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <bitset>
#include <cstdio>
#include <climits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;
typedef long long ll;
#define ln '\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define drep(i,n) int i = 0;for(;i<(n);++i)
#define FOR(i,s,e) for(int i=s;i<=(e);++i)
#define FORR(i,s,e) for(int i=s;i>=(e);--i)
#define fore(a, x) for(auto& a : x)
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ft first
#define sd second
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define Fill(a,b) memset(a,b,sizeof(a))
template<typename T1, typename T2> bool Max(T1& a, T2 b) { return a<b ? a = b, 1 : 0; }
template<typename T1, typename T2> bool Min(T1& a, T2 b) { return a>b ? a = b, 1 : 0; }

struct UnionFind {
  vector<int> data;
  void init(int n) { data.assign(n, -1); }
  bool un(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

const int N = 200001;
const int M = 18;
struct edge { int u, v, c, i ,in; bool operator<(const edge& e)const { return c<e.c; } };
struct node { int v, c, i; };
edge e[N];
vector<node> g[N];
int dep[N], par[M][N], mxv[M][N];
int ans[N], fa[N], ve[N];

void dfs(int i, int p) {
  fore(j, g[i]) if (j.v != p) {
    dep[j.v] = dep[i] + 1;
    par[0][j.v] = i;
    mxv[0][j.v] = j.c;
    ve[j.v] = j.i;
    dfs(j.v, i);
  }

}

int lca(int u, int v) {
  if (dep[u] < dep[v])swap(u, v);
  FORR(i, M - 1, 0) if (dep[par[i][u]] >= dep[v]) u = par[i][u];
  if (u == v)return u;
  FORR(i, M - 1, 0)if (par[i][u] != par[i][v])u = par[i][u], v = par[i][v];
  return par[0][u];
}

int getmx(int u, int p) {
  int ret = 0;
  FORR(i, M - 1, 0)if (dep[par[i][u]] >= dep[p]) {
    Max(ret, mxv[i][u]);
    u = par[i][u];
  }
  return ret;
}

int update(int u, int p, int c) {
  if (dep[u] <= dep[p]) return u;
  if (fa[u] == u) {
    Min(ans[ve[u]], c);
    fa[u] = par[0][u];
  }
  return fa[u] = update(fa[u], p, c);

}

void solve() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    cin >> e[i].u >> e[i].v >> e[i].c;
    e[i].i = i;
    ans[i] = 1e9;
  }
  sort(e, e + m);
  UnionFind uf;
  uf.init(n + 1);
  rep(i, m) {
    if (uf.un(e[i].u, e[i].v)) {
      g[e[i].u].push_back({ e[i].v,e[i].c,e[i].i });
      g[e[i].v].push_back({ e[i].u,e[i].c,e[i].i });
      e[i].in = 1;
    }
  }
  //par[0][1] = 1;
  dfs(1, 0);

  rep(i, M - 1)FOR(j,1, n) {
    par[i + 1][j] = par[i][par[i][j]];
    mxv[i + 1][j] = max(mxv[i][j], mxv[i][par[i][j]]);
  }
  FOR(i, 1, n) fa[i] = i;

  rep(i, m) if(!e[i].in) {
    int u = e[i].u, v = e[i].v, c = e[i].c;
    int p = lca(u, v);
    ans[e[i].i] = max(getmx(u, p), getmx(v, p)) - 1;
    update(u, p, c - 1);
    update(v, p, c - 1);
  }
  rep(i, m) {
    cout << ((ans[i] == 1e9) ? -1 : ans[i]) << ' ';
  }
  cout << ln;

}

void init() {

}

int main(void) {
  ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
  //freopen("C:\\Users\\LENOVO\\Documents\\Visual Studio 2015\\Projects\\Test\\Input\\in.in", "r", stdin);

  init();
  int TC = 1;
#if 0
  cin >> TC; cin.get();
#endif
  FOR(TI, 1, TC) {
    //cout << "Case #" << TI << ": ";
    solve();
  }

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  return {par[0][v], max(res, max(emax[0][v], emax[0][u]))};
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  if(sz[v] > sz[nv]) {
    up[v] = nv; sz[nv] += sz[v];
  }
  else {
    up[nv] = v; sz[v] += sz[nv];
  }

  v = nv;
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	//cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      //cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	//cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      //cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	//cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        if(!is_father(e.v, e.u)) goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	//cerr << val[i] << " ";
  }
  //cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      //cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      //cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  return {par[0][v], max(res, max(emax[0][v], emax[0][u]))};
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  if(sz[v] > sz[nv] || v == 1) {
    up[nv] = v; sz[v] += sz[nv];
  }
  else {
    up[v] = nv; sz[nv] += sz[v];
  }
  v = up[nv];
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	//cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      //cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	//cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      //cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	//cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        else goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	//cerr << val[i] << " ";
  }
  //cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      //cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      //cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int label[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  return {par[0][v], max(res, max(emax[0][v], emax[0][u]))};
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  if(sz[v] > sz[nv]) swap(v, nv);
  up[v] = nv;
  sz[nv] += sz[v];
  label[nv] = (depth[v] < depth[nv] ? v : nv);
  v = label[nv];
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	//cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      //cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	//cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      //cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	//cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        if(!is_father(e.v, e.u)) goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	//cerr << val[i] << " ";
  }
  //cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      //cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      //cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) > depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  else {
    return {par[0][v], max(res, emax[0][v])};
  }
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  // if(sz[e.u] > sz[neu]) swap(e.u, neu); !!!
  up[v] = nv;
  v = nv;
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	//cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      //cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	//cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      //cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	//cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        if(!is_father(e.v, e.u)) goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	//cerr << val[i] << " ";
  }
  //cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      //cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      //cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  return {par[0][v], max(res, max(emax[0][v], emax[0][u]))};
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  if(sz[v] > sz[nv]) {
    up[v] = nv; sz[nv] += sz[v];
  }
  else {
    up[nv] = v; sz[v] += sz[nv];
  }
  v = up[nv];
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	//cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      //cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	//cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      //cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	//cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        else goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	//cerr << val[i] << " ";
  }
  //cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      //cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      //cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  return {par[0][v], max(res, max(emax[0][v], emax[0][u]))};
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  // if(sz[e.u] > sz[neu]) swap(e.u, neu); !!!
  up[v] = nv;
  v = nv;
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        if(!is_father(e.v, e.u)) goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	cerr << val[i] << " ";
  }
  cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  return {par[0][v], max(res, max(emax[0][v], emax[0][u]))};
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  if(sz[v] > sz[nv]) swap(v, nv);
  up[v] = nv;
  v = nv;
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	//cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      //cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	//cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      //cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	//cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        if(!is_father(e.v, e.u)) goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	//cerr << val[i] << " ";
  }
  //cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      //cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      //cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  return {par[0][v], max(res, max(emax[0][v], emax[0][u]))};
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  if(sz[v] > sz[nv]) swap(v, nv);
  up[v] = nv; sz[nv] += sz[v];
  v = nv;
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	//cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      //cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	//cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      //cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	//cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        if(!is_father(e.v, e.u)) goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	//cerr << val[i] << " ";
  }
  //cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      //cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      //cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}