#include <bits/stdc++.h>
using namespace std;
int n, m, an[200000], pa[200001], dis[200001], dp[200001], in[200001],
    out[200001], tpa[200001];
vector<pair<int, int> > g[200001];
vector<pair<int, pair<int, int> > > tr[200001];
vector<int> eu, kil;
bool vis[200001];
pair<int, int> ba[200001][17];
struct E {
  int u, v, w, id;
  E() {}
  bool operator<(E const &o) const { return w < o.w; }
} ed[200000];
int P(int v) { return pa[v] ? pa[v] = P(pa[v]) : v; }
inline void ad(int i, int x) {
  while (i < (int)eu.size()) eu[i] += x, i += i & -i;
}
inline int gt(int i) {
  int an = 0;
  while (i) an += eu[i], i ^= i & -i;
  return an;
}
void pl1(int v = 1, int p = 0) {
  dis[v] = dp[v] = dis[p] + 1;
  vis[v] = true;
  for (int i = 0; i < (int)(g[v].size()); ++i) {
    int u = g[v][i].first;
    if (u == p) continue;
    if (vis[u])
      dp[v] = min(dp[v], dis[u]);
    else {
      pl1(u, v);
      dp[v] = min(dp[v], dp[u]);
      if (dp[u] > dis[v]) an[g[v][i].second] = -1;
    }
  }
}
void pl2(int v = 1, int p = 0, int w = 0) {
  dp[v] = dp[p] + 1;
  in[v] = eu.size();
  eu.push_back(0);
  ba[v][0] = make_pair(p, w);
  int k = 1, z;
  while (k < 17 && (z = ba[v][k - 1].first))
    ba[v][k] = make_pair(ba[z][k - 1].first,
                         max(ba[v][k - 1].second, ba[z][k - 1].second)),
    ++k;
  for (int i = 0; i < (int)(tr[v].size()); ++i) {
    int u = tr[v][i].first;
    if (u == p) {
      tpa[v] = i;
      continue;
    }
    pl2(u, v, tr[v][i].second.second);
  }
  out[v] = eu.size();
  eu.push_back(0);
}
inline int D(int v) { return dp[v] + gt(in[v]); }
int W;
inline int JJ(int v) {
  pair<int, pair<int, int> > &t = tr[v][tpa[v]];
  int u = P(t.first);
  pa[v] = u;
  an[t.second.first] = W;
  ad(in[v], -1);
  ad(out[v], 1);
  return u;
}
void J(int a, int b) {
  int dpa = D(a), dpb = D(b);
  if (dpa < dpb) swap(a, b), swap(dpa, dpb);
  int d = dpa - dpb;
  while (d--) {
    a = JJ(a);
  }
  if (a == b) return;
  while ((a = JJ(a)) != (b = JJ(b)))
    ;
}
int lca(int a, int b) {
  if (dp[a] < dp[b]) swap(a, b);
  int an = 0;
  int d = dp[a] - dp[b], j = 0;
  while (d) {
    if (d & 1) an = max(an, ba[a][j].second), a = ba[a][j].first;
    d >>= 1;
    ++j;
  }
  if (a == b) return an;
  for (int i = 16; i >= 0; --i)
    if (ba[a][i].first != ba[b][i].first)
      an = max(an, max(ba[a][i].second, ba[b][i].second)), a = ba[a][i].first,
      b = ba[b][i].first;
  return max(an, max(ba[a][0].second, ba[b][0].second));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (int)(m); ++i) {
    scanf("%d%d%d", &ed[i].u, &ed[i].v, &ed[i].w);
    ed[i].id = i;
    g[ed[i].u].push_back(make_pair(ed[i].v, ed[i].id));
    g[ed[i].v].push_back(make_pair(ed[i].u, ed[i].id));
  }
  pl1();
  sort(ed, ed + m);
  for (int i = 0; i < (int)(m); ++i) {
    int a = P(ed[i].u), b = P(ed[i].v);
    if (a == b)
      kil.push_back(i);
    else {
      pa[b] = a;
      tr[ed[i].u].push_back(make_pair(ed[i].v, make_pair(ed[i].id, ed[i].w)));
      tr[ed[i].v].push_back(make_pair(ed[i].u, make_pair(ed[i].id, ed[i].w)));
    }
  }
  eu.push_back(0);
  pl2();
  for (int i = 1; i < (int)(n + 1); ++i) pa[i] = 0;
  for (int i = 0; i < (int)(kil.size()); ++i)
    an[ed[kil[i]].id] = lca(ed[kil[i]].v, ed[kil[i]].u) - 1;
  for (int i = 0; i < (int)(kil.size()); ++i)
    W = ed[kil[i]].w - 1, J(P(ed[kil[i]].v), P(ed[kil[i]].u));
  printf("%d", an[0]);
  for (int i = 1; i < (int)(m); ++i) printf(" %d", an[i]);
  printf("\n");
}
