#include<iostream>

#include<cstring>

using namespace std;

int main()
{
    int n, x;

    cin >> n >> x;

    int a[n + 5], b[n + 5];

    for(int i = 1; i <= n; i++)
    {
        cin >> a[i] >> b[i];
    }

    int res = 0, tmp = 1;

    for(int i = 1; i <= n; i++)
    {
         while(tmp + x <= a[i]) tmp += x;

         res += b[i] - tmp + 1;

         if((a[i + 1] - b[i]) % x == 0) tmp = a[i + 1];
         else tmp = b[i] + 1;
    }

    cout << res;
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#include <set>
#include <bitset>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;

typedef long long int ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
// #define forstl(i, s)    for(__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
#define WHITE 1
#define BLACK -1

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.in" , "r" , stdin);
    //freopen("out.out" , "w" , stdout);
#endif
    int n,x,ans=0,q,w,t=1;
    cin >> n >> x;
    while(n--){
        cin >> q >> w;
        t += ((q-t)/x)*x;
        ans += w-t+1;
    }
    cout << ans << endl;
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#include <set>
#include <bitset>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;

typedef long long int ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
// #define forstl(i, s)    for(__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
#define WHITE 1
#define BLACK -1

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.in" , "r" , stdin);
    //freopen("out.out" , "w" , stdout);
#endif
    int n,x,ans=0,q,w,t=1;
    cin >> n >> x;
    while(n--){
        cin >> q >> w;
        t += ((q-t)/x)*x;
        ans += w-t+1;
        t = w+1;
    }
    cout << ans << endl;
    return 0;
}


#include<stdio.h>
int main(){
	int n,x;
	scanf("%d %d",&n,&x);
	int min=0;
	int a,b,B=-1;
	int i=0;
	while(i<n){
		scanf("%d %d",&a,&b);
		a=(a-1)/x;
		b=(b-1)/x;
		if(a==B) min--;
		min+=(b-a+1);
		B=b;
		i++;
	}
	printf("%d",min*x);
	return 0;
}


#include<stdio.h>
int main(){
	int n,x;
	scanf("%d %d",&n,&x);
	int min=0;
	int a,b,b_aux,B=-1;
	int i=0;
	while(i<n){
		scanf("%d %d",&a,&b);
		a=(a-1)/x;
		b_aux=b;
		b=(b-1)/x;
		if(a==B) min--;
		min+=(b-a+1);
		B=b;
		i++;
	}
	printf("%d",min*x-(B+1)*x+b_aux);
	return 0;
}


#include<stdio.h>
int main(){
	int n,x;
	scanf("%d %d",&n,&x);
	int min=0;
	int a,b,actual=1;
	while(n--){
		scanf("%d %d",&a,&b);
		actual=actual+((a-actual)/x)*x;
		min+=b-actual+1;
		actual=b+1;
	}
	printf("%d",min);
	return 0;
}


#include<stdio.h>

int n,x,l,r,start=1,len=0;

int main()
{
    scanf("%d%d",&n,&x);
    while(n--)
    {
        scanf("%d%d",&l,&r);
        start+=((l-start)/x)*x;
//      printf("start=%d\n",start);
        len+=r-start+1;
        start=r;
//      printf("len=%d\n",len);
    }
    printf("%d\n",len);
    return 0;
}


#include<stdio.h>

int n,x,l,r,start=1,len=0;

int main()
{
    scanf("%d%d",&n,&x);
    while(n--)
    {
        scanf("%d%d",&l,&r);
        start+=((l-start)/x)*x;
//      printf("start=%d\n",start);
        len+=r-start+1;
//      printf("len=%d\n",len);
    }
    printf("%d\n",len);
    return 0;
}


#include<stdio.h>

int n,x,l,r,start=1,len=0;

int main()
{
    scanf("%d%d",&n,&x);
    while(n--)
    {
        scanf("%d%d",&l,&r);
        start+=((l-start)/x)*x;
//        printf("start=%d\n",start);
        len+=r-start+1;
        start=r+1;
//        printf("start=%d len=%d\n",start,len);
    }
    printf("%d\n",len);
    return 0;
}


#include <stdio.h>

int main(void) {
    // your code goes here
long int i,j,k,a[100000],b[100000],count=0,result=0,n,x;
    scanf("%d%d",&n,&x);
    for(i=0;i<x;i++){
        scanf("%d%d",&a[i],&b[i]);
    }
        j=1;
        k=0;
    //  printf("%d ",b[n-1]);
    while(j<=b[n-1]){

        if(j>=b[k])
            k++;

        while(j+x<=a[k]){
            j=j+x;
        }

        result++;
        j++;
//      printf("%d ",result);
    }
    printf("%d\n",result);
    return 0;
}


#include <stdio.h>

int main(void) {
    // your code goes here
    int i,j,k,a[100000],b[100000],count=0,result=0,n,x;
    scanf("%d%d",&n,&x);
    for(i=0;i<x;i++){
        scanf("%d%d",&a[i],&b[i]);
    }
        j=1;
        k=0;
    //  printf("%d ",b[n-1]);
    while(j<=b[n-1]){

        if(j>b[k])
            k++;

        while(j+x<=a[k]){
            j=j+x;
        }

        result++;
        j++;
//      printf("%d ",result);
    }
    printf("%d",result);
    return 0;
}


#include <stdio.h>

int main(void) {
    // your code goes here
long int i,j,k,a[100000],b[100000],count=0,result=0,n,x;
    scanf("%d%d",&n,&x);
    for(i=0;i<n;i++){
        scanf("%d%d",&a[i],&b[i]);
    }
        j=1;
        k=0;
    //  printf("%d ",b[n-1]);
    while(j<=b[n-1]){

        if(j>b[k])
            k++;

        while(j+x<=a[k]){
            j=j+x;
        }

        result++;
        j++;
//      printf("%d ",result);
    }
    printf("%d\n",result);
    return 0;
}


#include <stdio.h>

int main(void) {
    // your code goes here
long int i,j,k,a[100000],b[100000],count=0,result=0,n,x;
    scanf("%d%d",&n,&x);
    for(i=0;i<x;i++){
        scanf("%d%d",&a[i],&b[i]);
    }
        j=1;
        k=0;
    //  printf("%d ",b[n-1]);
    while(j<=b[n-1]){

        if(j>b[k])
            k++;

        while(j+x<=a[k]){
            j=j+x;
        }

        result++;
        j++;
//      printf("%d ",result);
    }
    printf("%d",result);
    return 0;
}


#include <stdio.h>

