#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10, inf = 1e9 + 1;
int egh[maxn], ect;
int N, M, fa[maxn][20], bf[maxn], bm[maxn], ma[maxn][20], dep[maxn];
int ans[maxn];
struct Edge {
  int a, b, l, ne;
  bool used;
} eg[maxn], eg2[maxn * 2];
inline long long rd() {
  long long x = 0;
  char c = getchar();
  int neg = 1;
  while (c < '0' || c > '9') {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * neg;
}
inline int getf(int x) { return bf[x] == x ? x : bf[x] = getf(bf[x]); }
inline bool cmp(Edge a, Edge b) { return a.l < b.l; }
inline void adeg(int a, int b, int c) {
  eg2[++ect].b = b;
  eg2[ect].ne = egh[a];
  eg2[ect].l = c, egh[a] = ect;
}
void dfs(int x) {
  for (int i = 0; fa[x][i] && fa[fa[x][i]]; i++)
    fa[x][i + 1] = fa[fa[x][i]][i],
              ma[x][i + 1] = max(ma[x][i], ma[fa[x][i]][i]);
  for (int i = egh[x]; i; i = eg2[i].ne) {
    int b = eg2[i].b;
    if (b == fa[x][0]) continue;
    ma[b][0] = eg2[i].l;
    fa[b][0] = x;
    dep[b] = dep[x] + 1;
    dfs(b);
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int re = 0;
  for (int i = 19; i >= 0 && dep[x] != dep[y]; i--)
    if (fa[x][i] && dep[fa[x][i]] >= dep[y])
      re = max(re, ma[x][i]), x = fa[x][i];
  if (x == y) return re;
  for (int i = 19; i >= 0; i--)
    if (fa[x][i] != fa[y][i])
      re = max(re, max(ma[x][i], ma[y][i])), x = fa[x][i], y = fa[y][i];
  return max(re, max(ma[x][0], ma[y][0]));
}
int main() {
  int i, j, k;
  N = rd(), M = rd();
  for (i = 1; i <= M; i++) {
    eg[i].a = rd(), eg[i].b = rd(), eg[i].l = rd();
    eg[i].ne = i;
  }
  sort(eg + 1, eg + M + 1, cmp);
  for (i = 1; i <= N; i++) bf[i] = i;
  for (i = 1, j = 0; i <= M && j < N - 1; i++) {
    int a = getf(eg[i].a), b = getf(eg[i].b);
    if (a != b) {
      bf[a] = b;
      adeg(eg[i].a, eg[i].b, eg[i].l);
      adeg(eg[i].b, eg[i].a, eg[i].l);
      eg[i].l = inf;
      eg[i].used = 1;
      j++;
    }
  }
  dep[1] = 1;
  dfs(1);
  sort(eg + 1, eg + M + 1, cmp);
  for (i = 1; i <= N; i++) bf[i] = i, bm[i] = inf;
  for (i = 1; i <= M; i++) {
    if (eg[i].used) continue;
    int a = getf(eg[i].a), b = getf(eg[i].b);
    while (a != b) {
      if (dep[a] < dep[b]) swap(a, b);
      int bb = getf(fa[a][0]);
      bf[a] = bb, bm[a] = eg[i].l;
      a = bb;
    }
  }
  for (i = 1; i <= M; i++)
    if (eg[i].used) {
      int a = eg[i].a, b = eg[i].b;
      if (dep[a] < dep[b]) swap(a, b);
      if (bm[a] < inf)
        ans[eg[i].ne] = bm[a] - 1;
      else
        ans[eg[i].ne] = -1;
    } else
      ans[eg[i].ne] = lca(eg[i].a, eg[i].b) - 1;
  for (i = 1; i <= M; i++) printf("%d ", ans[i]);
  return 0;
}
