#include <bits/stdc++.h>
using namespace std;
int n, m, q;
struct ss {
  int u, v, w, bh;
  bool ju = 0;
} ee[200010];
bool cmp(ss a, ss b) { return a.w < b.w; }
int ff[200010], d[200010];
int find(int a) {
  if (ff[a] == a) {
    return a;
  }
  return ff[a] = find(ff[a]);
}
int head[200010], cnt = 0;
struct sd {
  int to, next, dis;
} e[400010];
inline void add(int a, int b, int c) {
  cnt++;
  e[cnt].to = b;
  e[cnt].dis = c;
  e[cnt].next = head[a];
  head[a] = cnt;
}
int dep[200010], f[200010][22], ma[200010][22];
void dfs(int u, int fa) {
  f[u][0] = fa;
  dep[u] = dep[fa] + 1;
  for (int i = 1; i <= 20; i++) {
    f[u][i] = f[f[u][i - 1]][i - 1];
    ma[u][i] = max(ma[u][i - 1], ma[f[u][i - 1]][i - 1]);
  }
  for (int i = head[u]; i != 0; i = e[i].next) {
    if (e[i].to == fa) {
      continue;
    }
    ma[e[i].to][0] = e[i].dis;
    dfs(e[i].to, u);
  }
  return;
}
int llca(int a, int b) {
  int ans = 0;
  if (dep[a] < dep[b]) {
    swap(a, b);
  }
  for (int i = 20; i >= 0; i--) {
    if (dep[a] - (1 << i) >= dep[b]) {
      ans = max(ans, ma[a][i]);
      a = f[a][i];
    }
  }
  if (a == b) {
    return ans;
  }
  for (int i = 20; i >= 0; i--) {
    if (f[a][i] != f[b][i]) {
      ans = max(ans, max(ma[a][i], ma[b][i]));
      a = f[a][i];
      b = f[b][i];
    }
  }
  ans = max(ans, max(ma[a][0], ma[b][0]));
  return ans;
}
int k[200010];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &ee[i].u, &ee[i].v, &ee[i].w);
    ee[i].bh = i;
  }
  sort(ee + 1, ee + m + 1, cmp);
  for (int i = 1; i <= n; i++) ff[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = find(ee[i].u), v = find(ee[i].v);
    if (u != v) {
      ff[u] = v;
      ee[i].ju = 1;
      add(ee[i].u, ee[i].v, ee[i].w);
      add(ee[i].v, ee[i].u, ee[i].w);
    }
  }
  dfs(1, 0);
  memset(d, -1, sizeof(d));
  memset(k, 0x3f3f3f3f, sizeof(k));
  for (int i = 1; i <= m; i++) {
    if (!ee[i].ju) {
      int yy = llca(ee[i].u, ee[i].v);
      d[ee[i].bh] = yy - 1;
    }
  }
  for (int i = 1; i <= n; i++) ff[i] = i;
  for (int i = 1; i <= m; i++) {
    if (!ee[i].ju) {
      int u = find(ee[i].u), v = find(ee[i].v);
      while (u != v) {
        if (dep[u] < dep[v]) swap(u, v);
        k[u] = ee[i].w - 1;
        ff[u] = f[u][0];
        u = find(u);
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    if (ee[i].ju) {
      if (dep[ee[i].u] < dep[ee[i].v]) swap(ee[i].u, ee[i].v);
      if (f[ee[i].u][0] == ee[i].v && k[ee[i].u] != 0x3f3f3f3f) {
        d[ee[i].bh] = k[ee[i].u];
      }
    }
  }
  for (int i = 1; i <= m; i++) printf("%d ", d[i]);
  return 0;
}