int main(void) {
    int i;
    int n, x, l[51], r[51] = {0};
    int ans;

    scanf("%d %d", &n, &x);
    for (i = 1; i <= n; i++)
        scanf("%d %d", l + i, r + i);

    ans = 0;
    for (i = 1; i <= n; i++)
        ans += (l[i] - r[i - 1] - 1) % x + r[i] - l[i] + 1;

    printf("%d\n", ans);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,x,count = 0,count1 = 0;
    scanf("%d %d",&n,&x);
    int arr[n][2];
    int i = 0,j = 0;
    for (i = 0; i < n;i++){
       for (j = 0; j < 2;j++){
        scanf("%d",&arr[i][j]);
        count = (arr[i][j] - count);
       }
        count1+= count + 1;
        count = 0;
    }
    int current = 1;
    for (i = 0; i < n;i++){
       for (j = 0; j < 1;j++){
        if ((current + x) > arr[i][j])
            count1+= (arr[i][j] - current) ;

            else if ((current + x) < arr[i][j]){
                while((current + x) < arr[i][j]){
                current+=x;
                }
                count1+= (arr[i][j] - current);
            }

       }
       current = arr[i][j] + 1;
    }

    printf("%d",count1);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,x,count = 0,count1 = 0;
    scanf("%d %d",&n,&x);
    int arr[n][2];
    int i = 0,j = 0;
    for (i = 0; i < n;i++){
       for (j = 0; j < 2;j++){
        scanf("%d",&arr[i][j]);
        count = (arr[i][j] - count);
       }
        count1+= count + 1;
        count = 0;
    }
    int current = 1;
    for (i = 0; i < n;i++){
       for (j = 0; j < 1;j++){
        if ((current + x) > arr[i][j])
            count1+= (arr[i][j] - current) ;

            else if ((current + x) <= arr[i][j]){
                while((current + x) <= arr[i][j]){
                current+=x;
                }
                count1+= (arr[i][j] - current);
            }

       }
       current = arr[i][j] + 1;
    }

    printf("%d",count1);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,x,count = 0,count1 = 0;
    scanf("%d %d",&n,&x);
    int arr[n][2];
    int i = 0,j = 0;
    for (i = 0; i < n;i++){
       for (j = 0; j < 2;j++){
        scanf("%d",&arr[i][j]);
        count = (arr[i][j] - count);
       }
        count1+= count + 1;
        count = 0;
    }
    int current = 1;
    for (i = 0; i < n;i++){
       for (j = 0; j < 1;j++){
        if ((current + x) > arr[i][j])
            count1+= (arr[i][j] - current) ;
            else if ((current + x) < arr[i][j])
                count1+= (arr[i][j] - (current + x));
       }
       current = arr[i][j] + 1;
    }

    printf("%d",count1);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define N 1000001
int main()
{
      int n,t,m=0,i,sum=0,l=1,a[300001];

      int b[10001];
      scanf("%d %d",&n,&t);
      for(i=1;i<=n;i++)
      {
            scanf("%d %d",&a[i],&b[i]);
            if(i=1)
            {
                  sum+=(b[i]-a[i]+1)+(a[i]-1)%t;
            }
            else
            sum+=(b[i]-a[i])+(a[i]-b[i-1]-1)%t;
      }
      printf("%d",sum);
      return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define N 1000001
int main()
{
      int n,t,m=0,i,sum=0,l=1,a[300001];

      int b[10001];
      scanf("%d %d",&n,&t);
      for(i=1;i<=n;i++)
      {
            scanf("%d %d",&a[i],&b[i]);
            if(i==1)
            {
                  sum+=(b[i]-a[i]+1)+(a[i]-1)%t;
            }
            else
            sum+=(b[i]-a[i])+(a[i]-b[i-1]-1)%t;
      }
      printf("%d",sum);
      return 0;
}


#include <stdio.h>

int main()
{
    int n;
    scanf("%d", &n);
    int x;
    scanf("%d", &x);
    int current_minute = 1;
    int minutes = 0;
    int i = 0;
    while (i < n)
    {
        int l;
        int r;
        scanf("%d", &l);
        scanf("%d", &r);
        while (l - current_minute > x)
        {
            current_minute = current_minute + x;
        }
        minutes = minutes + r - current_minute + 1;
        current_minute = r + 1;
        i++;
    }
    printf("%d", minutes);
    return 0;
}


#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

int main()
{
    int n;
    scanf("%d", &n);
    int x;
    scanf("%d", &x);
    int current_minute = 0;
    int minutes = 0;
    int i = 0;
    while (i < n)
    {
        int l;
        int r;
        scanf("%d", &l);
        scanf("%d", &r);
        while (l - current_minute > x)
        {
            current_minute = current_minute + 3;
        }
        minutes = minutes + r - current_minute;
        current_minute = r;
        i++;
    }
    printf("%d", minutes);
    return 0;
}


#include <stdio.h>

int main()
{
    int n;
    scanf("%d", &n);
    int x;
    scanf("%d", &x);
    int current_minute = 0;
    int minutes = 0;
    int i = 0;
    while (i < n)
    {
        int l;
        int r;
        scanf("%d", &l);
        scanf("%d", &r);
        while (l - current_minute > x)
        {
            current_minute = current_minute + x;
        }
        minutes = minutes + r - current_minute;
        current_minute = r;
        i++;
    }
    printf("%d", minutes);
    return 0;
}


#include <stdio.h>

int main()
{
    int n;
    scanf("%d", &n);
    int x;
    scanf("%d", &x);
    int current_minute = 1;
    int minutes = 0;
    int i = 0;
    while (i < n)
    {
        int l;
        int r;
        scanf("%d", &l);
        scanf("%d", &r);
        while (l - current_minute > x)
        {
            current_minute = current_minute + 3;
        }
        minutes = minutes + r - current_minute + 1;
        current_minute = r + 1;
        i++;
    }
    printf("%d", minutes);
    return 0;
}


#include <stdio.h>
int main()
{
    int ans=0, n, l[51], s, r[51], i;
    scanf("%d %d", &n, &s);
    for(i=0; i<n; i++)
        scanf("%d %d", &l[i], &r[i]);
    int k=1;
    for(i=0; i<n; i++)
    {
        while(k+s<=l[i])
        {
            k+=s;
            //printf("k=%d\n", k);
        }
        ans+=r[i]-k+1;
        //printf("intermediate ans=%d\n", ans);
    }
    printf("%d\n", ans);
    return 0;
}


#include <stdio.h>
int main()
{
    int ans=0, n, l[51], s, r[51], i;
    scanf("%d %d", &n, &s);
    for(i=0; i<n; i++)
        scanf("%d %d", &l[i], &r[i]);
    int k=1;
    for(i=0; i<n; i++)
    {
        while(k+s<=l[i])
        {
            k+=s;
            //printf("k=%d\n", k);
        }
        ans+=r[i]-k+1;
        //printf("intermediate ans=%d\n", ans);
        k=r[i]+1;
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
template<class T, class T1> void chkmax(T &x, const T1 &y) { if(x < y) x = y; }
template<class T, class T1> void chkmin(T &x, const T1 &y) { if(x > y) x = y; }
const int MAXN = (1 << 20);
const int inf = (int)1e9 + 42;

struct node
{
	int mn, lazy;

	node() {mn = inf; lazy = inf;}
	node(int val)
	{
		mn = val;
		lazy = inf;
	}
};

node temp, broken;

node merge(node l, node r)
{
	temp.mn = min(l.mn, r.mn);
	temp.lazy = inf;
	return temp;
}

struct segment_tree
{
	node tr[4 * MAXN];

	void push(int l, int r, int idx)
	{
		if(tr[idx].lazy != inf)
		{
			chkmin(tr[idx].mn, tr[idx].lazy);

			if(l != r)
			{
				chkmin(tr[2 * idx + 1].lazy, tr[idx].lazy);
				chkmin(tr[2 * idx + 2].lazy, tr[idx].lazy);
			}

			tr[idx].lazy = inf;
		}
	}

	void init(int l, int r, int idx)
	{
		if(l == r)
		{
			tr[idx] = node(inf);
			return;
		}

		int mid = (l + r) >> 1;
		init(l, mid, 2 * idx + 1);
		init(mid + 1, r, 2 * idx + 2);

		tr[idx] = merge(tr[2 * idx + 1], tr[2 * idx + 2]);
	}

	void update(int qL, int qR, int val, int l, int r, int idx)
	{
		push(l, r, idx);

		if(qL > r || l > qR)
			return;

		if(qL <= l && r <= qR)
		{
			tr[idx].lazy = val;
			push(l, r, idx);
			return;
		}

		int mid = (l + r) >> 1;
		update(qL, qR, val, l, mid, 2 * idx + 1);
		update(qL, qR, val, mid + 1, r, 2 * idx + 2);

		tr[idx] = merge(tr[2 * idx + 1], tr[2 * idx + 2]);
	}

	node query(int qL, int qR, int l, int r, int idx)
	{
		push(l, r, idx);

		if(l > qR || r < qL)
			return broken;

		if(qL <= l && r <= qR)
			return tr[idx];

		int mid = (l + r) >> 1;
		return merge(query(qL, qR, l, mid, 2 * idx + 1), query(qL, qR, mid + 1, r, 2 * idx + 2));
	}
};

struct dsu
{
    vector<int> par, sz;
    dsu() { par.clear(), sz.clear(); }

    void init(int n)
    {
        par.resize(n + 1);
        sz.resize(n + 1);
        for(int i = 0; i <= n; i++)
            par[i] = i, sz[i] = 1;
    }

    int root(int x) { return x == par[x] ? x : (par[x] = root(par[x])); }

    bool connected(int u, int v) { return root(u) == root(v); }

    void unite(int u, int v)
    {
        u = root(u), v = root(v);
        if(u == v) return;
        if(sz[u] < sz[v]) swap(u, v);
        par[v] = u, sz[u] += sz[v];
    }
};

int n, m;
pair<int, pair<int, int> > e[MAXN], initial_e[MAXN];

void read()
{
    cin >> n >> m;
    for(int i = 0; i < m; i++)
    {
        cin >> e[i].second.first >> e[i].second.second >> e[i].first;
        initial_e[i] = e[i];
    }
}

dsu d;
vector<pair<int, int> > adj[MAXN];
int tr_sz[MAXN];

void pre_dfs(int u, int pr)
{
    tr_sz[u] = 1;
    for(pair<int, int> q: adj[u])
    {
        int v = q.first, w = q.second;
        if(v == pr) continue;
        pre_dfs(v, u);
        tr_sz[u] += tr_sz[v];
    }
}

int head[MAXN];
int dfs_time = 0, st[MAXN], en[MAXN];
int par[MAXN][20];

void decompose(int u, int pr, int head)
{
    ::head[u] = head;
    st[u] = ++dfs_time;
    par[u][0] = pr;
    for(int l = 1; l < 20; l++)
        par[u][l] = par[par[u][l - 1]][l - 1];

    pair<int, int> B = {-1, -1};
    for(pair<int, int> q: adj[u])
    {
        int v = q.first;
        if(v == pr) continue;
        chkmax(B, make_pair(tr_sz[v], v));
    }

    if(B.second != -1)
        decompose(B.second, u, head);

    for(pair<int, int> q: adj[u])
    {
        int v = q.first;
        if(v == pr || v == B.second) continue;
        decompose(v, u, v);
    }
}

inline bool upper(int x, int y) { return st[x] <= st[y] && en[y] <= en[x]; }

int lca(int u, int v)
{
    if(upper(u, v)) return u;
    if(upper(v, u)) return v;

    for(int l = 19; l >= 0; l--)
        if(!upper(par[u][l], v))
            u = par[u][l];

    return par[u][0];
}

vector<pair<int, int> > get_path_up(int u, int anc)
{
    vector<pair<int, int> > ret;
    while(st[anc] < st[u])
    {
        ret.push_back({max(st[anc] + 1, st[head[u]]), st[u]});
        u = par[head[u]][0];
    }

    return ret;
}

vector<pair<int, int> > get_path(int u, int v)
{
    int l = lca(u, v);
    vector<pair<int, int> > f = get_path_up(u, l);
    vector<pair<int, int> > s = get_path_up(v, l);
    while(!s.empty()) f.push_back(s.back()), s.pop_back();
    return f;
}

bool is_edge_in_mst(int u, int v)
{
    if(par[u][0] == v) return true;
    if(par[v][0] == u) return true;
    return false;
}

vector<int> add[MAXN], rem[MAXN];

segment_tree t1, t2;

void solve()
{
    d.init(n);

    sort(e, e + m);
    for(int i = 0; i < m; i++)
    {
        int u = e[i].second.first, v = e[i].second.second, w = e[i].first;
        if(!d.connected(u, v))
        {
            d.unite(u, v);
            adj[u].push_back({v, w});
            adj[v].push_back({u, w});
        }
    }

    pre_dfs(1, 1);
    decompose(1, 1, 1);
    t1.init(1, dfs_time, 0);
    t2.init(1, dfs_time, 0);

    for(int i = 0; i < m; i++)
    {
        int u = e[i].second.first, v = e[i].second.second, w = e[i].first;
        if(is_edge_in_mst(u, v))
        {
            if(par[u][0] != v) swap(u, v);
            t2.update(st[u], st[u], w, 1, dfs_time, 0);
        }
        else
        {
            vector<pair<int, int> > E = get_path(u, v);
            for(pair<int, int> it: E)
                t1.update(it.first, it.second, w, 1, dfs_time, 0);
        }
    }

    for(int i = 0; i < m; i++)
    {
        int u = initial_e[i].second.first, v = initial_e[i].second.second;
        if(is_edge_in_mst(u, v))
        {
            if(par[u][0] != v) swap(u, v);
            int ans = t1.query(st[u], st[u], 1, dfs_time, 0).mn;
            if(ans == inf) ans = -1;
            else ans = ans - 1;
            cout << ans << " ";
        }
        else
        {
            int ans = inf;
            vector<pair<int, int> > E = get_path(u, v);
            for(pair<int, int> it: E)
                chkmin(ans, t2.query(it.first, it.second, 1, dfs_time, 0).mn);

            if(ans == inf) ans = -1;
            else ans = ans - 1;
            cout << ans << " ";
        }
    }

    cout << endl;
}

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

    read();
    solve();
    return 0;
}


#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
template<class T, class T1> void chkmax(T &x, const T1 &y) { if(x < y) x = y; }
template<class T, class T1> void chkmin(T &x, const T1 &y) { if(x > y) x = y; }
const int MAXN = (1 << 20);
const int inf = (int)1e9 + 42;

struct node
{
	int mn, lazy;

	node() {mn = inf; lazy = inf;}
	node(int val)
	{
		mn = val;
		lazy = inf;
	}
};

node temp, broken;

node merge(node l, node r)
{
	temp.mn = min(l.mn, r.mn);
	temp.lazy = inf;
	return temp;
}

struct segment_tree
{
	node tr[4 * MAXN];

	void push(int l, int r, int idx)
	{
		if(tr[idx].lazy != inf)
		{
			chkmin(tr[idx].mn, tr[idx].lazy);

			if(l != r)
			{
				chkmin(tr[2 * idx + 1].lazy, tr[idx].lazy);
				chkmin(tr[2 * idx + 2].lazy, tr[idx].lazy);
			}

			tr[idx].lazy = inf;
		}
	}

	void init(int l, int r, int idx)
	{
		if(l == r)
		{
			tr[idx] = node(inf);
			return;
		}

		int mid = (l + r) >> 1;
		init(l, mid, 2 * idx + 1);
		init(mid + 1, r, 2 * idx + 2);

		tr[idx] = merge(tr[2 * idx + 1], tr[2 * idx + 2]);
	}

	void update(int qL, int qR, int val, int l, int r, int idx)
	{
		push(l, r, idx);

		if(qL > r || l > qR)
			return;

		if(qL <= l && r <= qR)
		{
			tr[idx].lazy = val;
			push(l, r, idx);
			return;
		}

		int mid = (l + r) >> 1;
		update(qL, qR, val, l, mid, 2 * idx + 1);
		update(qL, qR, val, mid + 1, r, 2 * idx + 2);

		tr[idx] = merge(tr[2 * idx + 1], tr[2 * idx + 2]);
	}

	node query(int qL, int qR, int l, int r, int idx)
	{
		push(l, r, idx);

		if(l > qR || r < qL)
			return broken;

		if(qL <= l && r <= qR)
			return tr[idx];

		int mid = (l + r) >> 1;
		return merge(query(qL, qR, l, mid, 2 * idx + 1), query(qL, qR, mid + 1, r, 2 * idx + 2));
	}
};

struct dsu
{
    vector<int> par, sz;
    dsu() { par.clear(), sz.clear(); }

    void init(int n)
    {
        par.resize(n + 1);
        sz.resize(n + 1);
        for(int i = 0; i <= n; i++)
            par[i] = i, sz[i] = 1;
    }

    int root(int x) { return x == par[x] ? x : (par[x] = root(par[x])); }

    bool connected(int u, int v) { return root(u) == root(v); }

    void unite(int u, int v)
    {
        u = root(u), v = root(v);
        if(u == v) return;
        if(sz[u] < sz[v]) swap(u, v);
        par[v] = u, sz[u] += sz[v];
    }
};

int n, m;
pair<int, pair<int, int> > e[MAXN], initial_e[MAXN];

void read()
{
    cin >> n >> m;
    for(int i = 0; i < m; i++)
    {
        cin >> e[i].second.first >> e[i].second.second >> e[i].first;
        initial_e[i] = e[i];
    }
}

dsu d;
vector<pair<int, int> > adj[MAXN];
int tr_sz[MAXN];

void pre_dfs(int u, int pr)
{
    tr_sz[u] = 1;
    for(pair<int, int> q: adj[u])
    {
        int v = q.first, w = q.second;
        if(v == pr) continue;
        pre_dfs(v, u);
        tr_sz[u] += tr_sz[v];
    }
}

int head[MAXN];
int dfs_time = 0, st[MAXN], en[MAXN];
int par[MAXN][20];

void decompose(int u, int pr, int head)
{
    ::head[u] = head;
    st[u] = ++dfs_time;
    par[u][0] = pr;
    for(int l = 1; l < 20; l++)
        par[u][l] = par[par[u][l - 1]][l - 1];

    pair<int, int> B = {-1, -1};
    for(pair<int, int> q: adj[u])
    {
        int v = q.first;
        if(v == pr) continue;
        chkmax(B, make_pair(tr_sz[v], v));
    }

    if(B.second != -1)
        decompose(B.second, u, head);

    for(pair<int, int> q: adj[u])
    {
        int v = q.first;
        if(v == pr || v == B.second) continue;
        decompose(v, u, v);
    }
}

inline bool upper(int x, int y) { return st[x] <= st[y] && en[y] <= en[x]; }

int lca(int u, int v)
{
    if(upper(u, v)) return u;
    if(upper(v, u)) return v;

    for(int l = 19; l >= 0; l--)
        if(!upper(par[u][l], v))
            u = par[u][l];

    return par[u][0];
}

vector<pair<int, int> > get_path_up(int u, int anc)
{
    vector<pair<int, int> > ret;
    while(st[anc] < st[u])
    {
        ret.push_back({max(st[anc] + 1, st[head[u]]), st[u]});
        u = par[head[u]][0];
    }

    return ret;
}

vector<pair<int, int> > get_path(int u, int v)
{
    int l = lca(u, v);
    vector<pair<int, int> > f = get_path_up(u, l);
    vector<pair<int, int> > s = get_path_up(v, l);
    while(!s.empty()) f.push_back(s.back()), s.pop_back();
    return f;
}

bool is_edge_in_mst(int u, int v)
{
    if(par[u][0] == v) return true;
    if(par[v][0] == u) return true;
    return false;
}

vector<int> add[MAXN], rem[MAXN];

segment_tree t1, t2;

void solve()
{
    d.init(n);

    sort(e, e + m);
    for(int i = 0; i < m; i++)
    {
        int u = e[i].second.first, v = e[i].second.second, w = e[i].first;
        if(!d.connected(u, v))
        {
            d.unite(u, v);
            adj[u].push_back({v, w});
            adj[v].push_back({u, w});
        }
    }

    pre_dfs(1, 1);
    decompose(1, 1, 1);
    t1.init(1, dfs_time, 0);
    t2.init(1, dfs_time, 0);

    for(int i = 0; i < m; i++)
    {
        int u = e[i].second.first, v = e[i].second.second, w = e[i].first;
        if(is_edge_in_mst(u, v))
        {
            if(par[u][0] != v) swap(u, v);
            t2.update(st[u], st[u], -w, 1, dfs_time, 0);
        }
        else
        {
            vector<pair<int, int> > E = get_path(u, v);
            for(pair<int, int> it: E)
                t1.update(it.first, it.second, w, 1, dfs_time, 0);
        }
    }

    for(int i = 0; i < m; i++)
    {
        int u = initial_e[i].second.first, v = initial_e[i].second.second;
        if(is_edge_in_mst(u, v))
        {
            if(par[u][0] != v) swap(u, v);
            int ans = t1.query(st[u], st[u], 1, dfs_time, 0).mn;
            if(ans == inf) ans = -1;
            else ans = ans - 1;
            cout << ans << " ";
        }
        else
        {
            int ans = inf;
            vector<pair<int, int> > E = get_path(u, v);
            for(pair<int, int> it: E)
                chkmin(ans, t2.query(it.first, it.second, 1, dfs_time, 0).mn);

            if(ans == inf) ans = -1;
            else ans = -ans - 1;
            cout << ans << " ";
        }
    }

    cout << endl;
}

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

    read();
    solve();
    return 0;
}


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

const int MAXN = 222222;
const int MAXM = MAXN << 1;
const int INF = 2e9;
int n, m, cnt, DFN, size[MAXN], dep[MAXN], fa[MAXN], top[MAXN], which[MAXN], adj[MAXN];
int ans[MAXM], dfn[MAXN], son[MAXN], fa_e[MAXN];

struct OriEdge{
	int u, v, w, id;
	bool used;
} edge[MAXM];

inline bool cmp_edge(const OriEdge &p, const OriEdge &q) {
	return p.w < q.w;
}

inline int get_fa(int u) {
	return fa[u] == u ? u : fa[u] = get_fa(fa[u]);
}

struct Edge {
	int v, nxt, id;
} e[MAXM];

inline void addedge(int u, int v, int id) {
	e[++cnt].v = v;
	e[cnt].id = id;
	e[cnt].nxt = adj[u];
	adj[u] = cnt;
}

inline void dfs1(int u, int par) {
	size[u] = 1;
	fa[u] = par;
	dep[u] = dep[par] + 1;
	int maxsize = -1;
	for (int p = adj[u]; p; p = e[p].nxt) {
		int v = e[p].v;
		if (v == par) {
			continue;
		}
		fa_e[v] = e[p].id;
		dfs1(v, u);
		if (size[v] > maxsize) {
			maxsize = size[v];
			son[u] = v;
		}
	}
}

inline void dfs2(int u, int par) {
	dfn[u] = ++DFN;
	which[DFN] = u;
//cout << u << " " << fa[u] << " " << size[u] << " " << dfn[u] << " " << son[u] << endl;
	if (!son[u]) {
		return;
	}
	top[son[u]] = top[u];
	dfs2(son[u], u);
	for (int p = adj[u]; p; p = e[p].nxt) {
	//cout << u << " " << par << " " << p << " " << e[p].v << endl;
		int v = e[p].v;
		if (v == par || v == son[u]) {
			continue;
		}
		top[v] = v;
		dfs2(v, u);
	}
}

struct SegmentTree {
	int cov, minw;
} tr[MAXN << 2];

inline int get_node(int l, int r) {
	return l + r | l != r;
}

inline void build(int l, int r) {
	int k = get_node(l, r);
	tr[k].cov = INF;
	if (l == r) {
		tr[k].minw = edge[fa_e[which[l]]].w;
		return;
	}
	int mid = l + r >> 1;
	build(l, mid);
	build(mid + 1, r);
	tr[k].minw = max(tr[get_node(l, mid)].minw, tr[get_node(mid + 1, r)].minw);
}

inline int query_minw(int l, int r, int a, int b, int x) {
	int k = get_node(l, r);
	if (l == a && r == b) {
		tr[k].cov = min(tr[k].cov, x);
		return tr[k].minw;
	}
	int mid = l + r >> 1;
	if (a > mid) {
		return query_minw(mid + 1, r, a, b, x);
	}
	if (b <= mid) {
		return query_minw(l, mid, a, b, x);
	}
	return max(query_minw(l, mid, a, mid, x),
			query_minw(mid + 1, r, mid + 1, b, x));
}

inline int query_cov(int l, int r, int x) {
	int k = get_node(l, r);
	if (l == r) {
		return tr[k].cov;
	}
	int mid = l + r >> 1;
	int ret = tr[k].cov;
	if (x > mid) {
		ret = min(ret, query_cov(mid + 1, r, x));
	} else {
		ret = min(ret, query_cov(l, mid, x));
	}
	return ret;
}

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

	cin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		cin >> edge[i].u >> edge[i].v >> edge[i].w;
		edge[i].id = i;
		edge[i].used = false;
	}

	sort(edge + 1, edge + m + 1, cmp_edge);
	for (int i = 1; i <= n; ++i) {
		fa[i] = i;
	}
	for (int i = 1; i <= m; ++i) {
		int u = edge[i].u;
		int v = edge[i].v;
		int uu = get_fa(u);
		int vv = get_fa(v);
		if (uu == vv) {
			continue;
		}
		edge[i].used = true;
		fa[uu] = vv;
		addedge(u, v, i);
		addedge(v, u, i);
		//cout << "link " << u << "-----" << v << endl;
	}

	memset(fa, 0, sizeof(fa));
	dfs1(1, 0);
	top[1] = 1;
	dfs2(1, 0);
	build(1, n);
	for (int i = 1; i <= m; ++i) {
		if (edge[i].used) {
			continue;
		}
		int u = edge[i].u;
		int v = edge[i].v;
		int nowans = INF;
		while (top[u] != top[v]) {
			if (dep[u] < dep[v]) {
				swap(u, v);
			}
//cout << "modify " << dfn[top[u]] << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = min(nowans, query_minw(1, n, dfn[top[u]], dfn[u], edge[i].w - 1));
			u = fa[top[u]];
		}
		if (u != v) {
			if (dep[u] < dep[v]) {
				swap(u, v);
			}
//cout << "modify " << dfn[v] + 1 << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = query_minw(1, n, dfn[v] + 1, dfn[u], edge[i].w - 1);
		}
		ans[edge[i].id] = nowans - 1;
	}
	for (int i = 1; i <= m; ++i) {
		if (!edge[i].used) {
			continue;
		}
		int u = edge[i].u,
			v = edge[i].v;
		if (dep[u] < dep[v]) {
			swap(u, v);
		}
		ans[edge[i].id] = query_cov(1, n, dfn[u]);
		if (ans[edge[i].id] == INF) {
			ans[edge[i].id] = -1;
		}
	}

	//cout << query_cov(1, n, 4) << endl; return 0;
	for (int i = 1; i <= m; ++i) {
		cout << ans[i];
		if (i < m) {
			cout << " ";
		}
	}
	cout << endl;
}


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

