#include <bits/stdc++.h>
using namespace std;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const long long LINF = 1e18 + 100;
int n, m;
int efrom[200100], eto[200100], ew[200100], ans[200100];
bool in_mst[200100];
int eind[200100];
int dpar[200100];
vector<int> adj[200100];
int h[200100], lpar[25][200100], lmx[25][200100];
int epar[200100];
vector<int> qr_add[200100], qr_ers[200100];
int chain[200100];
bool isbig[200100];
int subtree[200100];
vector<int> dfs_ord;
int getroot(int x) {
  return dpar[x] == x ? dpar[x] : dpar[x] = getroot(dpar[x]);
}
bool merge(int x, int y) {
  x = getroot(x);
  y = getroot(y);
  if (x == y) return false;
  dpar[x] = y;
  return true;
}
void dfs1(int v) {
  dfs_ord.push_back(v);
  pair<int, int> p = make_pair(-1, -1);
  subtree[v] = 1;
  for (int e : adj[v]) {
    int u = efrom[e] ^ eto[e] ^ v;
    if (u == lpar[0][v]) continue;
    lpar[0][u] = v;
    h[u] = h[v] + 1;
    lmx[0][u] = ew[e];
    epar[u] = e;
    dfs1(u);
    subtree[v] += subtree[u];
    p = max(p, {subtree[u], u});
  }
  if (p.second >= 0) isbig[p.second] = true;
}
pair<int, int> getlca(int u, int v) {
  if (h[u] > h[v]) swap(u, v);
  int res = 0;
  for (int i = (0); i < (25); i++) {
    if ((h[v] - h[u]) >> i & 1) {
      res = max(res, lmx[i][v]);
      v = lpar[i][v];
    }
  }
  if (u == v) return {u, res};
  for (int i = 25 - 1; i >= 0; i--) {
    if (lpar[i][v] != lpar[i][u]) {
      res = max({res, lmx[i][v], lmx[i][u]});
      u = lpar[i][u];
      v = lpar[i][v];
    }
  }
  res = max({res, lmx[0][v], lmx[0][u]});
  return {lpar[0][u], res};
}
void process(int up, int dn, int w) {
  if (chain[up] == chain[dn]) {
    qr_add[dn].push_back(w);
    qr_ers[up].push_back(w);
    return;
  }
  qr_add[dn].push_back(w);
  qr_ers[chain[dn]].push_back(w);
  ans[epar[chain[dn]]] = min(ans[epar[chain[dn]]], w - 1);
  process(up, lpar[0][chain[dn]], w);
}
multiset<int> ms[200100];
void dfs2(int v) {
  for (int e : adj[v]) {
    int u = efrom[e] ^ eto[e] ^ v;
    if (u == lpar[0][v]) continue;
    dfs2(u);
    if (isbig[u]) ms[u].swap(ms[v]);
  }
  for (int u : qr_add[v]) ms[v].insert(u);
  for (int u : qr_ers[v]) ms[v].erase(ms[v].find(u));
  if (!ms[v].empty()) ans[epar[v]] = *ms[v].begin() - 1;
}
int32_t main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (int i = (1); i < (m + 1); i++) cin >> efrom[i] >> eto[i] >> ew[i];
  iota(eind + 1, eind + m + 1, 1);
  sort(eind + 1, eind + m + 1, [&](int x, int y) { return ew[x] < ew[y]; });
  iota(dpar, dpar + n + 10, 0);
  for (int j = (1); j < (m + 1); j++) {
    int i = eind[j];
    if (!merge(efrom[i], eto[i])) continue;
    in_mst[i] = true;
    adj[efrom[i]].push_back(i);
    adj[eto[i]].push_back(i);
  }
  dfs1(1);
  for (int i = (1); i < (25); i++) {
    for (int j = (1); j < (n + 1); j++) {
      lpar[i][j] = lpar[i - 1][lpar[i - 1][j]];
      lmx[i][j] = max(lmx[i - 1][j], lmx[i - 1][lpar[i - 1][j]]);
    }
  }
  for (int u : dfs_ord) {
    if (isbig[u])
      chain[u] = chain[lpar[0][u]];
    else
      chain[u] = u;
  }
  fill(ans, ans + m + 10, INF);
  for (int i = (1); i < (m + 1); i++) {
    if (!in_mst[i]) {
      pair<int, int> p = getlca(efrom[i], eto[i]);
      ans[i] = p.second - 1;
      if (p.first != efrom[i] && p.first != eto[i]) {
        process(p.first, efrom[i], ew[i]);
        process(p.first, eto[i], ew[i]);
      } else {
        int dn = efrom[i];
        if (dn == p.first) dn = eto[i];
        process(p.first, dn, ew[i]);
      }
    }
  }
  dfs2(1);
  for (int i = (1); i < (m + 1); i++)
    cout << (ans[i] == INF ? -1 : ans[i]) << ' ';
  cout << '\n';
  return 0;
}
