#include <bits/stdc++.h>
using namespace std;
const int N = 200 * 1000 + 20, L = 20;
int n, m;
vector<pair<int, int>> adj[N], g[N];
vector<pair<int, pair<int, int>>> edg;
set<pair<int, int>> s[N];
int w[N], _rank[N], gp[N], par[N][L], mx[N][L], dis[N], ans[N];
bool tree[N];
inline int find(int a) {
  if (gp[a] == a) return a;
  return gp[a] = find(gp[a]);
}
inline void _union(int a, int b) {
  a = find(a), b = find(b);
  if (_rank[a] < _rank[b]) swap(a, b);
  gp[b] = a;
  if (_rank[a] == _rank[b]) _rank[a]++;
  return;
}
inline bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  return w[a.first] == w[b.first] ? a.second < b.second
                                  : w[a.first] < w[b.first];
}
inline void mst() {
  sort(edg.begin(), edg.end(), cmp);
  for (auto e : edg) {
    int u = e.second.first, v = e.second.second;
    if (find(u) == find(v)) continue;
    _union(u, v);
    g[u].push_back({v, e.first});
    g[v].push_back({u, e.first});
    tree[e.first] = 1;
  }
  return;
}
void dfst(int v, int p = -1) {
  for (auto e : g[v]) {
    int u = e.first, i = e.second;
    if (u == p) continue;
    par[u][0] = v;
    mx[u][0] = w[i];
    dis[u] = dis[v] + 1;
    dfst(u, v);
  }
  return;
}
inline int findpar(int x, int v) {
  for (int i = 0; i < L; i++) {
    if ((x >> i) & 1) v = par[v][i];
  }
  return v;
}
inline int findmx(int x, int v) {
  int res = 0;
  for (int i = 0; i < L; i++) {
    if ((x >> i) & 1) {
      res = max(res, mx[v][i]);
      v = par[v][i];
    }
  }
  return res;
}
inline int lca(int u, int v) {
  if (dis[u] < dis[v]) swap(u, v);
  u = findpar(dis[u] - dis[v], u);
  if (u == v) return v;
  for (int i = L - 1; i > -1; i--) {
    if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
  }
  return par[v][0];
}
void dfs(int v, int p = -1, int id = -1) {
  int mx = int(s[v].size()), umx = v;
  for (auto e : g[v]) {
    int u = e.first, i = e.second;
    if (u != p) {
      dfs(u, v, i);
      if (mx < int(s[u].size())) {
        mx = int(s[u].size());
        umx = u;
      }
    }
  }
  swap(s[v], s[umx]);
  for (auto e : g[v]) {
    int u = e.first;
    if (u != p) {
      while (!s[u].empty()) {
        s[v].insert(*s[u].begin());
        s[u].erase(s[u].begin());
      }
    }
  }
  while (!s[v].empty() && s[v].begin()->second >= dis[v])
    s[v].erase(s[v].begin());
  if (s[v].empty())
    ans[id] = -1;
  else {
    ans[id] = s[v].begin()->first - 1;
  }
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ;
  for (int i = 0; i < N; i++) gp[i] = i;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v >> w[i];
    u--, v--;
    adj[v].push_back({u, i});
    adj[u].push_back({v, i});
    edg.push_back({i, {u, v}});
  }
  mst();
  dfst(0);
  for (int i = 1; i < L; i++) {
    for (int v = 0; v < n; v++) {
      par[v][i] = par[par[v][i - 1]][i - 1];
      mx[v][i] = max(mx[par[v][i - 1]][i - 1], mx[v][i - 1]);
    }
  }
  sort(edg.begin(), edg.end());
  for (int i = 0; i < m; i++) {
    if (!tree[i]) {
      int u = edg[i].second.first, v = edg[i].second.second;
      int a = lca(u, v);
      int res = max(findmx(dis[v] - dis[a], v), findmx(dis[u] - dis[a], u));
      ans[i] = res - 1;
      s[v].insert({w[i], dis[a]});
      s[u].insert({w[i], dis[a]});
    }
  }
  dfs(0);
  for (int i = 0; i < m; i++) cout << ans[i] << ' ';
  return 0;
}