const int MAXN = 422222;
const int MAXM = MAXN << 1;
const int INF = 2e9;
int n, m, cnt, DFN, size[MAXN], dep[MAXN], fa[MAXN], top[MAXN], which[MAXN], adj[MAXN];
int ans[MAXM], dfn[MAXN], son[MAXN], fa_e[MAXN];

struct OriEdge{
	int u, v, w, id;
	bool used;
} edge[MAXM];

inline bool cmp_edge(const OriEdge &p, const OriEdge &q) {
	return p.w < q.w;
}

inline int get_fa(int u) {
	return fa[u] == u ? u : fa[u] = get_fa(fa[u]);
}

struct Edge {
	int v, nxt, id;
} e[MAXM];

inline void addedge(int u, int v, int id) {
	e[++cnt].v = v;
	e[cnt].id = id;
	e[cnt].nxt = adj[u];
	adj[u] = cnt;
}

inline void dfs1(int u, int par) {
	size[u] = 1;
	fa[u] = par;
	dep[u] = dep[par] + 1;
	int maxsize = -1;
	for (int p = adj[u]; p; p = e[p].nxt) {
		int v = e[p].v;
		if (v == par) {
			continue;
		}
		fa_e[v] = e[p].id;
		dfs1(v, u);
		size[u] += size[v];
		if (size[v] > maxsize) {
			maxsize = size[v];
			son[u] = v;
		}
	}
}

