#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int N = 200005;
int n, m;
int f[N];
int head[N], nxt[N * 2], to[N * 2], w[N * 2], cnt;
int dfn[N], DFN, dep[N], top[N], fa[N], size[N], son[N];
bool vis[N];
int ed[N], tree[N];
int Max[N << 2], lazy[N << 2];
int ans[N];
int Ans[N];
struct edge {
  int a, b, c, id;
  friend bool operator<(edge a, edge b) { return a.c < b.c; }
} e[N];
inline void insert(int a, int b, int c) {
  to[++cnt] = b;
  nxt[cnt] = head[a];
  head[a] = cnt;
  w[cnt] = c;
  to[++cnt] = a;
  nxt[cnt] = head[b];
  head[b] = cnt;
  w[cnt] = c;
}
int getf(int x) {
  if (x == f[x]) return x;
  return f[x] = getf(f[x]);
}
void dfs1(int x, int f) {
  fa[x] = f;
  size[x] = 1;
  for (int i = head[x]; i; i = nxt[i])
    if (to[i] != f) {
      ed[to[i]] = w[i];
      dfs1(to[i], x);
      size[x] += size[to[i]];
      if (size[to[i]] > size[son[x]]) son[x] = to[i];
    }
}
void dfs2(int x, int f) {
  top[x] = f;
  dfn[x] = ++DFN;
  tree[DFN] = x;
  dep[x] = dep[fa[x]] + 1;
  if (son[x]) dfs2(son[x], f);
  for (int i = head[x]; i; i = nxt[i])
    if (to[i] != fa[x] && to[i] != son[x]) dfs2(to[i], to[i]);
}
void build(int num, int l, int r) {
  lazy[num] = 2e9;
  if (l == r) {
    Max[num] = e[ed[tree[l]]].c;
    return;
  }
  int mid = (l + r) >> 1;
  build(num << 1, l, mid);
  build(num << 1 | 1, mid + 1, r);
  Max[num] = max(Max[num << 1], Max[num << 1 | 1]);
}
int getMx(int num, int l, int r, int x, int y, int k) {
  if (x <= l && r <= y) {
    lazy[num] = min(lazy[num], k);
    return Max[num];
  }
  int mid = (l + r) >> 1, Mx = 0;
  if (x <= mid) Mx = getMx(num << 1, l, mid, x, y, k);
  if (y > mid) Mx = max(Mx, getMx(num << 1 | 1, mid + 1, r, x, y, k));
  return Mx;
}
int solve(int a, int b, int c) {
  int x = a, y = b, Mx = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    Mx = max(Mx, getMx(1, 2, n, dfn[top[x]], dfn[x], c));
    x = fa[top[x]];
  }
  if (dep[x] < dep[y]) swap(x, y);
  if (x != y) Mx = max(Mx, getMx(1, 2, n, dfn[y] + 1, dfn[x], c));
  return Mx;
}
inline void down(int k) {
  int l = k << 1, r = k << 1 | 1;
  lazy[l] = min(lazy[l], lazy[k]);
  lazy[r] = min(lazy[r], lazy[k]);
}
void Getans(int num, int l, int r) {
  if (l == r) {
    ans[ed[tree[l]]] = lazy[num];
    return;
  }
  int mid = (l + r) >> 1;
  down(num);
  Getans(num << 1, l, mid);
  Getans(num << 1 | 1, mid + 1, r);
}
signed main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; ++i) {
    e[i].a = read();
    e[i].b = read();
    e[i].c = read();
    e[i].id = i;
  }
  for (int i = 1; i <= n; ++i) f[i] = i;
  sort(e + 1, e + m + 1);
  int num = 1;
  for (int i = 1; i <= m; ++i) {
    int fa = getf(e[i].a), fb = getf(e[i].b);
    if (fa == fb) continue;
    f[fa] = fb;
    vis[i] = true;
    ++num;
    insert(e[i].a, e[i].b, i);
    if (num == n) break;
  }
  dfs1(1, 1);
  dfs2(1, 1);
  build(1, 2, n);
  for (int i = 1; i <= m; ++i) {
    if (vis[i]) continue;
    ans[i] = solve(e[i].a, e[i].b, e[i].c);
  }
  Getans(1, 2, n);
  for (int i = 1; i <= m; ++i) Ans[e[i].id] = ans[i];
  for (int i = 1; i <= m; ++i) {
    if (Ans[i] == 2e9)
      printf("-1 ");
    else
      printf("%d ", Ans[i] - 1);
  }
  puts("");
  return 0;
}
