#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long num = 0, neg = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num * neg;
}
const int INF = 0x3f3f3f3f;
struct node {
  int x, y, c, id;
} s[200010];
bool cmp1(node x, node y) { return x.c < y.c; }
bool book[200010];
int n, m, dep[200010], fa[200010], size[200010], son[200010], p[200010];
int id[200010], tmp[200010], tot, top[200010], ans[200010];
int bel[200010], to[400010], head[200010], nxt[400010], val[400010], cnt;
int a[200010 << 2], b[200010 << 2], tag[200010 << 2];
inline int find(int x) { return bel[x] == x ? x : bel[x] = find(bel[x]); }
inline void insert(int x, int y, int z) {
  to[++cnt] = y, nxt[cnt] = head[x], head[x] = cnt, val[cnt] = z;
}
inline void kruskal() {
  sort(s + 1, s + m + 1, cmp1);
  for (int i = 1; i <= n; i++) bel[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = find(s[i].x), v = find(s[i].y);
    if (u != v)
      book[i] = 1, bel[u] = v, insert(s[i].x, s[i].y, s[i].c),
      insert(s[i].y, s[i].x, s[i].c);
  }
  return;
}
inline void dfs1(int u, int father) {
  dep[u] = dep[father] + 1, fa[u] = father, size[u] = 1;
  int maxson = 0;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == father) continue;
    p[v] = val[i];
    dfs1(v, u);
    size[u] += size[v];
    if (size[v] > size[maxson]) maxson = v;
  }
  son[u] = maxson;
  return;
}
inline void dfs2(int u, int topf) {
  id[u] = ++tot, tmp[tot] = p[u], top[u] = topf;
  if (!son[u]) return;
  dfs2(son[u], topf);
  for (int i = head[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == fa[u] || v == son[u]) continue;
    dfs2(v, v);
  }
  return;
}
inline void push_up(int x) {
  a[x] = max(a[x << 1], a[x << 1 | 1]), b[x] = min(b[x << 1], b[x << 1 | 1]);
}
inline void build(int l, int r, int x) {
  if (l == r) {
    a[x] = tmp[l], b[x] = INF;
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
  push_up(x);
}
inline void push_down(int x) {
  if (tag[x]) {
    b[x << 1] = b[x << 1 | 1] = tag[x << 1] = tag[x << 1 | 1] = tag[x];
    tag[x] = 0;
  }
  return;
}
inline void update(int ul, int ur, int l, int r, int x, int k) {
  if (ul <= l && ur >= r) {
    b[x] = tag[x] = k;
    return;
  }
  int mid = l + r >> 1;
  push_down(x);
  if (ul <= mid) update(ul, ur, l, mid, x << 1, k);
  if (ur > mid) update(ul, ur, mid + 1, r, x << 1 | 1, k);
  return;
}
inline int querya(int ql, int qr, int l, int r, int x) {
  if (ql <= l && qr >= r) return a[x];
  int mid = l + r >> 1, res = 0;
  push_down(x);
  if (ql <= mid) res = max(res, querya(ql, qr, l, mid, x << 1));
  if (qr > mid) res = max(res, querya(ql, qr, mid + 1, r, x << 1 | 1));
  return res;
}
inline int queryb(int q, int l, int r, int x) {
  if (l == r && q == l) return b[x];
  int mid = l + r >> 1;
  push_down(x);
  if (q <= mid)
    return queryb(q, l, mid, x << 1);
  else
    return queryb(q, mid + 1, r, x << 1 | 1);
}
inline void updrange(int x, int y, int k) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    update(id[top[x]], id[x], 1, n, 1, k);
    x = fa[top[x]];
  }
  if (dep[x] > dep[y]) swap(x, y);
  if (x == y) return;
  update(id[x] + 1, id[y], 1, n, 1, k);
}
inline 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, querya(id[top[x]], id[x], 1, n, 1));
    x = fa[top[x]];
  }
  if (dep[x] > dep[y]) swap(x, y);
  if (x == y) return res;
  res = max(res, querya(id[x] + 1, id[y], 1, n, 1));
  return res;
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++)
    s[i].x = read(), s[i].y = read(), s[i].c = read(), s[i].id = i;
  kruskal();
  dfs1(1, 1), dfs2(1, 1), build(1, n, 1);
  for (int i = m; i >= 1; i--) {
    if (!book[i]) {
      updrange(s[i].x, s[i].y, s[i].c);
      ans[s[i].id] = qrange(s[i].x, s[i].y) - 1;
    } else {
      int u = (dep[s[i].x] > dep[s[i].y] ? s[i].x : s[i].y);
      ans[s[i].id] = queryb(id[u], 1, n, 1) - 1;
    }
  }
  for (int i = 1; i <= m; i++) {
    if (ans[i] == INF - 1)
      printf("-1 ");
    else
      printf("%d ", ans[i]);
  }
  return 0;
}