inline void dfs2(int u, int par) {
	dfn[u] = ++DFN;
	which[DFN] = u;
//cout << u << " " << fa[u] << " " << size[u] << " " << dfn[u] << " " << son[u] << endl;
	if (!son[u]) {
		return;
	}
	top[son[u]] = top[u];
	dfs2(son[u], u);
	for (int p = adj[u]; p; p = e[p].nxt) {
	//cout << u << " " << par << " " << p << " " << e[p].v << endl;
		int v = e[p].v;
		if (v == par || v == son[u]) {
			continue;
		}
		top[v] = v;
		dfs2(v, u);
	}
}

struct SegmentTree {
	int cov, minw;
} tr[MAXN << 2];

inline int get_node(int l, int r) {
	return l + r | l != r;
}

inline void build(int l, int r) {
	int k = get_node(l, r);
	tr[k].cov = INF;
	if (l == r) {
		tr[k].minw = edge[fa_e[which[l]]].w;
		return;
	}
	int mid = l + r >> 1;
	build(l, mid);
	build(mid + 1, r);
	tr[k].minw = max(tr[get_node(l, mid)].minw, tr[get_node(mid + 1, r)].minw);
}

inline int query_minw(int l, int r, int a, int b, int x) {
	int k = get_node(l, r);
	if (l == a && r == b) {
		tr[k].cov = min(tr[k].cov, x);
		return tr[k].minw;
	}
	int mid = l + r >> 1;
	if (a > mid) {
		return query_minw(mid + 1, r, a, b, x);
	}
	if (b <= mid) {
		return query_minw(l, mid, a, b, x);
	}
	return max(query_minw(l, mid, a, mid, x),
			query_minw(mid + 1, r, mid + 1, b, x));
}

