#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 2, lg = 18;
int x[maxn], y[maxn], w[maxn];
int par[maxn], h[maxn];
int mx[lg + 2][maxn], Par[lg + 2][maxn];
int ans[maxn];
vector<pair<int, int> > adj[maxn];
set<pair<int, int> > s[maxn];
bool mark[maxn];
int get_par(int x) {
  if (x == par[x]) {
    return x;
  }
  return par[x] = get_par(par[x]);
}
void merge(int x, int y) { par[get_par(x)] = get_par(y); }
void dfs(int v, int p = -1) {
  for (auto u : adj[v]) {
    if (u.first != p) {
      h[u.first] = h[v] + 1;
      Par[0][u.first] = v;
      mx[0][u.first] = w[u.second];
      for (int i = 1; i < lg; i++) {
        Par[i][u.first] = Par[i - 1][Par[i - 1][u.first]];
        mx[i][u.first] =
            max(mx[i - 1][u.first], mx[i - 1][Par[i - 1][u.first]]);
      }
      dfs(u.first, v);
    }
  }
}
int find_par(int x, int p) {
  for (int i = 0; i < lg; i++) {
    if (p & (1 << i)) {
      x = Par[i][x];
    }
  }
  return x;
}
int lca(int x, int y) {
  if (h[x] < h[y]) {
    swap(x, y);
  }
  x = find_par(x, h[x] - h[y]);
  for (int i = lg; i >= 0; i--) {
    if (Par[i][x] != Par[i][y]) {
      x = Par[i][x];
      y = Par[i][y];
    }
  }
  if (x == y) return x;
  return Par[0][x];
}
int find_mx(int x, int d) {
  int res = 0;
  for (int i = 0; i < lg; i++) {
    if (d & (1 << i)) {
      res = max(res, mx[i][x]);
      x = Par[i][x];
    }
  }
  return res;
}
void Merge(int x, int y) {
  if (s[x].size() > s[y].size()) {
    swap(s[x], s[y]);
  }
  for (auto u : s[x]) {
    s[y].insert(u);
  }
  s[x].clear();
}
void dfs2(int v, int p = -1) {
  for (auto u : adj[v]) {
    if (u.first != p) {
      dfs2(u.first, v);
      ans[u.second] = (s[u.first].size()) ? s[u.first].begin()->first : -1;
      Merge(u.first, v);
    }
  }
  while (s[v].size() && -s[v].begin()->second >= h[v]) {
    s[v].erase(s[v].begin());
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > e;
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i] >> w[i];
    x[i]--;
    y[i]--;
    e.push_back({w[i], i});
  }
  for (int i = 0; i < n; i++) par[i] = i;
  sort(e.begin(), e.end());
  for (int i = 0; i < m; i++) {
    int j = e[i].second;
    if (get_par(x[j]) != get_par(y[j])) {
      merge(x[j], y[j]);
      adj[x[j]].push_back({y[j], j});
      adj[y[j]].push_back({x[j], j});
      mark[j] = true;
    }
  }
  dfs(0);
  for (int i = 0; i < m; i++) {
    if (!mark[i]) {
      int lc = lca(x[i], y[i]);
      ans[i] =
          max(find_mx(x[i], h[x[i]] - h[lc]), find_mx(y[i], h[y[i]] - h[lc])) -
          1;
      s[x[i]].insert({w[i] - 1, -h[lc]});
      s[y[i]].insert({w[i] - 1, -h[lc]});
    }
  }
  dfs2(0);
  for (int i = 0; i < m; i++) {
    cout << ans[i] << " ";
  }
  return 0;
}
