#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 100;   const int LVL = 18;   int n, m;   int a[N];   int b[N];   int c[N];   int h[N];   int inplace[N], parent[N];   int inmst[N];   int dp[N][LVL], mini[N][LVL], maxi[N][LVL];   int ans[N];   vector<int> tree[N];   int find(int node) {     if (parent[node] == node) {       return node;     }     return parent[node] = find(parent[node]);   }   void addEdge(int edgeNo) {     if (find(a[edgeNo]) != find(b[edgeNo])) {       inmst[edgeNo] = true;       parent[find(a[edgeNo])] = find(b[edgeNo]);       tree[a[edgeNo]].push_back(edgeNo);       tree[b[edgeNo]].push_back(edgeNo);     } else {       inmst[edgeNo] = false;     }   }   pair<int, int> lca(int a, int b) {     if (h[a] > h[b]) {       int temp = a;       a = b;       b = temp;     }     int diff = abs(h[a] - h[b]);     int max_weight_edge = 0;     for (int i = 0; i <= 17; i++) {       if ((diff >> i) & 1) {         max_weight_edge = max(maxi[b][i], max_weight_edge);         b = dp[b][i];       }     }     if (b == a) {       return make_pair(a, max_weight_edge);     }     for (int i = 17; i >= 0; i--) {       if (dp[a][i] != dp[b][i]) {         max_weight_edge = max(max_weight_edge, maxi[a][i]);         max_weight_edge = max(max_weight_edge, maxi[b][i]);         a = dp[a][i];         b = dp[b][i];       }     }     return make_pair(dp[a][0], max(max_weight_edge, max(maxi[a][0], maxi[b][0])));   }   void dfs(int node, int par) {     dp[node][0] = par;     for (int edgeNo : tree[node]) {       int child = a[edgeNo] ^ b[edgeNo] ^ node;       if (child != par) {         maxi[child][0] = c[edgeNo];         h[child] = h[node] + 1;         dfs(child, node);       }     }   }   void dfs2(int node, int par) {     for (int edgeNo : tree[node]) {       int next = a[edgeNo] ^ b[edgeNo] ^ node;       if (next != par) {         ans[edgeNo] = mini[next][0];         dfs2(next, node);       }     }   }   void update(int node, int lca, int val) {     int diff = abs(h[node] - h[lca]);     for (int i = LVL - 1; i >= 0; i--) {       if (diff & (1 << i)) {         mini[node][i] = min(mini[node][i], val);         node = dp[node][i];       }     }   }   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) {       cin >> a[i] >> b[i] >> c[i];       inplace[i] = i;     }     for (int i = 1; i <= n; i++) {       parent[i] = i;     }     sort(inplace + 1, inplace + 1 + m, [](int x, int y) {       return make_pair(c[x], make_pair(a[x], b[x])) <              make_pair(c[y], make_pair(a[y], b[y]));     });     for (int i = 1; i <= m; i++) {       addEdge(inplace[i]);     }     h[1] = 0;     dfs(1, 0);     for (int l = 1; l < LVL; l++) {       for (int i = 1; i <= n; i++) {         dp[i][l] = dp[dp[i][l - 1]][l - 1];         maxi[i][l] = max(maxi[i][l - 1], maxi[dp[i][l - 1]][l - 1]);       }     }     for (int j = 0; j < LVL; j++) {       for (int i = 1; i <= n; i++) {         mini[i][j] = 1e9 + 7;       }     }     for (int i = 1; i <= m; i++) {       if (inmst[i]) {         ans[i] = 1e9 + 7;       } else {         pair<int, int> result = lca(a[i], b[i]);         ans[i] = result.second;         update(a[i], result.first, c[i]);         update(b[i], result.first, c[i]);       }     }     for (int l = LVL - 1; l > 0; l--) {       for (int i = 1; i <= n; i++) {         mini[i][l - 1] = min(mini[i][l - 1], mini[i][l]);         mini[dp[i][l - 1]][l - 1] = min(mini[i][l], mini[dp[i][l - 1]][l - 1]);       }     }     dfs2(1, 0);     for (int i = 1; i <= m; i++) {       if (ans[i] == 1e9 + 7) {         ans[i] = 0;       }     }     for (int i = 1; i <= m; i++) {       cout << ans[i] - 1 << endl;     }   }