inline int query_cov(int l, int r, int x) {
	int k = get_node(l, r);
	if (l == r) {
		return tr[k].cov;
	}
	int mid = l + r >> 1;
	int ret = tr[k].cov;
	if (x > mid) {
		ret = min(ret, query_cov(mid + 1, r, x));
	} else {
		ret = min(ret, query_cov(l, mid, x));
	}
	return ret;
}

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

	cin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		cin >> edge[i].u >> edge[i].v >> edge[i].w;
		edge[i].id = i;
		edge[i].used = false;
	}

	sort(edge + 1, edge + m + 1, cmp_edge);
	for (int i = 1; i <= n; ++i) {
		fa[i] = i;
	}
	for (int i = 1; i <= m; ++i) {
		int u = edge[i].u;
		int v = edge[i].v;
		int uu = get_fa(u);
		int vv = get_fa(v);
		if (uu == vv) {
			continue;
		}
		edge[i].used = true;
		fa[uu] = vv;
		addedge(u, v, i);
		addedge(v, u, i);
		//cout << "link " << u << "-----" << v << endl;
	}

	memset(fa, 0, sizeof(fa));
	dfs1(1, 0);
	top[1] = 1;
	dfs2(1, 0);
	build(1, n);
	for (int i = 1; i <= m; ++i) {
		if (edge[i].used) {
			continue;
		}
		int u = edge[i].u;
		int v = edge[i].v;
		int nowans = INF;
		while (top[u] != top[v]) {
			if (dep[u] < dep[v]) {
				swap(u, v);
			}
//cout << "modify " << dfn[top[u]] << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = min(nowans, query_minw(1, n, dfn[top[u]], dfn[u], edge[i].w - 1));
			u = fa[top[u]];
		}
		if (u != v) {
			if (dep[u] < dep[v]) {
				swap(u, v);
			}
//cout << "modify " << dfn[v] + 1 << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = min(nowans, query_minw(1, n, dfn[v] + 1, dfn[u], edge[i].w - 1));
		}
		ans[edge[i].id] = nowans - 1;
	}
	for (int i = 1; i <= m; ++i) {
		if (!edge[i].used) {
			continue;
		}
		int u = edge[i].u,
			v = edge[i].v;
		if (dep[u] < dep[v]) {
			swap(u, v);
		}
		ans[edge[i].id] = query_cov(1, n, dfn[u]);
		if (ans[edge[i].id] == INF) {
			ans[edge[i].id] = -1;
		}
	}

	//cout << query_cov(1, n, 4) << endl; return 0;
	for (int i = 1; i <= m; ++i) {
		cout << ans[i];
		if (i < m) {
			cout << " ";
		}
	}
	cout << endl;
}


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

const int MAXN = 422222;
const int MAXM = MAXN << 1;
const int INF = 2e9;
int n, m, cnt, DFN, size[MAXN], dep[MAXN], fa[MAXN], top[MAXN], which[MAXN], adj[MAXN];
int ans[MAXM], dfn[MAXN], son[MAXN], fa_e[MAXN];

struct OriEdge{
	int u, v, w, id;
	bool used;
} edge[MAXM];

inline bool cmp_edge(const OriEdge &p, const OriEdge &q) {
	return p.w < q.w;
}

inline int get_fa(int u) {
	return fa[u] == u ? u : fa[u] = get_fa(fa[u]);
}

struct Edge {
	int v, nxt, id;
} e[MAXM];

inline void addedge(int u, int v, int id) {
	e[++cnt].v = v;
	e[cnt].id = id;
	e[cnt].nxt = adj[u];
	adj[u] = cnt;
}

inline void dfs1(int u, int par) {
	size[u] = 1;
	fa[u] = par;
	dep[u] = dep[par] + 1;
	int maxsize = -1;
	for (int p = adj[u]; p; p = e[p].nxt) {
		int v = e[p].v;
		if (v == par) {
			continue;
		}
		fa_e[v] = e[p].id;
		dfs1(v, u);
		size[u] += size[v];
		if (size[v] > maxsize) {
			maxsize = size[v];
			son[u] = v;
		}
	}
}

inline void dfs2(int u, int par) {
	dfn[u] = ++DFN;
	which[DFN] = u;
//cout << u << " " << fa[u] << " " << size[u] << " " << dfn[u] << " " << son[u] << endl;
	if (!son[u]) {
assert(size[u] == 1);
		return;
	}
assert(son[u]);
	top[son[u]] = top[u];
	dfs2(son[u], u);
	for (int p = adj[u]; p; p = e[p].nxt) {
	//cout << u << " " << par << " " << p << " " << e[p].v << endl;
		int v = e[p].v;
		if (v == par || v == son[u]) {
			continue;
		}
		top[v] = v;
		dfs2(v, u);
	}
}

struct SegmentTree {
	int cov, minw;
} tr[MAXN << 2];

inline int get_node(int l, int r) {
	return l + r | l != r;
}

inline void build(int l, int r) {
	int k = get_node(l, r);
	tr[k].cov = INF;
	if (l == r) {
		tr[k].minw = edge[fa_e[which[l]]].w;
		return;
	}
	int mid = l + r >> 1;
	build(l, mid);
	build(mid + 1, r);
	tr[k].minw = max(tr[get_node(l, mid)].minw, tr[get_node(mid + 1, r)].minw);
}

inline int query_minw(int l, int r, int a, int b, int x) {
	int k = get_node(l, r);
	if (l == a && r == b) {
		tr[k].cov = min(tr[k].cov, x);
		return tr[k].minw;
	}
	int mid = l + r >> 1;
	if (a > mid) {
		return query_minw(mid + 1, r, a, b, x);
	}
	if (b <= mid) {
		return query_minw(l, mid, a, b, x);
	}
	return max(query_minw(l, mid, a, mid, x),
			query_minw(mid + 1, r, mid + 1, b, x));
}

inline int query_cov(int l, int r, int x) {
	int k = get_node(l, r);
	if (l == r) {
		return tr[k].cov;
	}
	int mid = l + r >> 1;
	int ret = tr[k].cov;
	if (x > mid) {
		ret = min(ret, query_cov(mid + 1, r, x));
	} else {
		ret = min(ret, query_cov(l, mid, x));
	}
	return ret;
}

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

	cin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		cin >> edge[i].u >> edge[i].v >> edge[i].w;
		edge[i].id = i;
		edge[i].used = false;
	}

	sort(edge + 1, edge + m + 1, cmp_edge);
	for (int i = 1; i <= n; ++i) {
		fa[i] = i;
	}
	int ccc = 0;
	for (int i = 1; i <= m; ++i) {
		int u = edge[i].u;
		int v = edge[i].v;
		int uu = get_fa(u);
		int vv = get_fa(v);
		if (uu == vv) {
			continue;
		}
		edge[i].used = true;
		fa[uu] = vv;
		++ccc;
		addedge(u, v, i);
		addedge(v, u, i);
		//cout << "link " << u << "-----" << v << endl;
	}
	assert(ccc == n - 1);

	memset(fa, 0, sizeof(fa));
	dfs1(1, 0);
	top[1] = 1;
	DFN = 0;
	dfs2(1, 0);
	assert(DFN == n);
	build(1, n);
	for (int i = 1; i <= m; ++i) {
		if (edge[i].used) {
			continue;
		}
		int u = edge[i].u;
		int v = edge[i].v;
		int nowans = INF;
		while (top[u] != top[v]) {
			if (dep[top[u]] < dep[top[v]]) {
				swap(u, v);
			}
//cout << "modify " << dfn[top[u]] << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = min(nowans, query_minw(1, n, dfn[top[u]], dfn[u], edge[i].w - 1));
			u = fa[top[u]];
		}
		if (u != v) {
			if (dep[u] < dep[v]) {
				swap(u, v);
			}
//cout << "modify " << dfn[v] + 1 << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = min(nowans, query_minw(1, n, dfn[v] + 1, dfn[u], edge[i].w - 1));
		}
		ans[edge[i].id] = nowans - 1;
	}
	for (int i = 1; i <= m; ++i) {
		if (!edge[i].used) {
			continue;
		}
		int u = edge[i].u,
			v = edge[i].v;
		if (dep[u] < dep[v]) {
			swap(u, v);
		}
		ans[edge[i].id] = query_cov(1, n, dfn[u]);
		if (ans[edge[i].id] == INF) {
			ans[edge[i].id] = -1;
		}
	}

	//cout << query_cov(1, n, 4) << endl; return 0;
	for (int i = 1; i <= m; ++i) {
		cout << ans[i];
		if (i < m) {
			cout << " ";
		}
	}
	cout << endl;
}


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

const int MAXN = 222222;
const int MAXM = MAXN << 1;
const int INF = 2e9;
int n, m, cnt, DFN, size[MAXN], dep[MAXN], fa[MAXN], top[MAXN], which[MAXN], adj[MAXN];
int ans[MAXM], dfn[MAXN], son[MAXN], fa_e[MAXN];

struct OriEdge{
	int u, v, w, id;
	bool used;
} edge[MAXM];

inline bool cmp_edge(const OriEdge &p, const OriEdge &q) {
	return p.w < q.w;
}

inline int get_fa(int u) {
	return fa[u] == u ? u : fa[u] = get_fa(fa[u]);
}

struct Edge {
	int v, nxt, id;
} e[MAXM];

inline void addedge(int u, int v, int id) {
	e[++cnt].v = v;
	e[cnt].id = id;
	e[cnt].nxt = adj[u];
	adj[u] = cnt;
}

