#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5, mod = 1e9 + 7, inf = mod + 10ll;
int n, m, a[maxn], ans[maxn], tme, st[maxn], ft[maxn], blp[maxn], h[maxn],
    par[20][maxn], mx[20][maxn], pw2[20], p[maxn], sz[maxn], ip[maxn], mn[maxn],
    seg[maxn * 4ll], lzy[maxn * 4ll];
vector<pair<int, int>> v1[maxn], v2[maxn];
vector<pair<int, pair<int, int>>> adj[maxn], g[maxn];
vector<pair<pair<int, int>, pair<int, int>>> vec;
set<pair<int, int>> s[maxn];
bool mrk[maxn];
int get_par(int u) {
  if (p[u] == 0) return u;
  return p[u] = get_par(p[u]);
}
bool merge(int u, int v) {
  u = get_par(u);
  v = get_par(v);
  if (u == v) return 0;
  if (sz[u] < sz[v]) swap(u, v);
  p[v] = u;
  sz[u] += sz[v];
  return 1;
}
void dfs12(int u) {
  for (int i = 1; i < 20; i++) {
    int v = par[i - 1][u];
    if ((v == 0) || (par[i - 1][v] == 0)) break;
    par[i][u] = par[i - 1][v];
    mx[i][u] = max(mx[i - 1][u], mx[i - 1][v]);
  }
  for (auto j : g[u]) {
    int v = j.first, w = j.second.first;
    if (j.first == par[0][u]) continue;
    ip[v] = j.second.second;
    mx[0][v] = w;
    par[0][v] = u;
    h[v] = h[u] + 1;
    dfs12(v);
  }
}
void build_mst() {
  sort(vec.begin(), vec.end());
  for (auto i : vec) {
    int u = i.second.first, v = i.second.second, w = i.first.first;
    if (merge(u, v)) {
      g[u].push_back({v, {w, i.first.second}});
      g[v].push_back({u, {w, i.first.second}});
      mrk[i.first.second] = 1;
    }
  }
  dfs12(1);
}
int get_pr(int v, int d) {
  for (int i = 0; i < 20; i++) {
    if ((pw2[i] & d) == 0) continue;
    v = par[i][v];
  }
  return v;
}
int get_mx(int v, int d) {
  int MX = 0;
  for (int i = 0; i < 20; i++) {
    if ((pw2[i] & d) == 0) continue;
    MX = max(MX, mx[i][v]);
    v = par[i][v];
  }
  return MX;
}
int get_lca(int u, int v) {
  if (h[u] > h[v]) swap(u, v);
  if (h[u] != h[v]) v = get_pr(v, h[v] - h[u]);
  if (u == v) return u;
  for (int i = 19; i >= 0; i--) {
    if (par[i][v] == par[i][u]) continue;
    u = par[i][u];
    v = par[i][v];
  }
  return par[0][u];
}
void fix_tree(int u, int prr) {
  sz[u] = 1;
  pair<int, pair<int, int>> big = {-1, {-1, -1}};
  for (auto j : g[u]) {
    if (j.first == prr) continue;
    fix_tree(j.first, u);
    if (big.first == -1)
      big = j;
    else if (sz[big.first] < sz[j.first])
      big = j;
  }
  if (big.first == -1) {
    g[u].clear();
    return;
  }
  vector<pair<int, pair<int, int>>> g2;
  g2.push_back(big);
  for (auto j : g[u]) {
    if (j.first == prr) continue;
    if (j.first == big.first) continue;
    g2.push_back(j);
  }
  g[u] = g2;
}
void dfs(int u) {
  st[u] = tme;
  tme++;
  int big = 0;
  if (g[u].size()) big = g[u][0].first;
  for (auto j : g[u]) {
    if (j.first == big)
      blp[big] = blp[u];
    else
      blp[j.first] = j.first;
    dfs(j.first);
  }
  ft[u] = tme;
}
void shift(int v, int s, int e) {
  int x = lzy[v], lc = v << 1, rc = lc | 1;
  seg[lc] = min(seg[lc], x);
  seg[rc] = min(seg[rc], x);
  lzy[lc] = min(lzy[lc], x);
  lzy[rc] = min(lzy[rc], x);
}
int get(int l, int r, int v = 1, int s = 0, int e = n) {
  if ((l >= e) || (r <= s)) return inf;
  if ((l <= s) && (e <= r)) return seg[v];
  shift(v, s, e);
  int mid = (e + s) >> 1, lc = v << 1, rc = lc | 1;
  return min(get(l, r, lc, s, mid), get(l, r, rc, mid, e));
}
void fix(int l, int r, int x, int v = 1, int s = 0, int e = n) {
  if ((l >= e) || (r <= s)) return;
  if ((l <= s) && (e <= r)) {
    seg[v] = min(seg[v], x);
    lzy[v] = min(lzy[v], x);
    return;
  }
  int mid = (e + s) >> 1, lc = v << 1, rc = lc | 1;
  fix(l, r, x, lc, s, mid);
  fix(l, r, x, rc, mid, e);
  seg[v] = min(seg[lc], seg[rc]);
}
void fix_path(int lca, int v, int w) {
  if (v == lca) return;
  if (blp[v] == v) {
    ans[ip[v]] = min(ans[ip[v]], w);
    fix_path(lca, par[0][v], w);
    return;
  }
  if (h[lca] <= h[blp[v]]) {
    fix(st[blp[v]] + 1, st[v] + 1, w);
    fix_path(lca, blp[v], w);
    return;
  }
  fix(st[lca] + 1, st[v] + 1, w);
  return;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  pw2[0] = 1;
  for (int i = 1; i < 20; i++) pw2[i] = (pw2[i - 1] * 2ll);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].push_back({v, {w, i}});
    adj[v].push_back({u, {w, i}});
    vec.push_back({{w, i}, {u, v}});
  }
  build_mst();
  for (auto j : vec) {
    int u = j.second.first, v = j.second.second, w = j.first.first,
        i = j.first.second;
    if (h[u] > h[v]) swap(u, v);
    if (par[0][v] == u) {
      ans[i] = inf;
      continue;
    }
    int lca = get_lca(u, v);
    v1[u].push_back({w, v});
    v1[v].push_back({w, u});
    v2[lca].push_back({w, v});
    v2[lca].push_back({w, u});
    ans[i] = max(get_mx(u, h[u] - h[lca]), get_mx(v, h[v] - h[lca])) - 1;
  }
  tme = 0;
  fix_tree(1, 1);
  blp[1] = 1;
  dfs(1);
  fill(seg, seg + maxn * 4ll, inf);
  fill(lzy, lzy + maxn * 4ll, inf);
  for (auto j : vec) {
    int u = j.second.first, v = j.second.second, w = j.first.first - 1ll,
        i = j.first.second;
    if (h[u] > h[v]) swap(u, v);
    if (par[0][v] == u) continue;
    int lca = get_lca(u, v);
    fix_path(lca, u, w);
    fix_path(lca, v, w);
  }
  for (int i = 2; i <= n; i++)
    ans[ip[i]] = min(get(st[i], st[i] + 1ll), ans[ip[i]]);
  for (int i = 0; i < m; i++) {
    if (ans[i] > mod) ans[i] = -1;
    cout << ans[i] << ' ';
  }
}
