#include <bits/stdc++.h>
using namespace std;
void ga(int N, int *A) {
  for (int i(0); i < N; i++) scanf("%d", A + i);
}
struct LCA {
  vector<pair<int, int> > g[(1 << 18)];
  int D[(1 << 18)], P[(1 << 18)][18 + 1], V[(1 << 18)][18 + 1];
  void vnd(int u, int p, int d, int w) {
    *P[u] = p, *V[u] = w, D[u] = d++;
    for (auto &h : g[u])
      if (h.first ^ p) vnd(h.first, u, d, h.second);
  }
  void st(int N, int r = 0) {
    vnd(r, r, 0, 0);
    for (int i(0); i < 18; i++)
      for (int j(0); j < N; j++)
        P[j + 1][i + 1] = P[P[j + 1][i]][i],
                     V[j + 1][i + 1] = max(V[P[j + 1][i]][i], V[j + 1][i]);
  }
  int lca(int a, int b) {
    if (D[a] > D[b]) swap(a, b);
    int d = D[b] - D[a], s = 0, X = -int(1e9 + 1);
    while (d)
      X = max(X, d & 1 ? V[b][s] : -int(1e9 + 1)), b = d & 1 ? P[b][s] : b,
      d >>= 1, ++s;
    for (int i(18 - 1); ~i; --i)
      if (P[a][i] ^ P[b][i])
        X = max(X, max(V[a][i], V[b][i])), a = P[a][i], b = P[b][i];
    return a ^ b ? max(X, max(*V[a], *V[b])) : X;
  }
  void ADD(int A, int B, int C) {
    g[A].push_back({B, C}), g[B].push_back({A, C});
  }
  void CCL() {
    for (auto &h : g) h.clear();
  }
} L;
struct LCB {
  vector<int> g[(1 << 18)];
  int D[(1 << 18)], P[(1 << 18)][18 + 1];
  void dfs(int u, int p, int d) {
    *P[u] = p, D[u] = d++;
    for (int h : g[u])
      if (h ^ p) dfs(h, u, d);
  }
  void pre(int N, int r = 0) {
    dfs(r, r, 0);
    for (int i(0); i < 18; i++)
      for (int j(0); j < N; j++) P[j][i + 1] = P[P[j][i]][i];
  }
  int lca(int a, int b) {
    if (D[a] > D[b]) swap(a, b);
    int d = D[b] - D[a], s = 0;
    while (d) b = d & 1 ? P[b][s] : b, d /= 2, ++s;
    for (int i(18 - 1); ~i; --i)
      if (P[a][i] ^ P[b][i]) a = P[a][i], b = P[b][i];
    return a ^ b ? *P[a] : a;
  }
  void ADD(int A, int B) { g[A].push_back(B), g[B].push_back(A); }
  void CLR() {
    for (auto &h : g) h.clear();
  }
} l;
struct nd {
  int a, b, w, is, I;
  bool operator<(const nd &r) const { return w < r.w; }
} G[(1 << 18)];
struct CRS {
  int C[(1 << 18)], R[(1 << 18)], W;
  int geco(int a) { return C[a] = (a == C[a] ? a : geco(C[a])); }
  bool con(int a, int b) {
    if (geco(a) == geco(b)) return 0;
    if (R[C[a]] > R[C[b]])
      C[C[b]] = C[a];
    else
      R[C[b]] += R[C[a]] == R[C[b]], C[C[a]] = C[b];
    return --W, 1;
  }
  int spa(int M, int N) {
    int S(0);
    sort(G, G + M);
    iota(C, C + N, 0), (memset(R, 0, sizeof(R))), W = N;
    for (int i(0); i < M && W - 1; i++)
      if (con(G[i].a, G[i].b)) S += G[i].w, G[i].is = 1;
    return S;
  }
} K;
int C[(1 << 18)];
int gc(int a) { return C[a] = (a == C[a] ? a : gc(C[a])); }
vector<int> g[(1 << 18)];
vector<pair<int, int> > Q[(1 << 18)];
set<pair<int, int> > T[(1 << 18)];
int N, M, a, b, c, O[(1 << 18)], D[(1 << 18)], S[(1 << 18)], H[(1 << 18)],
    I[(1 << 18)];
bool cp(int a, int b) { return S[a] > S[b]; }
int dfs(int u, int p, int d) {
  D[u] = d, S[u] = 1;
  for (auto &h : g[u])
    if (h ^ p) S[u] += dfs(h, u, d + 1);
  return S[u];
}
void go(int u, int p) {
  if (!u || g[u].size() > 1u) go(g[u][1 - !u], u), C[u] = gc(g[u][1 - !u]);
  for (int k(2 - !u); k < (int)g[u].size(); k++) {
    go(g[u][k], u);
    for (auto &h : T[gc(g[u][k])]) T[gc(u)].insert(h);
  }
  for (auto &h : Q[u]) T[gc(u)].insert(h);
  while (!T[gc(u)].empty() && D[(*T[gc(u)].begin()).second] >= D[u])
    T[gc(u)].erase((*T[gc(u)].begin()));
  if (u) O[I[u]] = T[gc(u)].empty() ? -1 : (*T[gc(u)].begin()).first - 1;
}
int main(void) {
  scanf("%d%d", &N, &M), L.CCL(), l.CLR(), (iota(C, C + N, 0));
  for (int i(0); i < M; i++)
    scanf("%d%d%d", &a, &b, &c), --a, --b, G[i] = {a, b, c, 0, i};
  K.spa(M, N);
  for (int i(0); i < M; i++)
    if (G[i].is)
      a = G[i].a, b = G[i].b, c = G[i].w, l.ADD(a, b), L.ADD(a, b, c),
      g[a].push_back(b), g[b].push_back(a);
  l.pre(N), L.st(N), dfs(0, 0, 0);
  for (int i(0); i < N; i++) sort(g[i].begin(), g[i].end(), cp);
  for (int i(0); i < M; i++)
    if (!G[i].is) {
      O[G[i].I] = L.lca(a = G[i].a, b = G[i].b) - 1;
      Q[a].push_back({c = G[i].w, l.lca(a, b)});
      Q[b].push_back({c, l.lca(a, b)});
    } else
      a = G[i].a, b = G[i].b, a = D[a] < D[b] ? b : a, I[a] = G[i].I;
  go(0, 0);
  for (int i(0); i < M; i++) printf("%d%c", O[i], i + 1 == M ? 10 : 32);
  return 0;
}