inline void dfs1(int u, int par) {
	size[u] = 1;
	fa[u] = par;
	dep[u] = dep[par] + 1;
	int maxsize = -1;
	for (int p = adj[u]; p; p = e[p].nxt) {
		int v = e[p].v;
		if (v == par) {
			continue;
		}
		fa_e[v] = e[p].id;
		dfs1(v, u);
		if (size[v] > maxsize) {
			maxsize = size[v];
			son[u] = v;
		}
	}
}

inline void dfs2(int u, int par) {
	dfn[u] = ++DFN;
	which[DFN] = u;
	if (!son[u]) {
		return;
	}
//cout << u << " " << fa[u] << " " << size[u] << " " << dfn[u] << " " << son[u] << endl;
	top[son[u]] = top[u];
	dfs2(son[u], u);
	for (int p = adj[u]; p; p = e[p].nxt) {
	//cout << u << " " << par << " " << p << " " << e[p].v << endl;
		int v = e[p].v;
		if (v == par || v == son[u]) {
			continue;
		}
		top[v] = v;
		dfs2(v, u);
	}
}

struct SegmentTree {
	int cov, minw;
} tr[MAXN << 2];

inline int get_node(int l, int r) {
	return l + r | l != r;
}

inline void build(int l, int r) {
	int k = get_node(l, r);
	tr[k].cov = INF;
	if (l == r) {
		tr[k].minw = edge[fa_e[which[l]]].w;
		return;
	}
	int mid = l + r >> 1;
	build(l, mid);
	build(mid + 1, r);
	tr[k].minw = max(tr[get_node(l, mid)].minw, tr[get_node(mid + 1, r)].minw);
}

inline int query_minw(int l, int r, int a, int b, int x) {
	int k = get_node(l, r);
	if (l == a && r == b) {
		tr[k].cov = x;
		return tr[k].minw;
	}
	int mid = l + r >> 1;
	if (a > mid) {
		return query_minw(mid + 1, r, a, b, x);
	}
	if (b <= mid) {
		return query_minw(l, mid, a, b, x);
	}
	return max(query_minw(l, mid, a, mid, x),
			query_minw(mid + 1, r, mid + 1, b, x));
}

inline int query_cov(int l, int r, int x) {
	int k = get_node(l, r);
	if (l == r) {
		return tr[k].cov;
	}
	int mid = l + r >> 1;
	int ret = tr[k].cov;
	if (x > mid) {
		ret = min(ret, query_cov(mid + 1, r, x));
	} else {
		ret = min(ret, query_cov(l, mid, x));
	}
	return ret;
}

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

	cin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		cin >> edge[i].u >> edge[i].v >> edge[i].w;
		edge[i].id = i;
		edge[i].used = false;
	}

	sort(edge + 1, edge + m + 1, cmp_edge);
	for (int i = 1; i <= n; ++i) {
		fa[i] = i;
	}
	for (int i = 1; i <= m; ++i) {
		int u = edge[i].u;
		int v = edge[i].v;
		int uu = get_fa(u);
		int vv = get_fa(v);
		if (uu == vv) {
			continue;
		}
		edge[i].used = true;
		fa[uu] = vv;
		addedge(u, v, i);
		addedge(v, u, i);
		//cout << "link " << u << "-----" << v << endl;
	}

	memset(fa, 0, sizeof(fa));
	dfs1(1, 0);
	top[1] = 1;
	dfs2(1, 0);
	build(1, n);
	for (int i = 1; i <= m; ++i) {
		if (edge[i].used) {
			continue;
		}
		int u = edge[i].u;
		int v = edge[i].v;
		int nowans = INF;
		while (top[u] != top[v]) {
			if (dep[u] < dep[v]) {
				swap(u, v);
			}
//cout << "modify " << dfn[top[u]] << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = min(nowans, query_minw(1, n, dfn[top[u]], dfn[u], edge[i].w - 1));
			u = fa[top[u]];
		}
		if (u != v) {
			if (dep[u] < dep[v]) {
				swap(u, v);
			}
//cout << "modify " << dfn[v] + 1 << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = query_minw(1, n, dfn[v] + 1, dfn[u], edge[i].w - 1);
		}
		ans[edge[i].id] = nowans - 1;
	}
	for (int i = 1; i <= m; ++i) {
		if (!edge[i].used) {
			continue;
		}
		int u = edge[i].u,
			v = edge[i].v;
		if (dep[u] < dep[v]) {
			swap(u, v);
		}
		ans[edge[i].id] = query_cov(1, n, dfn[u]);
		if (ans[edge[i].id] >= edge[i].w) {
			ans[edge[i].id] = -1;
		}
	}

	for (int i = 1; i <= m; ++i) {
		cout << ans[i];
		if (i < m) {
			cout << " ";
		}
	}
	cout << endl;
}


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

const int MAXN = 422222;
const int MAXM = MAXN << 1;
const int INF = 2e9;
int n, m, cnt, DFN, size[MAXN], dep[MAXN], fa[MAXN], top[MAXN], which[MAXN], adj[MAXN];
int ans[MAXM], dfn[MAXN], son[MAXN], fa_e[MAXN];

struct OriEdge{
	int u, v, w, id;
	bool used;
} edge[MAXM];

inline bool cmp_edge(const OriEdge &p, const OriEdge &q) {
	return p.w < q.w;
}

inline int get_fa(int u) {
	return fa[u] == u ? u : fa[u] = get_fa(fa[u]);
}

struct Edge {
	int v, nxt, id;
} e[MAXM];

inline void addedge(int u, int v, int id) {
	e[++cnt].v = v;
	e[cnt].id = id;
	e[cnt].nxt = adj[u];
	adj[u] = cnt;
}

inline void dfs1(int u, int par) {
	size[u] = 1;
	fa[u] = par;
	dep[u] = dep[par] + 1;
	int maxsize = -1;
	for (int p = adj[u]; p; p = e[p].nxt) {
		int v = e[p].v;
		if (v == par) {
			continue;
		}
		fa_e[v] = e[p].id;
		dfs1(v, u);
		size[u] += size[v];
		if (size[v] > maxsize) {
			maxsize = size[v];
			son[u] = v;
		}
	}
}

inline void dfs2(int u, int par) {
	dfn[u] = ++DFN;
	which[DFN] = u;
//cout << u << " " << fa[u] << " " << size[u] << " " << dfn[u] << " " << son[u] << endl;
	if (!son[u]) {
assert(size[u] == 1);
		return;
	}
assert(son[u]);
	top[son[u]] = top[u];
	dfs2(son[u], u);
	for (int p = adj[u]; p; p = e[p].nxt) {
	//cout << u << " " << par << " " << p << " " << e[p].v << endl;
		int v = e[p].v;
		if (v == par || v == son[u]) {
			continue;
		}
		top[v] = v;
		dfs2(v, u);
	}
}

struct SegmentTree {
	int cov, minw;
} tr[MAXN << 2];

inline int get_node(int l, int r) {
	return l + r | l != r;
}

inline void build(int l, int r) {
	int k = get_node(l, r);
	tr[k].cov = INF;
	if (l == r) {
		tr[k].minw = edge[fa_e[which[l]]].w;
		return;
	}
	int mid = l + r >> 1;
	build(l, mid);
	build(mid + 1, r);
	tr[k].minw = max(tr[get_node(l, mid)].minw, tr[get_node(mid + 1, r)].minw);
}

inline int query_minw(int l, int r, int a, int b, int x) {
	int k = get_node(l, r);
	if (l == a && r == b) {
		tr[k].cov = min(tr[k].cov, x);
		return tr[k].minw;
	}
	int mid = l + r >> 1;
	if (a > mid) {
		return query_minw(mid + 1, r, a, b, x);
	}
	if (b <= mid) {
		return query_minw(l, mid, a, b, x);
	}
	return max(query_minw(l, mid, a, mid, x),
			query_minw(mid + 1, r, mid + 1, b, x));
}

inline int query_cov(int l, int r, int x) {
	int k = get_node(l, r);
	if (l == r) {
		return tr[k].cov;
	}
	int mid = l + r >> 1;
	int ret = tr[k].cov;
	if (x > mid) {
		ret = min(ret, query_cov(mid + 1, r, x));
	} else {
		ret = min(ret, query_cov(l, mid, x));
	}
	return ret;
}

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

	cin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		cin >> edge[i].u >> edge[i].v >> edge[i].w;
		edge[i].id = i;
		edge[i].used = false;
	}

	sort(edge + 1, edge + m + 1, cmp_edge);
	for (int i = 1; i <= n; ++i) {
		fa[i] = i;
	}
	int ccc = 0;
	for (int i = 1; i <= m; ++i) {
		int u = edge[i].u;
		int v = edge[i].v;
		int uu = get_fa(u);
		int vv = get_fa(v);
		if (uu == vv) {
			continue;
		}
		edge[i].used = true;
		fa[uu] = vv;
		++ccc;
		addedge(u, v, i);
		addedge(v, u, i);
		//cout << "link " << u << "-----" << v << endl;
	}
	assert(ccc == n - 1);

	memset(fa, 0, sizeof(fa));
	dfs1(1, 0);
	top[1] = 1;
	DFN = 0;
	dfs2(1, 0);
	assert(DFN == n);
	build(1, n);
	for (int i = 1; i <= m; ++i) {
		if (edge[i].used) {
			continue;
		}
		int u = edge[i].u;
		int v = edge[i].v;
		int nowans = -1;
		while (top[u] != top[v]) {
			if (dep[top[u]] < dep[top[v]]) {
				swap(u, v);
			}
//cout << "modify " << dfn[top[u]] << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = max(nowans, query_minw(1, n, dfn[top[u]], dfn[u], edge[i].w - 1));
			u = fa[top[u]];
		}
		if (u != v) {
			if (dep[u] < dep[v]) {
				swap(u, v);
			}
//cout << "modify " << dfn[v] + 1 << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = max(nowans, query_minw(1, n, dfn[v] + 1, dfn[u], edge[i].w - 1));
		}
		ans[edge[i].id] = nowans - 1;
	}
	for (int i = 1; i <= m; ++i) {
		if (!edge[i].used) {
			continue;
		}
		int u = edge[i].u,
			v = edge[i].v;
		if (dep[u] < dep[v]) {
			swap(u, v);
		}
		ans[edge[i].id] = query_cov(1, n, dfn[u]);
		if (ans[edge[i].id] == INF) {
			ans[edge[i].id] = -1;
		}
	}

	//cout << query_cov(1, n, 4) << endl; return 0;
	for (int i = 1; i <= m; ++i) {
		cout << ans[i];
		if (i < m) {
			cout << " ";
		}
	}
	cout << endl;
}


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

