#include <bits/stdc++.h>
using namespace std;
const int N = 200 * 1000 + 10;
struct edge {
  int u, v, w;
  edge() {}
  edge(int u, int v, int w) : u(u), v(v), w(w) {}
} e[N];
vector<int> vec[N];
int n, m, root[N], mx[35][N], par[35][N], h[N], ans[N];
bool mst[N], mark[N];
multiset<int> add[N], rem[N];
bool cmp(int i, int j) { return e[i].w < e[j].w; }
int dfs(int v) {
  mark[v] = true;
  for (int ind : vec[v]) {
    int u = e[ind].u + e[ind].v - v;
    if (mst[ind]) {
      if (!mark[u]) {
        h[u] = h[v] + 1;
        par[0][u] = v;
        mx[0][u] = e[ind].w;
        ans[ind] = dfs(u);
        if ((int)add[u].size() > (int)add[v].size()) swap(add[u], add[v]);
        for (int x : add[u]) add[v].insert(x);
      }
    }
  }
  for (int x : rem[v]) add[v].erase(add[v].find(x));
  if (add[v].empty()) return -1;
  return *add[v].begin() - 1;
}
void fill_par() {
  for (int i = 0; i < n; i++) mark[i] = false;
  for (int i = 0; i < 31; i++)
    for (int j = 0; j < n; j++) par[i][j] = -1;
  dfs(0);
  for (int i = 1; i < 31; i++)
    for (int j = 0; j < n; j++)
      if (par[i - 1][j] != -1) {
        mx[i][j] = max(mx[i - 1][j], mx[i - 1][par[i - 1][j]]);
        par[i][j] = par[i - 1][par[i - 1][j]];
      }
}
int get_root(int v) {
  if (v == root[v]) return v;
  return root[v] = get_root(root[v]);
}
pair<int, int> get_lca(int u, int v) {
  if (h[u] < h[v]) swap(u, v);
  int mxe = 0;
  for (int i = 30; i >= 0; i--)
    if ((h[u] - h[v]) & (1 << i)) {
      mxe = max(mxe, mx[i][u]);
      u = par[i][u];
    }
  for (int i = 30; i >= 0; i--)
    if (par[i][u] != par[i][v]) {
      mxe = max(mxe, mx[i][v]), mxe = max(mxe, mx[i][u]);
      u = par[i][u], v = par[i][v];
    }
  if (u != v) {
    mxe = max(mxe, mx[0][v]), mxe = max(mxe, mx[0][u]);
    u = par[0][u];
  }
  return {u, mxe};
}
void merge(int ind, int ty) {
  int u = e[ind].u, v = e[ind].v;
  int pu = get_root(u), pv = get_root(v);
  if (pu != pv) {
    mst[ind] = true;
    root[pu] = pv;
  }
  if (pu != pv || !ty) return;
  ans[ind] = (get_lca(u, v)).second - 1;
}
void merge() {
  for (int i = 0; i < n; i++) root[i] = i;
  vector<int> temp(m);
  iota(temp.begin(), temp.end(), 0);
  sort(temp.begin(), temp.end(), cmp);
  for (int i = 0; i < m; i++) merge(temp[i], 0);
  fill_par();
  for (int i = 0; i < m; i++)
    if (!mst[i]) {
      int u = e[i].u, v = e[i].v;
      int lca = get_lca(u, v).first;
      add[u].insert(e[i].w);
      add[v].insert(e[i].w);
      rem[lca].insert(e[i].w);
      rem[lca].insert(e[i].w);
    }
  fill_par();
  for (int i = 0; i < n; i++) root[i] = i;
  for (int i = 0; i < m; i++) merge(temp[i], 1);
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    e[i] = edge(u, v, w);
    vec[u].push_back(i);
    vec[v].push_back(i);
  }
  merge();
  for (int i = 0; i < m; i++) cout << ans[i] << " \n"[i == m - 1];
  return 0;
}
