#include <bits/stdc++.h>
using namespace std;
int fa[400010], n, m, mp[200010], tp[400010][20];
int mask[400010], num[400010], ans[400010];
int dep[400010];
struct edg {
  int to, num;
};
vector<edg> ne[400010];
int l, r, val, cnt, tar;
struct edge {
  int l, r, val, num;
  bool operator<(const edge &a) const { return val < a.val; }
};
edge sth[200010], ed[200010];
void ade(int l, int r, int val) {
  ne[l].push_back({r, val});
  ne[r].push_back({l, val});
}
int lf(int k) {
  int c = k, nw;
  while (fa[k] != k) k = fa[k];
  while (c != k) nw = c, c = fa[c], fa[nw] = k;
  return k;
}
int lca(int l, int r) {
  if (dep[l] > dep[r]) swap(l, r);
  if (dep[l] != dep[r]) {
    for (int i = 19; i >= 0; --i)
      if (dep[tp[r][i]] > dep[l]) r = tp[r][i];
    r = tp[r][0];
  }
  if (l == r) return l;
  for (int i = 19; i >= 0; --i)
    if (tp[l][i] != tp[r][i]) l = tp[l][i], r = tp[r][i];
  return tp[l][0];
}
void go() {
  sort(sth + 1, sth + m + 1);
  for (int i = 1; i <= n; ++i) fa[i] = i, mp[i] = i;
  cnt = n;
  for (int i = 1; i <= m; ++i) {
    l = sth[i].l, r = sth[i].r, l = lf(l), r = lf(r);
    if (l == r) continue;
    ade(sth[i].l, sth[i].r, sth[i].num);
    tp[mp[l]][0] = tp[mp[r]][0] = ++cnt;
    mp[l] = mp[r] = cnt, fa[l] = r;
    num[cnt] = sth[i].num;
  }
  tp[cnt][0] = cnt;
  for (int i = 1; i <= 19; ++i) {
    for (int j = 1; j <= cnt; ++j) tp[j][i] = tp[tp[j][i - 1]][i - 1];
  }
  for (int i = cnt; i >= 1; --i) dep[i] = dep[tp[i][0]] + 1;
  memset(ans, -1, sizeof(ans));
  for (int i = 1; i <= m; ++i) {
    l = sth[i].l, r = sth[i].r, tar = lca(l, r);
    if (num[tar] == sth[i].num)
      continue;
    else {
      ans[sth[i].num] = ed[num[tar]].val - 1;
    }
  }
}
void dfs(int k, int fro) {
  for (int i = 0; i < ne[k].size(); ++i) {
    int tar = ne[k][i].to;
    if (tar == fro) continue;
    dep[tar] = dep[k] + 1, tp[tar][0] = k;
    num[tar] = ne[k][i].num;
    dfs(tar, k);
  }
}
void run() {
  memset(dep, 0, sizeof(dep)), memset(tp, 0, sizeof(tp));
  memset(num, 0, sizeof(num)), memset(mask, 0x7f, sizeof(mask));
  dep[1] = 1, tp[1][0] = 1, dfs(1, 0);
  for (int i = 1; i <= 19; ++i) {
    for (int j = 1; j <= n; ++j) tp[j][i] = tp[tp[j][i - 1]][i - 1];
  }
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    l = sth[i].l, r = sth[i].r;
    int dd = dep[r] - dep[l];
    dd = dd > 0 ? dd : -dd;
    if (dd == 1 && (tp[l][0] == r || tp[r][0] == l)) continue;
    int lc = lca(l, r);
    l = lf(l);
    while (dep[l] > dep[lc]) {
      ans[num[l]] = sth[i].val - 1;
      fa[l] = lf(tp[l][0]);
      l = lf(l);
    }
    r = lf(r);
    while (dep[r] > dep[lc]) {
      ans[num[r]] = sth[i].val - 1;
      fa[r] = lf(tp[r][0]);
      r = lf(r);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &l, &r, &val);
    sth[i] = ed[i] = {l, r, val, i};
  }
  go();
  run();
  for (int i = 1; i <= m; ++i) printf("%d ", ans[i]);
}