const int MAXN = 222222;
const int MAXM = MAXN << 1;
const int INF = 2e9;
int n, m, cnt, DFN, size[MAXN], dep[MAXN], fa[MAXN], top[MAXN], which[MAXN], adj[MAXN];
int ans[MAXM], dfn[MAXN], son[MAXN], fa_e[MAXN];

struct OriEdge{
	int u, v, w, id;
	bool used;
} edge[MAXM];

inline bool cmp_edge(const OriEdge &p, const OriEdge &q) {
	return p.w < q.w;
}

inline int get_fa(int u) {
	return fa[u] == u ? u : fa[u] = get_fa(fa[u]);
}

struct Edge {
	int v, nxt, id;
} e[MAXM];

inline void addedge(int u, int v, int id) {
	e[++cnt].v = v;
	e[cnt].id = id;
	e[cnt].nxt = adj[u];
	adj[u] = cnt;
}

inline void dfs1(int u, int par) {
	size[u] = 1;
	fa[u] = par;
	dep[u] = dep[par] + 1;
	int maxsize = -1;
	for (int p = adj[u]; p; p = e[p].nxt) {
		int v = e[p].v;
		if (v == par) {
			continue;
		}
		fa_e[v] = e[p].id;
		dfs1(v, u);
		if (size[v] > maxsize) {
			maxsize = size[v];
			son[u] = v;
		}
	}
}

inline void dfs2(int u, int par) {
	dfn[u] = ++DFN;
	which[DFN] = u;
//cout << u << " " << fa[u] << " " << size[u] << " " << dfn[u] << " " << son[u] << endl;
	if (!son[u]) {
		return;
	}
	top[son[u]] = top[u];
	dfs2(son[u], u);
	for (int p = adj[u]; p; p = e[p].nxt) {
	//cout << u << " " << par << " " << p << " " << e[p].v << endl;
		int v = e[p].v;
		if (v == par || v == son[u]) {
			continue;
		}
		top[v] = v;
		dfs2(v, u);
	}
}

struct SegmentTree {
	int cov, minw;
} tr[MAXN << 2];

inline int get_node(int l, int r) {
	return l + r | l != r;
}

inline void build(int l, int r) {
	int k = get_node(l, r);
	tr[k].cov = INF;
	if (l == r) {
		tr[k].minw = edge[fa_e[which[l]]].w;
		return;
	}
	int mid = l + r >> 1;
	build(l, mid);
	build(mid + 1, r);
	tr[k].minw = max(tr[get_node(l, mid)].minw, tr[get_node(mid + 1, r)].minw);
}

inline int query_minw(int l, int r, int a, int b, int x) {
	int k = get_node(l, r);
	if (l == a && r == b) {
		tr[k].cov = min(tr[k].cov, x);
		return tr[k].minw;
	}
	int mid = l + r >> 1;
	if (a > mid) {
		return query_minw(mid + 1, r, a, b, x);
	}
	if (b <= mid) {
		return query_minw(l, mid, a, b, x);
	}
	return max(query_minw(l, mid, a, mid, x),
			query_minw(mid + 1, r, mid + 1, b, x));
}

