#include <bits/stdc++.h>
using std::max;
using std::sort;
using std::swap;
struct edge {
  int u, v, w, id;
} E[200010];
inline bool operator<(const edge &A, const edge &B) { return A.w < B.w; }
struct adj {
  int to, val, nxt;
} e[200010 << 1];
int n, m, hd[200010], cnt = 1, ans[200010];
inline void addedge(int x, int y, int z) {
  e[++cnt] = (adj){y, z, hd[x]};
  hd[x] = cnt;
}
int fa[200010];
int getfa(int x) { return (fa[x] == x) ? x : (fa[x] = getfa(fa[x])); }
inline void kruskal() {
  sort(E + 1, E + m + 1);
  memset(hd, 0, sizeof(hd));
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int u = E[i].u, v = E[i].v, w = E[i].w;
    if (getfa(u) != getfa(v)) {
      fa[getfa(u)] = getfa(v);
      addedge(u, v, w);
      addedge(v, u, w);
      E[i].w = 0x3f3f3f3f;
    }
  }
  sort(E + 1, E + m + 1);
}
int f[200010][18], dep[200010], mx[200010][18];
void dfs(int x) {
  for (int i = 1; i <= 17; ++i)
    f[x][i] = f[f[x][i - 1]][i - 1],
    mx[x][i] = max(mx[x][i - 1], mx[f[x][i - 1]][i - 1]);
  for (int i = hd[x]; i; i = e[i].nxt) {
    int y = e[i].to;
    if (y == f[x][0]) continue;
    f[y][0] = x;
    mx[y][0] = e[i].val;
    dep[y] = dep[x] + 1;
    dfs(y);
  }
}
inline int getmax(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int ret = 0;
  for (int i = 17; i >= 0; --i)
    if (dep[f[x][i]] >= dep[y]) ret = max(ret, mx[x][i]), x = f[x][i];
  if (x == y) return ret;
  for (int i = 17; i >= 0; --i) {
    if (f[x][i] != f[y][i]) {
      ret = max(ret, max(mx[x][i], mx[y][i]));
      x = f[x][i];
      y = f[y][i];
    }
  }
  ret = max(ret, max(mx[x][0], mx[y][0]));
  return ret;
}
inline void solve_not_tree_edge() {
  for (int i = 1; i <= m; ++i) {
    if (E[i].w == 0x3f3f3f3f) break;
    int u = E[i].u, v = E[i].v, w = E[i].w;
    ans[E[i].id] = getmax(u, v) - 1;
  }
}
int mn[200010];
inline void cover_the_path() {
  memset(mn, 0x3f, sizeof(mn));
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    if (E[i].w == 0x3f3f3f3f) break;
    int u = getfa(E[i].u), v = getfa(E[i].v), w = E[i].w;
    while (u != v) {
      if (dep[u] < dep[v]) swap(u, v);
      mn[u] = w;
      fa[getfa(u)] = getfa(f[u][0]);
      u = getfa(f[u][0]);
    }
  }
}
inline void solve_is_tree_edge() {
  cover_the_path();
  for (int i = m - n + 2; i <= m; ++i) {
    int u = E[i].u, v = E[i].v;
    if (dep[u] < dep[v]) swap(u, v);
    int w = mx[u][0];
    if (mn[u] == 0x3f3f3f3f)
      ans[E[i].id] = -1;
    else
      ans[E[i].id] = mn[u] - 1;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    E[i] = (edge){u, v, w, i};
  }
  kruskal();
  dep[1] = 1;
  dfs(1);
  solve_not_tree_edge();
  solve_is_tree_edge();
  for (int i = 1; i <= m; ++i) printf("%d ", ans[i]);
  return 0;
}
