#include <bits/stdc++.h>
using namespace std;
int n, m, head[200005], rev[200005], nm[200005], son[200005], fts[200005],
    top[200005], cnt, tr[800005], rs, lazy[800005], p[200005][21],
    pw[200005][21], d[200005], ba[200005], fa[200005], vis[200005], res,
    ans[200005];
struct w {
  int u, v, w, num;
} a[200005];
struct zk {
  int v, next, w;
} e[400005];
int find(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
bool cmp(w x, w y) { return x.w < y.w; }
void add(int u, int v, int w) {
  e[++cnt].v = v;
  e[cnt].w = w;
  e[cnt].next = head[u];
  head[u] = cnt;
}
void dfs1(int u, int f, int w) {
  son[u] = 1;
  d[u] = d[f] + 1;
  ba[u] = f;
  p[u][0] = f;
  pw[u][0] = w;
  for (int i = 1; i <= 20; i++) {
    p[u][i] = p[p[u][i - 1]][i - 1];
    pw[u][i] = max(pw[u][i - 1], pw[p[u][i - 1]][i - 1]);
  }
  int hh = 0;
  for (int i = head[u]; ~i; i = e[i].next) {
    int v = e[i].v;
    if (v == f) continue;
    dfs1(v, u, e[i].w);
    son[u] += son[v];
    if (son[v] > hh) hh = son[v], fts[u] = v;
  }
}
void dfs2(int u, int f) {
  if (fts[u]) {
    nm[fts[u]] = ++rs;
    rev[rs] = fts[u];
    top[fts[u]] = top[u];
    dfs2(fts[u], u);
  }
  for (int i = head[u]; ~i; i = e[i].next) {
    int v = e[i].v;
    if (v == f || v == fts[u]) continue;
    top[v] = v;
    nm[v] = ++rs;
    rev[rs] = v;
    dfs2(v, u);
  }
}
int lca(int a, int b) {
  int res = 0;
  if (d[a] > d[b]) swap(a, b);
  for (int i = 20; i >= 0; i--) {
    if (d[a] <= d[p[b][i]]) {
      res = max(res, pw[b][i]);
      b = p[b][i];
    }
  }
  if (a == b) return res;
  for (int i = 20; i >= 0; i--) {
    if (p[a][i] != p[b][i]) {
      res = max(res, pw[a][i]);
      res = max(res, pw[b][i]);
      a = p[a][i];
      b = p[b][i];
    }
  }
  return max(res, max(pw[b][0], pw[a][0]));
}
void pushup(int u) { tr[u] = min(tr[u << 1], tr[u << 1 | 1]); }
void pushdown(int u) {
  if (lazy[u] != 0X3f3f3f3f) {
    tr[u << 1] = min(tr[u << 1], lazy[u]);
    lazy[u << 1] = min(lazy[u << 1], lazy[u]);
    tr[u << 1 | 1] = min(tr[u << 1 | 1], lazy[u]);
    lazy[u << 1 | 1] = min(lazy[u << 1 | 1], lazy[u]);
    lazy[u] = 0X3f3f3f3f;
  }
}
void update(int u, int l, int r, int L, int R, int w) {
  if (l > r) return;
  if (l >= L && r <= R) {
    tr[u] = min(tr[u], w);
    lazy[u] = min(lazy[u], w);
    return;
  }
  pushdown(u);
  int mid = (l + r) >> 1;
  if (L <= mid) update(u << 1, l, mid, L, R, w);
  if (R > mid) update(u << 1 | 1, mid + 1, r, L, R, w);
  pushup(u);
}
void qr(int u, int l, int r, int L, int R) {
  if (l >= L && r <= R) {
    res = min(res, tr[u]);
    return;
  }
  pushdown(u);
  int mid = (l + r) >> 1;
  if (L <= mid) qr(u << 1, l, mid, L, R);
  if (R > mid) qr(u << 1 | 1, mid + 1, r, L, R);
}
void addtr(int a, int b, int c) {
  while (top[a] != top[b]) {
    if (d[top[a]] > d[top[b]]) swap(a, b);
    update(1, 1, rs, nm[top[b]], nm[b], c);
    b = ba[top[b]];
  }
  if (d[a] > d[b]) swap(a, b);
  update(1, 1, rs, nm[fts[a]], nm[b], c);
}
int qrtr(int a, int b) {
  res = 0X3f3f3f3f;
  while (top[a] != top[b]) {
    if (d[top[a]] > d[top[b]]) swap(a, b);
    qr(1, 1, rs, nm[top[b]], nm[b]);
    b = ba[top[b]];
  }
  if (d[a] > d[b]) swap(a, b);
  qr(1, 1, rs, nm[fts[a]], nm[b]);
  return res;
}
int main() {
  memset(head, -1, sizeof(head));
  memset(tr, 0X3f, sizeof(tr));
  memset(lazy, 0X3f, sizeof(lazy));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++)
    scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w), a[i].num = i;
  sort(a + 1, a + m + 1, cmp);
  int hh = 0;
  for (int i = 1; i <= m; i++) {
    int u = a[i].u, v = a[i].v;
    int w = a[i].w;
    int fu = find(u), fv = find(v);
    if (fu != fv) {
      fa[fu] = fv;
      add(u, v, w);
      add(v, u, w);
      vis[i] = 1;
      hh++;
    }
    if (hh == n - 1) break;
  }
  dfs1(1, 0, 0);
  top[1] = nm[1] = rs = 1;
  dfs2(1, 0);
  for (int i = 1; i <= m; i++) {
    if (vis[i] == 1) continue;
    addtr(a[i].u, a[i].v, a[i].w);
  }
  for (int i = 1; i <= m; i++) {
    if (vis[i] == 1) {
      ans[a[i].num] = qrtr(a[i].u, a[i].v) - 1;
    } else {
      ans[a[i].num] = lca(a[i].u, a[i].v) - 1;
    }
    if (ans[a[i].num] == 0X3f3f3f3f - 1) ans[a[i].num] = -1;
  }
  for (int i = 1; i <= m; i++) printf("%d ", ans[i]);
  printf("\n");
}
