#include <bits/stdc++.h>
using namespace std;
int n, m, num = 0, mx = 0, f[200010], fa[200010][20], st[200010][20],
          ans[200010], val[200010], h[200010], flag[200010], d[200010];
struct node {
  int x, y, z, id;
} a[200010];
struct edge {
  int x, y, z, next;
} mp[200010 << 1];
inline char gc() {
  static char *S, *T, buf[1 << 16];
  if (T == S) {
    T = (S = buf) + fread(buf, 1, 1 << 16, stdin);
    if (T == S) return EOF;
  }
  return *S++;
}
inline int read() {
  int x = 0, f = 1;
  char ch = gc();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = gc();
  }
  while ('0' <= ch && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = gc();
  return x * f;
}
int find(int x) {
  if (f[x] != x) f[x] = find(f[x]);
  return f[x];
}
inline void ins(int x, int y, int z) {
  mp[++num].x = x;
  mp[num].y = y;
  mp[num].z = z;
  mp[num].next = h[x];
  h[x] = num;
  mp[++num].x = y;
  mp[num].y = x;
  mp[num].z = z;
  mp[num].next = h[y];
  h[y] = num;
}
inline bool cmp(node x, node y) { return x.z < y.z; }
inline void kruskal() {
  sort(a + 1, a + m + 1, cmp);
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = 1; i <= m; i++) {
    int x = find(a[i].x), y = find(a[i].y);
    if (x == y) continue;
    f[x] = y;
    flag[i] = 1;
    ins(a[i].x, a[i].y, a[i].z);
  }
}
void dfs(int x) {
  for (int i = 1; i <= 18; i++) {
    if (!fa[x][i - 1]) break;
    fa[x][i] = fa[fa[x][i - 1]][i - 1];
    st[x][i] = max(st[x][i - 1], st[fa[x][i - 1]][i - 1]);
  }
  for (int i = h[x]; i; i = mp[i].next) {
    int y = mp[i].y;
    if (y == fa[x][0]) continue;
    fa[y][0] = x;
    st[y][0] = mp[i].z;
    d[y] = d[x] + 1;
    dfs(y);
  }
}
inline int lca(int x, int y) {
  mx = 0;
  if (d[x] < d[y]) swap(x, y);
  int len = d[x] - d[y];
  for (int i = 0; i <= 18; i++) {
    if (len & (1 << i)) mx = max(mx, st[x][i]), x = fa[x][i];
  }
  if (x == y) return x;
  for (int i = 18; i >= 0; i--) {
    if (fa[x][i] != fa[y][i]) {
      mx = max(mx, max(st[x][i], st[y][i]));
      x = fa[x][i], y = fa[y][i];
    }
  }
  mx = max(mx, max(st[x][0], st[y][0]));
  return fa[x][0];
}
inline void merge(int x, int y, int z) {
  x = find(x);
  while (d[x] > d[y]) {
    val[x] = z;
    x = f[x] = find(fa[x][0]);
  }
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; i++)
    a[i].x = read(), a[i].y = read(), a[i].z = read(), a[i].id = i;
  kruskal();
  dfs(1);
  for (int i = 1; i <= n; i++) f[i] = i, val[i] = 0x3f3f3f3f;
  for (int i = 1; i <= m; i++) {
    if (!flag[i]) {
      int x = a[i].x, y = a[i].y, z = lca(x, y);
      ans[a[i].id] = mx - 1;
      merge(x, z, a[i].z);
      merge(y, z, a[i].z);
    }
  }
  for (int i = 1; i <= m; i++) {
    if (flag[i]) {
      int x = a[i].x, y = a[i].y;
      if (d[x] < d[y]) x = y;
      ans[a[i].id] = val[x] - 1;
    }
  }
  for (int i = 1; i <= m; i++)
    printf("%d ", ans[i] == 0x3f3f3f3f - 1 ? -1 : ans[i]);
  return 0;
}
