#include <bits/stdc++.h>
using namespace std;
int n, m;
const int maxn = 2e5 + 233;
int up[maxn][21];
int mx[maxn][21];
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  vector<tuple<int, int, int> > edge;
  for (int i = 1; i <= m; i += 1) {
    int u, v, w;
    cin >> u >> v >> w;
    edge.emplace_back(u, v, w);
  }
  vector<int> ord(m);
  iota(ord.begin(), ord.end(), 0);
  vector<int> f(n + 1);
  iota(f.begin() + 1, f.end(), 1);
  function<int(int)> find = [&](int x) {
    return f[x] == x ? x : f[x] = find(f[x]);
  };
  sort(ord.begin(), ord.end(),
       [&](int x, int y) { return get<2>(edge[x]) < get<2>(edge[y]); });
  vector<int> used(m);
  for (int o = 0; o < m; o += 1) {
    int i = ord[o];
    auto [u, v, w] = edge[i];
    int fx = find(u);
    int fy = find(v);
    if (fx == fy) continue;
    f[fy] = fx;
    used[i] = 1;
  }
  vector<pair<int, int> > G[n + 1];
  vector<int> d(n + 1);
  for (int i = 0; i < m; i += 1) {
    auto [u, v, w] = edge[i];
    if (used[i]) {
      G[u].emplace_back(v, i);
      G[v].emplace_back(u, i);
    }
  }
  vector<int> edge_up(n + 1);
  function<void(int, int, int)> dfs = [&](int u, int fa, int dis) {
    up[u][0] = fa;
    mx[u][0] = dis;
    d[u] = d[fa] + 1;
    for (int i = 1; i <= 20; i += 1) {
      up[u][i] = up[up[u][i - 1]][i - 1];
      mx[u][i] = max(mx[u][i - 1], mx[up[u][i - 1]][i - 1]);
    }
    for (auto [v, i] : G[u]) {
      if (v == fa) continue;
      edge_up[v] = i;
      dfs(v, u, get<2>(edge[i]));
    }
  };
  auto lca = [&](int x, int y) {
    if (d[x] > d[y]) swap(x, y);
    int val = 0;
    for (int i = 20; i >= 0; i -= 1) {
      if (d[up[y][i]] >= d[x]) {
        val = max(val, mx[y][i]);
        y = up[y][i];
      }
    }
    if (x == y) return make_pair(x, val);
    for (int i = 20; i >= 0; i -= 1) {
      if (up[x][i] != up[y][i]) {
        val = max(val, mx[x][i]);
        val = max(val, mx[y][i]);
        x = up[x][i];
        y = up[y][i];
      }
    }
    val = max(val, mx[x][0]);
    val = max(val, mx[y][0]);
    return make_pair(up[x][0], val);
  };
  dfs(1, 0, 0);
  vector<int> res(m, 2e9);
  vector<tuple<int, int, int> > ask;
  for (int i = 0; i < m; i += 1) {
    auto [u, v, w] = edge[i];
    if (d[u] > d[v]) swap(u, v);
    if (!used[i]) {
      auto [lc, val] = lca(u, v);
      res[i] = val - 1;
      if (u == lc) {
        ask.emplace_back(v, lc, w - 1);
      } else {
        ask.emplace_back(v, lc, w - 1);
        ask.emplace_back(u, lc, w - 1);
      }
    }
  }
  iota(f.begin() + 1, f.end(), 1);
  sort(ask.begin(), ask.end(),
       [&](tuple<int, int, int> a, tuple<int, int, int> b) {
         return get<2>(a) < get<2>(b);
       });
  for (auto [u, v, w] : ask) {
    int go = find(u);
    while (d[go] > d[v]) {
      res[edge_up[go]] = min(res[edge_up[go]], w);
      f[go] = up[go][0];
      go = up[go][0];
    }
  }
  for (int i = 0; i < m; i += 1) {
    cout << (res[i] == 2e9 ? -1 : res[i]) << " \n"[i == m - 1];
  }
  return 0;
}
