#include <bits/stdc++.h>
using namespace std;
const int O = 1e6 + 5;
int n, m, lab[O], h[O], f[21][O], w[21][O], is[O], d[O], ans[O];
struct AoN {
  int u, v, w, i;
};
vector<AoN> e, adj[O];
void Minimize(int &x, int y) {
  if (x > y || x == -1) x = y;
}
void Maximize(int &x, int y) {
  if (x < y) x = y;
}
bool how_sort(AoN x, AoN y) { return x.w < y.w; }
int Find(int u) { return lab[u] == u ? u : lab[u] = Find(lab[u]); }
void dfs(int u, int par) {
  for (AoN v : adj[u]) {
    if (v.u == par) continue;
    f[0][v.u] = u;
    w[0][v.u] = v.w;
    is[v.u] = v.i;
    h[v.u] = h[u] + 1;
    dfs(v.u, u);
  }
}
int Lca(int u, int v) {
  if (h[u] < h[v]) swap(u, v);
  for (int i = 20; i >= 0; --i)
    if (h[u] - (1 << i) >= h[v]) u = f[i][u];
  if (u == v) return u;
  for (int i = 20; i >= 0; --i) {
    if (f[i][u] != f[i][v]) {
      u = f[i][u];
      v = f[i][v];
    }
  }
  return f[0][u];
}
int maxLca(int u, int v) {
  if (h[u] < h[v]) swap(u, v);
  int res = 0;
  for (int i = 20; i >= 0; --i) {
    if (h[u] - (1 << i) >= h[v]) {
      Maximize(res, w[i][u]);
      u = f[i][u];
    }
  }
  if (u == v) return res;
  for (int i = 20; i >= 0; --i) {
    if (f[i][u] != f[i][v]) {
      Maximize(res, max(w[i][u], w[i][v]));
      u = f[i][u];
      v = f[i][v];
    }
  }
  Maximize(res, max(w[0][u], w[0][v]));
  return res;
}
int sol(int u, int v, int weight) {
  if (h[u] <= h[v]) return v;
  Minimize(ans[is[u]], weight);
  return lab[u] = sol(lab[f[0][u]], v, weight);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    e.push_back({u, v, w, i});
  }
  memset(f, -1, sizeof(f));
  for (int i = 1; i <= n; ++i) lab[i] = i;
  h[1] = 1;
  sort(e.begin(), e.end(), how_sort);
  for (int i = 0; i < m; ++i) {
    int u = Find(e[i].u);
    int v = Find(e[i].v);
    if (u == v) continue;
    lab[u] = v;
    adj[e[i].u].push_back({e[i].v, e[i].v, e[i].w, e[i].i});
    adj[e[i].v].push_back({e[i].u, e[i].u, e[i].w, e[i].i});
    d[e[i].i] = 1;
  }
  dfs(1, 0);
  memset(ans, -1, sizeof(ans));
  for (int i = 1; i <= 20; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (f[i - 1][j] != -1) {
        f[i][j] = f[i - 1][f[i - 1][j]];
        w[i][j] = max(w[i - 1][j], w[i - 1][f[i - 1][j]]);
      }
    }
  }
  for (int i = 1; i <= n; ++i) lab[i] = i;
  for (AoN x : e) {
    if (d[x.i] == 0) {
      ans[x.i] = maxLca(x.u, x.v) - 1;
      int uv = Lca(x.u, x.v);
      sol(x.u, uv, x.w - 1);
      sol(x.v, uv, x.w - 1);
    }
  }
  for (int i = 1; i <= m; ++i) cout << ans[i] << " ";
}
