#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  char ch = getchar(), w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * w;
}
void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(long long x) {
  write(x);
  puts("");
}
const int N = 210000, M = 450000;
int n, m;
int siz[N], g[N][22], f[N][22], son[N], id[N], num, top[N], d[N];
int ls[N << 2], rs[N << 2], val[N << 2], tag[N << 2];
const int inf = 0x3f3f3f3f;
void build(int o, int l, int r) {
  ls[o] = l, rs[o] = r;
  val[o] = inf;
  tag[o] = inf;
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (l <= mid) build((o << 1), l, mid);
  if (r > mid) build((o << 1 | 1), mid + 1, r);
}
void change(int o, int l, int r, int v) {
  if (l <= ls[o] && r >= rs[o]) {
    val[o] = min(val[o], v);
    tag[o] = min(tag[o], v);
    return;
  }
  int mid = (ls[o] + rs[o]) >> 1;
  if (l <= mid) change((o << 1), l, r, v);
  if (r > mid) change((o << 1 | 1), l, r, v);
}
int ask(int o, int x) {
  if (ls[o] == rs[o]) return val[o];
  int mid = (ls[o] + rs[o]) >> 1;
  if (x <= mid)
    return min(tag[o], ask((o << 1), x));
  else
    return min(tag[o], ask((o << 1 | 1), x));
}
void uprange(int x, int y, int z) {
  while (top[x] != top[y]) {
    if (d[top[x]] > d[top[y]]) swap(x, y);
    change(1, id[top[y]], id[y], z);
    y = f[top[y]][0];
  }
  if (d[x] > d[y]) swap(x, y);
  if (id[x] ^ id[y]) change(1, id[x] + 1, id[y], z);
}
struct G {
  struct Edge {
    int u, v, w, nxt, i;
    bool operator<(const Edge rhs) const { return w < rhs.w; }
  } e[M];
  int head[N], en, a[N];
  void add(int x, int y, int z) {
    e[++en].u = x, e[en].v = y, e[en].w = z, e[en].nxt = head[x], head[x] = en;
  }
  void dfs1(int x, int F) {
    siz[x] = 1;
    d[x] = d[F] + 1;
    f[x][0] = F;
    for (int i = 1; i <= 20; ++i) {
      f[x][i] = f[f[x][i - 1]][i - 1];
      g[x][i] = max(g[x][i - 1], g[f[x][i - 1]][i - 1]);
    }
    for (int i = head[x]; i; i = e[i].nxt) {
      int y = e[i].v;
      if (y == F) continue;
      g[y][0] = e[i].w;
      dfs1(y, x);
      siz[x] += siz[y];
      if (siz[y] > siz[son[x]]) son[x] = y;
    }
  }
  void dfs2(int x, int F) {
    id[x] = ++num;
    top[x] = F;
    if (!son[x]) return;
    dfs2(son[x], F);
    for (int i = head[x]; i; i = e[i].nxt) {
      int y = e[i].v;
      if (f[x][0] == y || y == son[x]) continue;
      dfs2(y, y);
    }
  }
  void init() {
    dfs1(1, 1);
    dfs2(1, 1);
    build(1, 1, n);
  }
} g1, g2;
int askmax(int x, int y) {
  int res = -inf;
  if (d[x] > d[y]) swap(x, y);
  for (int i = 20; i >= 0; --i)
    if (d[f[y][i]] >= d[x]) res = max(res, g[y][i]), y = f[y][i];
  if (y == x) return res;
  for (int i = 20; i >= 0; --i)
    if (f[x][i] != f[y][i])
      res = max(res, max(g[x][i], g[y][i])), x = f[x][i], y = f[y][i];
  return max(res, max(g[x][0], g[y][0]));
}
int fa[N], used[M], ans[N];
int Find(int x) { return fa[x] == x ? x : fa[x] = Find(fa[x]); }
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; ++i) {
    int x = read(), y = read(), z = read();
    g1.e[i] = (G::Edge){x, y, z, 0, i};
  }
  sort(g1.e + 1, g1.e + 1 + m);
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int x = Find(g1.e[i].u), y = Find(g1.e[i].v);
    if (x ^ y) {
      fa[x] = y;
      g2.add(g1.e[i].u, g1.e[i].v, g1.e[i].w);
      g2.add(g1.e[i].v, g1.e[i].u, g1.e[i].w);
      used[i] = 1;
    }
  }
  g2.init();
  for (int i = 1; i <= m; ++i)
    if (!used[i])
      ans[g1.e[i].i] = askmax(g1.e[i].u, g1.e[i].v) - 1,
      uprange(g1.e[i].u, g1.e[i].v, g1.e[i].w);
  for (int i = 1; i <= m; ++i)
    if (used[i]) {
      int x;
      if (d[g1.e[i].u] > d[g1.e[i].v])
        x = g1.e[i].u;
      else
        x = g1.e[i].v;
      int t = ask(1, id[x]);
      if (t == inf) {
        ans[g1.e[i].i] = -1;
        continue;
      }
      ans[g1.e[i].i] = t - 1;
    }
  for (int i = 1; i <= m; ++i) printf("%d ", ans[i]);
  return 0;
}
