#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int M = 2e5 + 10;
const int logn = 20;
int n, m;
vector<vector<int> > e;
bool mark[M];
int fa[M];
int ans[M];
int p[logn][M], pmaxn[logn][M];
int pid[M];
int deep[M];
vector<vector<int> > g[M];
void dfs(int o, int u) {
  deep[u] = deep[o] + 1;
  for (vector<int> &v : g[u])
    if (v[0] != o) {
      p[0][v[0]] = u;
      for (int i = 1; i < logn; ++i) p[i][v[0]] = p[i - 1][p[i - 1][v[0]]];
      pmaxn[0][v[0]] = v[2];
      for (int i = 1; i < logn; ++i)
        pmaxn[i][v[0]] = max(pmaxn[i - 1][v[0]], pmaxn[i - 1][p[i - 1][v[0]]]);
      pid[v[0]] = v[1];
      dfs(u, v[0]);
    }
}
int getlca(int u, int v) {
  if (deep[u] < deep[v]) swap(u, v);
  int maxn[2] = {0, 0};
  for (int i = logn - 1; i >= 0; --i)
    if (deep[p[i][u]] >= deep[v]) {
      maxn[0] = max(maxn[0], pmaxn[i][u]);
      u = p[i][u];
    }
  if (u == v) return maxn[0];
  for (int i = logn - 1; i >= 0; --i)
    if (p[i][u] != p[i][v]) {
      maxn[0] = max(maxn[0], pmaxn[i][u]);
      maxn[1] = max(maxn[1], pmaxn[i][v]);
      u = p[i][u], v = p[i][v];
    }
  return max({maxn[0], maxn[1], pmaxn[0][u], pmaxn[0][v]});
}
int dsu(int x) { return fa[x] == x ? x : fa[x] = dsu(fa[x]); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    e.emplace_back(vector<int>{c, u, v, i});
  }
  sort(e.begin(), e.end());
  iota(fa, fa + n + 1, 0);
  for (auto &v : e)
    if (dsu(v[1]) != dsu(v[2])) {
      fa[dsu(v[1])] = dsu(v[2]);
      mark[v[3]] = true;
      g[v[1]].emplace_back(vector<int>{v[2], v[3], v[0]});
      g[v[2]].emplace_back(vector<int>{v[1], v[3], v[0]});
    }
  dfs(0, 1);
  iota(fa, fa + n + 1, 0);
  memset(ans, 0, sizeof(ans[0]) * (n + 1));
  for (auto &v : e)
    if (!mark[v[3]]) {
      ans[v[3]] = getlca(v[1], v[2]);
      int U = v[1], V = v[2];
      while (true) {
        U = dsu(U);
        V = dsu(V);
        if (U == V) break;
        if (deep[U] < deep[V]) swap(U, V);
        ans[pid[U]] = v[0];
        fa[U] = dsu(p[0][U]);
      }
    }
  for (int i = 0; i < m; ++i) {
    printf("%d%c", ans[i] - 1, " \n"[i == m - 1]);
  }
  return 0;
}
