#include <bits/stdc++.h>   using namespace std;   struct edge {     int x, y, z, id;   } e[200005];   vector<int> to[200005];   int fa[200005], f[200005][18], deep[200005], g[200005][18], id[200005],       ans[200005];   bool flag[200005];   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   void dfs(int x, int fa) {     f[x][0] = fa, deep[x] = deep[fa] + 1;     for (auto tar : to[x])       if (tar != fa) dfs(tar, x);   }   pair<int, int> lca(int x, int y) {     int res = 0;     if (deep[x] < deep[y]) swap(x, y);     for (int k = 17; k >= 0; k--)       if (deep[f[x][k]] >= deep[y]) res = max(res, g[x][k]), x = f[x][k];     if (x == y) return {x, res};     for (int k = 17; k >= 0; k--)       if (f[x][k] != f[y][k])         res = max(res, max(g[x][k], g[y][k])), x = f[x][k], y = f[y][k];     res = max(res, max(g[x][0], g[y][0]));     return {f[x][0], res};   }   void merge(int from, int to, int v) {     from = find(from);     while (deep[from] > deep[to]) {       ans[id[from]] = v;       fa[from] = find(f[from][0]);       from = find(from);     }   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++)       scanf( %d%d%d , &e[i].x, &e[i].y, &e[i].z), e[i].id = i;     sort(e + 1, e + m + 1, [&](edge a, edge b) { return a.z < b.z; });     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= m; i++) {       int fx = find(e[i].x), fy = find(e[i].y);       if (fx != fy)         to[e[i].x].push_back(e[i].y), to[e[i].y].push_back(e[i].x), fa[fx] = fy;       else         flag[i] = 1;     }     dfs(1, 0);     for (int i = 1; i <= m; i++)       if (!flag[i])         g[deep[e[i].x] < deep[e[i].y] ? e[i].y : e[i].x]          [0] = e[i].z,             id[deep[e[i].x] < deep[e[i].y] ? e[i].y : e[i].x] = e[i].id;     for (int k = 1; k <= 17; k++)       for (int i = 1; i <= n; i++)         f[i][k] = f[f[i][k - 1]][k - 1],         g[i][k] = max(g[i][k - 1], g[f[i][k - 1]][k - 1]);     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= m; i++)       if (flag[i]) {         auto g = lca(e[i].x, e[i].y);         ans[e[i].id] = g.second;         merge(e[i].x, g.first, e[i].z), merge(e[i].y, g.first, e[i].z);       }     for (int i = 1; i <= m; i++) printf( %d  , ans[i] ? ans[i] - 1 : -1);     return 0;   }   