#include <bits/stdc++.h>
using namespace std;
int read() {
  char ch = getchar();
  int x = 0, pd = 0;
  while (ch < '0' || ch > '9') pd ^= ch == '-', ch = getchar();
  while ('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
  return pd ? -x : x;
}
const int maxn = 200005;
const int inf = 0x3f3f3f3f;
int n, m;
struct Edge {
  int a, b, w, id, chs;
} E[maxn];
int ans[maxn];
bool cmp1(Edge x, Edge y) { return x.w < y.w; }
bool cmp2(Edge x, Edge y) { return x.id < y.id; }
int e, bg[maxn], to[maxn * 2], nx[maxn * 2], c[maxn * 2];
void link(int u, int v, int _c) {
  to[++e] = v, c[e] = _c, nx[e] = bg[u], bg[u] = e;
}
struct Union {
  int fa[maxn];
  int findfa(int x) { return fa[x] == x ? x : fa[x] = findfa(fa[x]); }
  void init() {
    for (int i = 1; i <= n; i++) fa[i] = i;
  }
  bool check(int x, int y) { return findfa(x) == findfa(y); }
  void connect(int x, int y) { fa[findfa(x)] = y; }
} U;
void kruskal() {
  U.init();
  sort(E + 1, E + m + 1, cmp1);
  for (int i = 1; i <= m; i++) {
    int a = E[i].a, b = E[i].b;
    if (U.check(a, b)) continue;
    U.connect(a, b);
    link(a, b, E[i].w), link(b, a, E[i].w);
    E[i].chs = 1;
  }
}
int fa[maxn], dep[maxn], size[maxn], son[maxn];
int tot, id[maxn], rk[maxn], top[maxn];
int val[maxn];
void dfs1(int u) {
  dep[u] = dep[fa[u]] + 1, size[u] = 1;
  for (int i = bg[u]; i; i = nx[i]) {
    int v = to[i];
    if (v == fa[u]) continue;
    val[v] = c[i], fa[v] = u, dfs1(v);
    size[u] += size[v];
    if (size[v] > size[son[u]]) son[u] = v;
  }
}
void dfs2(int u, int t) {
  top[u] = t, rk[id[u] = ++tot] = u;
  if (son[u])
    dfs2(son[u], t);
  else
    return;
  for (int i = bg[u]; i; i = nx[i])
    if (to[i] != son[u] && to[i] != fa[u]) dfs2(to[i], to[i]);
}
int f[maxn][20];
void buildST() {
  for (int i = 2; i <= n; i++) f[i][0] = val[rk[i]];
  for (int j = 1, L = 1; j < 18; j++, L <<= 1)
    for (int i = 1; i + L * 2 - 1 <= n; i++)
      f[i][j] = max(f[i][j - 1], f[i + L][j - 1]);
}
int queryST(int x, int y) {
  int len = y - x + 1, lg = log2(len);
  return max(f[x][lg], f[y - (1 << lg) + 1][lg]);
}
int qrange(int x, int y) {
  int res = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] > dep[top[y]]) swap(x, y);
    res = max(res, queryST(id[top[y]], id[y]));
    y = fa[top[y]];
  }
  if (x == y) return res;
  if (dep[x] > dep[y]) swap(x, y);
  return max(res, queryST(id[x] + 1, id[y]));
}
int mn[maxn << 2], tg[maxn << 2];
void build(int rt, int l, int r) {
  mn[rt] = tg[rt] = inf;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build((rt << 1), l, mid), build((rt << 1 | 1), mid + 1, r);
}
void pushdown(int rt) {
  int t = tg[rt];
  mn[(rt << 1)] = min(mn[(rt << 1)], t), tg[(rt << 1)] = min(tg[(rt << 1)], t);
  mn[(rt << 1 | 1)] = min(mn[(rt << 1 | 1)], t),
                tg[(rt << 1 | 1)] = min(tg[(rt << 1 | 1)], t);
  tg[rt] = inf;
}
void update(int rt, int l, int r, int L, int R, int x) {
  if (L <= l && r <= R) {
    mn[rt] = min(mn[rt], x);
    tg[rt] = min(tg[rt], x);
    return;
  }
  if (tg[rt] < inf) pushdown(rt);
  int mid = (l + r) >> 1;
  if (L <= mid) update((rt << 1), l, mid, L, R, x);
  if (R > mid) update((rt << 1 | 1), mid + 1, r, L, R, x);
}
int query(int rt, int l, int r, int x) {
  if (l == r) return mn[rt];
  if (tg[rt] < inf) pushdown(rt);
  int mid = (l + r) >> 1;
  if (x <= mid) return query((rt << 1), l, mid, x);
  return query((rt << 1 | 1), mid + 1, r, x);
}
void urange(int x, int y, int z) {
  while (top[x] != top[y]) {
    if (dep[top[x]] > dep[top[y]]) swap(x, y);
    update(1, 1, n, id[top[y]], id[y], z);
    y = fa[top[y]];
  }
  if (x == y) return;
  if (dep[x] > dep[y]) swap(x, y);
  update(1, 1, n, id[x] + 1, id[y], z);
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    E[i].a = read(), E[i].b = read(), E[i].w = read();
    E[i].id = i;
  }
  kruskal();
  dfs1(1), dfs2(1, 1);
  build(1, 1, n);
  buildST();
  sort(E + 1, E + m + 1, cmp2);
  for (int i = 1; i <= m; i++)
    if (!E[i].chs) {
      ans[i] = qrange(E[i].a, E[i].b) - 1;
      urange(E[i].a, E[i].b, E[i].w);
    }
  for (int i = 1; i <= m; i++)
    if (E[i].chs) {
      int x = query(1, 1, n, max(id[E[i].a], id[E[i].b]));
      printf("%d ", x == inf ? -1 : x - 1);
    } else
      printf("%d ", ans[i]);
  return 0;
}
