#include <bits/stdc++.h>
const int N = 2e5 + 10, INF = 2e9;
int cnt, la[N];
struct edg {
  int v, w, la;
  inline void set(int _v, int _w, int _la) {
    v = _v;
    w = _w;
    la = _la;
  }
} g[N << 1];
inline void add(int u, int v, int w) {
  cnt++;
  g[cnt].set(v, w, la[u]);
  la[u] = cnt;
}
int f[N];
int find(int x) { return f[x] > 0 ? f[x] = find(f[x]) : x; }
bool un(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return false;
  if (f[x] > f[y])
    f[y] += f[x], f[x] = y;
  else
    f[x] += f[y], f[y] = x;
  return true;
}
struct edge {
  int u, v, w;
  bool flag;
  int id;
  inline void read(int i) {
    scanf("%d%d%d", &u, &v, &w);
    id = i;
  }
  bool operator<(const edge &e) const { return w < e.w; }
  inline void add() {
    ::add(u, v, w);
    ::add(v, u, w);
  }
  inline void un() {
    if (flag = ::un(u, v)) add();
  }
} e[N];
int n, mx[N << 2], tag[N << 2], val[N];
inline int max(int a, int b) { return a > b ? a : b; }
inline void fmin(int &a, int b) {
  if (a > b) a = b;
}
void build(int rt, int l, int r) {
  if (l == r) {
    mx[rt] = val[l];
    return;
  }
  int m = (l + r) >> 1;
  build(rt << 1, l, m);
  build(rt << 1 | 1, m + 1, r);
  mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
}
int update(int rt, int l, int r, int ql, int qr, int x) {
  if (ql <= l && r <= qr) {
    if (!tag[rt]) tag[rt] = x;
    return mx[rt];
  }
  int m = (l + r) >> 1, a = 0, b = 0;
  if (ql <= m) a = update(rt << 1, l, m, ql, qr, x);
  if (qr > m) b = update(rt << 1 | 1, m + 1, r, ql, qr, x);
  return max(a, b);
}
void query(int rt, int l, int r, int x, int &y) {
  if (tag[rt]) fmin(y, tag[rt]);
  if (l == r) {
    if (y == INF) y = 0;
    return;
  }
  int m = (l + r) >> 1;
  if (x <= m)
    query(rt << 1, l, m, x, y);
  else
    query(rt << 1 | 1, m + 1, r, x, y);
}
int son[N], siz[N], top[N], dep[N], id[N];
void dfs1(int now, int las) {
  int i;
  siz[now] = 1;
  f[now] = las;
  dep[now] = dep[las] + 1;
  for (i = la[now]; i; i = g[i].la)
    if (g[i].v != las) {
      dfs1(g[i].v, now);
      siz[now] += siz[g[i].v];
      if (siz[g[i].v] > siz[son[now]]) son[now] = g[i].v;
    }
}
void dfs2(int now, int tp) {
  top[now] = tp;
  id[now] = ++cnt;
  if (son[now]) dfs2(son[now], tp);
  int i;
  for (i = la[now]; i; i = g[i].la)
    if (g[i].v == f[now])
      val[id[now]] = g[i].w;
    else if (g[i].v != son[now])
      dfs2(g[i].v, g[i].v);
}
int LCA(int x, int y, int val) {
  int mx = 0, tmp;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) tmp = x, x = y, y = tmp;
    tmp = update(1, 1, n, id[top[x]], id[x], val);
    if (mx < tmp) mx = tmp;
    x = f[top[x]];
  }
  if (x != y) {
    if (dep[x] > dep[y]) tmp = x, x = y, y = tmp;
    tmp = update(1, 1, n, id[x] + 1, id[y], val);
    if (mx < tmp) mx = tmp;
  }
  return mx;
}
int main() {
  int i, m;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) e[i].read(i);
  std::sort(e + 1, e + 1 + m);
  memset(f, -1, sizeof f);
  for (i = 1; i <= m; i++) e[i].un();
  cnt = 0;
  dfs1(1, 0);
  dfs2(1, 1);
  build(1, 1, n);
  for (i = 1; i <= m; i++)
    if (!e[i].flag)
      val[e[i].id] = LCA(e[i].u, e[i].v, e[i].w);
    else
      val[e[i].id] = INF;
  for (i = 1; i <= m; i++)
    if (e[i].flag)
      query(1, 1, n, dep[e[i].v] > dep[e[i].u] ? id[e[i].v] : id[e[i].u],
            val[e[i].id]);
  for (i = 1; i <= m; i++) printf("%d ", val[i] - 1);
  return 0;
}
