#include <bits/stdc++.h>
using namespace std;
template <typename _Tp>
inline _Tp read(_Tp& x) {
  char c11 = getchar(), ob = 0;
  x = 0;
  while (c11 ^ '-' && !isdigit(c11)) c11 = getchar();
  if (c11 == '-') c11 = getchar(), ob = 1;
  while (isdigit(c11)) x = x * 10 + c11 - '0', c11 = getchar();
  if (ob) x = -x;
  return x;
}
void file(void) {}
long long head[200005], cnt, n, m, f[200005], ans[200005], fa[200005][21],
    minx[200005][21], dep[200005], maxx[200005][21], gg[200005], val[200005];
struct EDGE {
  long long to, v, from, cs, id;
} g[200005];
struct edge {
  long long to, v, next, id;
} e[400005];
bool cmp(const EDGE& a, const EDGE& b) { return a.v < b.v; }
long long find(long long x) { return x == f[x] ? x : f[x] = find(f[x]); }
void add(long long a, long long b, long long c, long long d) {
  e[cnt].to = b;
  e[cnt].v = c;
  e[cnt].next = head[a];
  e[cnt].id = d;
  head[a] = cnt++;
}
void dfs(long long u, long long pre) {
  for (long long i = 1; i <= 20; ++i) {
    fa[u][i] = fa[fa[u][i - 1]][i - 1];
    minx[u][i] = min(minx[u][i - 1], minx[fa[u][i - 1]][i - 1]);
    maxx[u][i] = max(maxx[u][i - 1], maxx[fa[u][i - 1]][i - 1]);
  }
  for (long long i = head[u]; i; i = e[i].next) {
    if (e[i].to == pre) continue;
    gg[e[i].to] = e[i].id;
    fa[e[i].to][0] = u;
    maxx[e[i].to][0] = e[i].v;
    dep[e[i].to] = dep[u] + 1;
    dfs(e[i].to, u);
  }
}
long long mimi(long long x, long long y) {
  if (dep[x] < dep[y]) swap(x, y);
  long long res = 0;
  for (long long i = 20; i >= 0; --i) {
    if (dep[fa[x][i]] >= dep[y]) res = max(res, maxx[x][i]), x = fa[x][i];
  }
  if (x == y) return res;
  for (long long i = 20; i >= 0; --i) {
    if (fa[x][i] != fa[y][i])
      res = max(res, maxx[x][i]), res = max(res, maxx[y][i]), x = fa[x][i],
      y = fa[y][i];
  }
  res = max(res, maxx[x][0]), res = max(res, maxx[y][0]);
  return res;
}
void getmin(long long x, long long y, long long z) {
  if (dep[x] < dep[y]) swap(x, y);
  for (long long i = 20; i >= 0; --i) {
    if (dep[fa[x][i]] >= dep[y]) minx[x][i] = min(minx[x][i], z), x = fa[x][i];
  }
  if (x == y) return;
  for (long long i = 20; i >= 0; --i) {
    if (fa[x][i] != fa[y][i])
      minx[x][i] = min(minx[x][i], z), minx[y][i] = min(minx[y][i], z),
      x = fa[x][i], y = fa[y][i];
  }
  minx[x][0] = min(minx[x][0], z);
  minx[y][0] = min(minx[y][0], z);
}
void dfs2(long long u, long long pre) {
  for (long long i = head[u]; i; i = e[i].next) {
    if (e[i].to == pre) continue;
    dfs2(e[i].to, u);
  }
  for (long long i = 20; i >= 1; --i) {
    minx[u][i - 1] = min(minx[u][i - 1], minx[u][i]);
    minx[fa[u][i - 1]][i - 1] = min(minx[fa[u][i - 1]][i - 1], minx[u][i]);
  }
  ans[gg[u]] = minx[u][0];
}
signed main() {
  cnt = 1;
  read(n);
  read(m);
  for (long long i = 1; i <= n; ++i) f[i] = i;
  long long a, b, c;
  memset(minx, 0x3f3f3f, sizeof(minx));
  memset(ans, 0x3f3f3f, sizeof(ans));
  for (long long i = 1; i <= m; ++i) {
    read(g[i].from);
    read(g[i].to);
    read(g[i].v);
    g[i].id = i;
    val[i] = g[i].v;
  }
  sort(g + 1, g + m + 1, cmp);
  for (long long i = 1; i <= m; ++i) {
    a = find(g[i].from);
    b = find(g[i].to);
    if (a != b) {
      f[b] = a;
      g[i].cs = 1;
      add(g[i].from, g[i].to, g[i].v, g[i].id);
      add(g[i].to, g[i].from, g[i].v, g[i].id);
    }
  }
  dfs(1, 0);
  for (long long i = 1; i <= m; ++i) {
    if (!g[i].cs) {
      ans[g[i].id] = mimi(g[i].from, g[i].to) - 1;
      getmin(g[i].from, g[i].to, g[i].v - 1);
    }
  }
  dfs2(1, 0);
  for (long long i = 1; i <= m; ++i) {
    if (ans[i] > 1e9)
      printf("-1 ");
    else
      printf("%lld ", ans[i]);
  }
}