inline int query_cov(int l, int r, int x) {
	int k = get_node(l, r);
	if (l == r) {
		return tr[k].cov;
	}
	int mid = l + r >> 1;
	int ret = tr[k].cov;
	if (x > mid) {
		ret = min(ret, query_cov(mid + 1, r, x));
	} else {
		ret = min(ret, query_cov(l, mid, x));
	}
	return ret;
}

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

	cin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		cin >> edge[i].u >> edge[i].v >> edge[i].w;
		edge[i].id = i;
		edge[i].used = false;
	}

	sort(edge + 1, edge + m + 1, cmp_edge);
	for (int i = 1; i <= n; ++i) {
		fa[i] = i;
	}
	for (int i = 1; i <= m; ++i) {
		int u = edge[i].u;
		int v = edge[i].v;
		int uu = get_fa(u);
		int vv = get_fa(v);
		if (uu == vv) {
			continue;
		}
		edge[i].used = true;
		fa[uu] = vv;
		addedge(u, v, i);
		addedge(v, u, i);
		//cout << "link " << u << "-----" << v << endl;
	}

	memset(fa, 0, sizeof(fa));
	dfs1(1, 0);
	top[1] = 1;
	dfs2(1, 0);
	build(1, n);
	for (int i = 1; i <= m; ++i) {
		if (edge[i].used) {
			continue;
		}
		int u = edge[i].u;
		int v = edge[i].v;
		int nowans = INF;
		while (top[u] != top[v]) {
			if (dep[u] < dep[v]) {
				swap(u, v);
			}
//cout << "modify " << dfn[top[u]] << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = min(nowans, query_minw(1, n, dfn[top[u]], dfn[u], edge[i].w - 1));
			u = fa[top[u]];
		}
		if (u != v) {
			if (dep[u] < dep[v]) {
				swap(u, v);
			}
//cout << "modify " << dfn[v] + 1 << " " << dfn[u] << " " << edge[i].w - 1 << endl;
			nowans = min(nowans, query_minw(1, n, dfn[v] + 1, dfn[u], edge[i].w - 1));
		}
		ans[edge[i].id] = nowans - 1;
	}
	for (int i = 1; i <= m; ++i) {
		if (!edge[i].used) {
			continue;
		}
		int u = edge[i].u,
			v = edge[i].v;
		if (dep[u] < dep[v]) {
			swap(u, v);
		}
		ans[edge[i].id] = query_cov(1, n, dfn[u]);
		if (ans[edge[i].id] == INF) {
			ans[edge[i].id] = -1;
		}
	}

	//cout << query_cov(1, n, 4) << endl; return 0;
	for (int i = 1; i <= m; ++i) {
		cout << ans[i];
		if (i < m) {
			cout << " ";
		}
	}
	cout << endl;
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<18)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];
int depth[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			depth[it] = depth[v]+1;
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(depth[top[a]]>depth[top[b]])
					swap(a, b);

				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(depth[a]>depth[b])
				swap(a, b);

			insert(pos[a]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(depth[A[i]]>depth[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;

			if(res[i]+1==inf16)
				res[i] = -1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			res[i] = -inf16;

			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(depth[top[a]]>depth[top[b]])
					swap(a, b);

				res[i] = max(res[i], -query(pos[top[b]], pos[b])-1);
				b = p[top[b]];
			}

			if(depth[a]>depth[b])
				swap(a, b);

			res[i] = max(res[i], -query(pos[a]+1, pos[b])-1);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		cout << res[i] << " ";
	}
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<17)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		return r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(res, inf16, sizeof res);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			insert(pos[top[a]]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(pos[A[i]]>pos[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	/*for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				res[i] = min(res[i], -query(pos[top[b]], pos[b])-1);
				insert(pos[top[b]], pos[b], -C[i]);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			res[i] = min(res[i], -query(pos[top[a]]+1, pos[b])-1);
			insert(pos[top[a]]+1, pos[b], -C[i]);
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = m; i >= 1; --i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				res[i] = min(res[i], -query(pos[top[b]], pos[b])-1);
				insert(pos[top[b]], pos[b], -C[i]);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
					swap(a, b);

			res[i] = min(res[i], -query(pos[top[a]]+1, pos[b])-1);
			insert(pos[top[a]]+1, pos[b], -C[i]);
		}
	}*/

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			res[i] = -inf16;

			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				res[i] = max(res[i], -query(pos[top[b]], pos[b])-1);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			res[i] = max(res[i], -query(pos[top[a]]+1, pos[b])-1);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(res[i]+1==inf16)
			cout << -1 << " ";
		else
			cout << res[i] << " ";
	}
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<17)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];
int depth[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			depth[it] = depth[v]+1;
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(depth[top[a]]>depth[top[b]])
					swap(a, b);

				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(depth[a]>depth[b])
				swap(a, b);

			insert(pos[a]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(depth[A[i]]>depth[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;

			if(res[i]+1==inf16)
				res[i] = -1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			res[i] = -inf16;

			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(depth[top[a]]>depth[top[b]])
					swap(a, b);

				res[i] = max(res[i], -query(pos[top[b]], pos[b])-1);
				b = p[top[b]];
			}

			if(depth[a]>depth[b])
				swap(a, b);

			res[i] = max(res[i], -query(pos[a]+1, pos[b])-1);

			if(res[i]==-inf16)
				res[i] = -1;
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		cout << res[i] << " ";
	}
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<17)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		return r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(res, inf16, sizeof res);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			if(pos[A[i]]>pos[B[i]])
				swap(A[i], B[i]);

			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			insert(pos[top[a]]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(pos[A[i]]>pos[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				res[i] = min(res[i], query(pos[top[b]], pos[b])-1);
				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			res[i] = min(res[i], query(pos[top[a]]+1, pos[b])-1);
			insert(pos[top[a]]+1, pos[b], C[i]);
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], C[i]);
	}

	for(int i = m; i >= 1; --i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				res[i] = min(res[i], query(pos[top[b]], pos[b])-1);
				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			res[i] = min(res[i], query(pos[top[a]]+1, pos[b])-1);
			insert(pos[top[a]]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(res[i]+1==inf16)
			cout << -1 << " ";
		else
			cout << res[i] << " ";
	}
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<17)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];
int depth[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			depth[it] = depth[v]+1;
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(depth[top[a]]>depth[pos[b]])
					swap(a, b);

				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(depth[a]>depth[b])
				swap(a, b);

			insert(pos[a]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(depth[A[i]]>depth[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;

			if(res[i]+1==inf16)
				res[i] = -1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			res[i] = -inf16;

			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(depth[top[a]]>depth[pos[b]])
					swap(a, b);

				res[i] = max(res[i], -query(pos[top[b]], pos[b])-1);
				b = p[top[b]];
			}

			if(depth[a]>depth[b])
				swap(a, b);

			res[i] = max(res[i], -query(pos[a]+1, pos[b])-1);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		cout << res[i] << " ";
	}
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<17)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		return r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			insert(pos[a]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(pos[A[i]]>pos[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;

			if(res[i]+1==inf16)
				res[i] = -1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			res[i] = -inf16;

			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				res[i] = max(res[i], -query(pos[top[b]], pos[b])-1);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			res[i] = max(res[i], -query(pos[a]+1, pos[b])-1);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		cout << res[i] << " ";
	}
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<18)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];
int depth[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			depth[it] = depth[v]+1;
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(depth[top[a]]>depth[top[b]])
					swap(a, b);

				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(depth[a]>depth[b])
				swap(a, b);

			insert(pos[a]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(depth[A[i]]>depth[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;

			if(res[i]+1==inf16)
				res[i] = -1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			res[i] = -inf16;

			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(depth[top[a]]>depth[top[b]])
					swap(a, b);

				res[i] = max(res[i], -query(pos[top[b]], pos[b])-1);
				b = p[top[b]];
			}

			if(depth[a]>depth[b])
				swap(a, b);

			res[i] = max(res[i], -query(pos[a]+1, pos[b])-1);

			if(res[i]==-inf16)
				res[i] = -1;
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		cout << res[i] << " ";
	}
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<17)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		return r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			insert(pos[a]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(pos[A[i]]>pos[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;

			if(res[i]+1==inf16)
				res[i] = -1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			res[i] = -inf16;

			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				res[i] = max(res[i], -query(pos[top[b]], pos[b])-1);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			res[i] = max(res[i], -query(pos[a]+1, pos[b])-1);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		cout << res[i] << " ";
	}
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<17)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		return r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(res, inf16, sizeof res);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			insert(pos[top[a]]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(pos[A[i]]>pos[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				res[i] = min(res[i], query(pos[top[b]], pos[b])-1);
				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			res[i] = min(res[i], query(pos[top[a]]+1, pos[b])-1);
			insert(pos[top[a]]+1, pos[b], C[i]);
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], C[i]);
	}

	for(int i = m; i >= 1; --i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				res[i] = min(res[i], query(pos[top[b]], pos[b])-1);
				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
					swap(a, b);

			res[i] = min(res[i], query(pos[top[a]]+1, pos[b])-1);
			insert(pos[top[a]]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(res[i]+1==inf16)
			cout << -1 << " ";
		else
			cout << res[i] << " ";
	}
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<17)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[top[a]]>pos[pos[b]])
					swap(a, b);

				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			insert(pos[a]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(pos[A[i]]>pos[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;

			if(res[i]+1==inf16)
				res[i] = -1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			res[i] = -inf16;

			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[top[a]]>pos[pos[b]])
					swap(a, b);

				res[i] = max(res[i], -query(pos[top[b]], pos[b])-1);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			res[i] = max(res[i], -query(pos[a]+1, pos[b])-1);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		cout << res[i] << " ";
	}
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<17)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		return r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			insert(pos[a]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(pos[A[i]]>pos[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;

			if(res[i]+1==inf16)
				res[i] = -1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			res[i] = -inf16;

			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				res[i] = max(res[i], -query(pos[top[b]], pos[b])-1);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			res[i] = max(res[i], -query(pos[a]+1, pos[b])-1);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		cout << res[i] << " ";
	}
}


#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define ull unsigned long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define flush fflush(stdout)
using namespace std;

#define N 200007
#define M (1<<18)

int n, m, curr;

int A[N];
int B[N];
int C[N];
int r[N];
VI G[N];
VI e;
bitset<N> ins;
int size[N];
int top[N];
int p[N];
int pos[N];
int sub[2*M];
int load[2*M];
int res[N];
int depth[N];

bool comp(int a, int b)
{
	return C[a]<C[b];
}

int Find(int a)
{
	if(r[a]!=a)
		r[a] = Find(r[a]);

	return r[a];
}

void insert(int a, int b, int c, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return;

	if(a<=l && r<=b)
	{
		load[v] = min(load[v], c);
		sub[v] = min(sub[v], c);
		return;
	}

	insert(a, b, c, 2*v, l, (l+r)/2);
	insert(a, b, c, 2*v+1, (l+r)/2+1, r);
	sub[v] = min(load[v], min(sub[2*v], sub[2*v+1]));
}

int query(int a, int b, int path = inf16, int v = 1, int l = 0, int r = M-1)
{
	if(a>b || a>r || l>b)
		return inf16;

	if(a<=l && r<=b)
		return min(sub[v], path);

	return min(query(a, b, min(path, load[v]), 2*v, l, (l+r)/2), query(a, b, min(path, load[v]), 2*v+1, (l+r)/2+1, r));
}

void dfs(int v)
{
	size[v] = 1;

	for(auto it:G[v])
	{
		if(it!=p[v])
		{
			depth[it] = depth[v]+1;
			p[it] = v;
			dfs(it);
			size[v] += size[it];
		}
	}
}

void hld(int v, int r)
{
	top[v] = r;
	pos[v] = ++curr;

	auto h = mp(-1, -1);

	for(auto it:G[v])
	{
		if(it!=p[v])
			h = max(h, mp(size[it], it));
	}

	if(h.nd!=-1)
	{
		hld(h.nd, r);

		for(auto it:G[v])
		{
			if(it!=p[v] && it!=h.nd)
				hld(it, it);
		}
	}
}

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

	cin >> n >> m;

	for(int i = 1; i <= n; ++i)
	{
		r[i] = i;
	}

	for(int i = 1; i <= m; ++i)
	{
		cin >> A[i] >> B[i] >> C[i];
		e.pb(i);
	}

	sort(ALL(e), comp);

	for(auto it:e)
	{
		int a = Find(A[it]);
		int b = Find(B[it]);

		if(a!=b)
		{
			r[a] = b;
			G[A[it]].pb(B[it]);
			G[B[it]].pb(A[it]);
			ins[it] = 1;
		}
	}

	dfs(1);
	hld(1, 1);

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				insert(pos[top[b]], pos[b], C[i]);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			insert(pos[a]+1, pos[b], C[i]);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
		{
			if(pos[A[i]]>pos[B[i]])
				swap(A[i], B[i]);

			res[i] = query(pos[B[i]], pos[B[i]])-1;

			if(res[i]+1==inf16)
				res[i] = -1;
		}
	}

	memset(sub, inf16, sizeof sub);
	memset(load, inf16, sizeof load);

	for(int i = 1; i <= m; ++i)
	{
		if(ins[i])
			insert(pos[B[i]], pos[B[i]], -C[i]);
	}

	for(int i = 1; i <= m; ++i)
	{
		if(!ins[i])
		{
			res[i] = -inf16;

			int a = A[i];
			int b = B[i];

			while(top[a]!=top[b])
			{
				if(pos[a]>pos[b])
					swap(a, b);

				res[i] = max(res[i], -query(pos[top[b]], pos[b])-1);
				b = p[top[b]];
			}

			if(pos[a]>pos[b])
				swap(a, b);

			res[i] = max(res[i], -query(pos[a]+1, pos[b])-1);
		}
	}

	for(int i = 1; i <= m; ++i)
	{
		cout << res[i] << " ";
	}
}


#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] = max(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_max(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 = -1e9;
	rep(i, 20) if(1<<i & (dep[x] - dep[lc])) res = max(res, upl[x][i]), x = up[x][i];
	rep(i, 20) if(1<<i & (dep[y] - dep[lc])) res = max(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_max(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;
};