#include <bits/stdc++.h>
using namespace std;
const int N = 202020, inf = 1e9 + 7;
int n, m;
int pre[N], ans[N];
bool in[N];
pair<pair<int, int>, int> E[N];
pair<pair<int, int>, pair<int, int> > e[N];
vector<pair<int, pair<int, int> > > g[N];
int find(int x) { return x == pre[x] ? x : (pre[x] = find(pre[x])); }
bool join(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return 0;
  pre[x] = y;
  return 1;
}
struct Seg {
  static const int N = ::N << 2;
  int mi[N], ma[N];
  void build() { fill_n(mi, N, inf); }
  void upd(int p, int c, int l, int r, int rt) {
    ma[rt] = max(ma[rt], c);
    if (l == r) return;
    int mid = l + r >> 1;
    if (p <= mid)
      upd(p, c, l, mid, rt << 1);
    else
      upd(p, c, mid + 1, r, rt << 1 | 1);
  }
  int qry(int L, int R, int l, int r, int rt) {
    if (L <= l && r <= R) return ma[rt];
    int mid = l + r >> 1, ans = 0;
    if (L <= mid) ans = max(ans, qry(L, R, l, mid, rt << 1));
    if (R > mid) ans = max(ans, qry(L, R, mid + 1, r, rt << 1 | 1));
    return ans;
  }
  void upd(int L, int R, int c, int l, int r, int rt) {
    if (L <= l && r <= R) {
      mi[rt] = min(mi[rt], c);
      return;
    }
    int mid = l + r >> 1;
    if (L <= mid) upd(L, R, c, l, mid, rt << 1);
    if (R > mid) upd(L, R, c, mid + 1, r, rt << 1 | 1);
  }
  int qry(int p, int l, int r, int rt) {
    if (l == r) return mi[rt];
    int mid = l + r >> 1, ans = mi[rt];
    if (p <= mid)
      ans = min(ans, qry(p, l, mid, rt << 1));
    else
      ans = min(ans, qry(p, mid + 1, r, rt << 1 | 1));
    return ans;
  }
} seg;
struct HeavyChain {
  static const int N = ::N;
  int sz[N], wson[N], top[N], dep[N], id[N], _, par[N], who[N], val[N],
      belong[N];
  void dfs(int c, int fa) {
    sz[c] = 1;
    par[c] = fa;
    dep[c] = dep[fa] + 1;
    int &s = wson[c] = top[c] = 0;
    for (auto _ : g[c])
      if (_.first != fa) {
        int t = _.first;
        val[t] = _.second.first;
        belong[_.second.second] = t;
        dfs(t, c);
        sz[c] += sz[t];
        if (sz[t] >= sz[s]) s = t;
      }
  }
  void dfs2(int c, int fa) {
    id[c] = ++_;
    who[_] = c;
    int s = wson[c];
    if (!top[c]) top[c] = c;
    if (s) top[s] = top[c], dfs2(s, c);
    for (auto t : g[c])
      if (t.first != fa && t.first != s) dfs2(t.first, c);
  }
  int Query(int a, int b, int o = 0) {
    int fa = top[a], fb = top[b], res = 0;
    while (fa != fb) {
      if (dep[fa] < dep[fb]) swap(a, b), swap(fa, fb);
      if (o)
        seg.upd(id[fa], id[a], o, 1, n, 1);
      else
        res = max(res, seg.qry(id[fa], id[a], 1, n, 1));
      a = par[fa];
      fa = top[a];
    }
    if (dep[a] < dep[b]) swap(a, b);
    if (o)
      seg.upd(id[b] + 1, id[a], o, 1, n, 1);
    else
      res = max(res, seg.qry(id[b] + 1, id[a], 1, n, 1));
    return res;
  }
  void Build() {
    dfs(1, 0);
    dfs2(1, 0);
    seg.build();
    for (int i = (2); i < (n + 1); i++) seg.upd(id[i], val[i], 1, n, 1);
  }
} hc;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> n >> m;
  fill_n(ans + 1, n, inf);
  for (int i = (1); i < (m + 1); i++)
    cin >> e[i].second.first >> e[i].second.second >> e[i].first.first,
        e[i].first.second = i, E[i] = make_pair(e[i].second, e[i].first.first);
  sort(e + 1, e + 1 + m);
  for (int i = (1); i < (n + 1); i++) pre[i] = i;
  for (int i = (1); i < (m + 1); i++)
    if (join(e[i].second.first, e[i].second.second)) {
      in[e[i].first.second] = 1;
      int u = e[i].second.first, v = e[i].second.second;
      g[u].push_back(make_pair(v, e[i].first));
      g[v].push_back(make_pair(u, e[i].first));
    }
  hc.Build();
  for (int i = (1); i < (m + 1); i++)
    if (!in[i]) {
      int t = hc.Query(E[i].first.first, E[i].first.second);
      ans[i] = t - 1;
    }
  for (int i = (1); i < (m + 1); i++)
    if (!in[i]) hc.Query(E[i].first.first, E[i].first.second, E[i].second);
  for (int i = (1); i < (m + 1); i++)
    if (in[i]) {
      int t = hc.belong[i];
      t = hc.id[t];
      int c = seg.qry(t, 1, n, 1);
      ans[i] = (c == inf) ? inf : c - 1;
    }
  for (int i = (1); i < (m + 1); i++)
    cout << (ans[i] == inf ? -1 : ans[i]) << " ";
  cout << "\n";
  return 0;
}
