#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int maxn5 = 5e5 + 10;
const int maxn3 = 1e3 + 10;
const long long mod = 1e9 + 7;
const long long inf = 2e9;
int par[maxn5], a[maxn5], b[maxn5], rnk[maxn5], h[maxn5], mn[maxn5], pos[maxn5];
bool mark[maxn5], used[maxn5];
vector<int> ver, av[maxn], cmpver[maxn5], rem[maxn5];
vector<pair<int, int>> adj[maxn5], kr, mstadj[maxn5];
map<int, vector<int>> have;
int n, m, s[maxn5];
int sareedge[maxn], edgefor[maxn], cmp[maxn], meet[maxn], parn[20][maxn5];
long long len[maxn5], val[maxn5], backedge[maxn];
multiset<int> bechecked[maxn5];
int get_par(int a) { return (par[a] == 0 ? a : par[a] = get_par(par[a])); }
void join(int a, int b) {
  a = get_par(a);
  b = get_par(b);
  if (a == b) return;
  if (rnk[a] > rnk[b]) swap(a, b);
  if (rnk[a] == rnk[b]) rnk[b]++;
  par[a] = b;
  return;
}
void dfs(int v, int par = -1) {
  mark[v] = true;
  mn[v] = v;
  for (auto [u, ind] : adj[v]) {
    if (!mark[u]) {
      h[u] = h[v] + 1;
      dfs(u, ind);
      if (h[mn[u]] > h[v]) {
        pos[ind] = 2;
      } else if (h[mn[u]] < h[mn[v]])
        mn[v] = mn[u];
    } else if (ind != par and h[mn[u]] < h[mn[v]])
      mn[v] = mn[u];
  }
  return;
}
void find_pos() {
  int num = 0;
  for (auto it = have.begin(); it != have.end(); it++) {
    val[num] = it->first;
    for (auto u : it->second) av[num].push_back(u);
    num++;
  }
  for (int i = 0; i < num; i++) {
    for (auto ind : av[i]) {
      int x = get_par(a[ind]), y = get_par(b[ind]);
      if (x != y) {
        adj[x].push_back({y, ind}), adj[y].push_back({x, ind});
        ver.push_back(x);
        ver.push_back(y);
        pos[ind] = 1;
      }
    }
    for (auto u : ver)
      if (!mark[u]) {
        h[u] = 0;
        dfs(u);
      }
    for (auto u : ver) {
      mark[u] = false;
      adj[u].clear();
    }
    ver.clear();
    for (auto ind : av[i]) join(a[ind], b[ind]);
  }
  return;
}
int lca(int a, int b) {
  if (h[a] < h[b]) swap(a, b);
  int d = h[a] - h[b];
  for (int i = 0; i < 20; i++)
    if ((d >> i) & 1) a = parn[i][a];
  if (a == b) return a;
  for (int i = 19; i >= 0; i--)
    if (parn[i][a] != parn[i][b]) a = parn[i][a], b = parn[i][b];
  return parn[0][a];
}
void dfsdet(int v) {
  for (int i = 1; i < 20 and parn[i - 1][v] != -1; i++)
    parn[i][v] = parn[i - 1][parn[i - 1][v]];
  for (auto [u, ind] : mstadj[v])
    if (u != parn[0][v]) {
      h[u] = h[v] + 1;
      parn[0][u] = v;
      dfsdet(u);
    }
  return;
}
void dfs_mst(int v, int par) {
  int big = 0;
  for (auto [u, ind] : mstadj[v])
    if (u != par) {
      dfs_mst(u, v);
      if (!bechecked[s[u]].empty()) backedge[ind] = *bechecked[s[u]].begin();
      if (bechecked[s[u]].size() >= bechecked[s[big]].size()) big = u;
    }
  s[v] = v;
  if (big != 0 and bechecked[v].size() < bechecked[s[big]].size())
    s[v] = s[big];
  if (s[v] != v)
    for (auto it = bechecked[v].begin(); it != bechecked[v].end(); it++)
      bechecked[s[v]].insert(*it);
  for (auto [u, ind] : mstadj[v])
    if (u != par and ((s[v] != v and u != big) or (s[v] == v))) {
      for (auto it = bechecked[s[u]].begin(); it != bechecked[s[u]].end(); it++)
        bechecked[s[v]].insert(*it);
    }
  for (auto u : rem[v]) {
    bechecked[s[v]].erase(bechecked[s[v]].find(u));
    bechecked[s[v]].erase(bechecked[s[v]].find(u));
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> len[i];
    have[len[i]].push_back(i);
  }
  find_pos();
  for (int i = 0; i < m; i++) kr.push_back({len[i], i});
  sort(kr.begin(), kr.end());
  for (int i = 1; i <= n; i++) {
    cmp[i] = i;
    cmpver[i].push_back(i);
  }
  int newnode = n + 1;
  for (int i = 0; i < m; i++)
    if (pos[i] != 2) {
      sareedge[newnode] = newnode + 1;
      sareedge[newnode + 1] = newnode;
      edgefor[newnode] = i;
      edgefor[newnode + 1] = i;
      cmp[newnode] = cmp[a[i]];
      cmp[newnode + 1] = cmp[b[i]];
      cmpver[a[i]].push_back(newnode++);
      cmpver[b[i]].push_back(newnode++);
    }
  for (auto [sz, ind] : kr) {
    if (cmp[a[ind]] == cmp[b[ind]]) continue;
    mstadj[a[ind]].push_back({b[ind], ind});
    mstadj[b[ind]].push_back({a[ind], ind});
    used[ind] = true;
    int x = cmp[a[ind]], y = cmp[b[ind]];
    if (cmpver[x].size() > cmpver[y].size()) swap(x, y);
    for (auto u : cmpver[x]) {
      cmp[u] = y;
      if (u > n and cmp[sareedge[u]] == cmp[u] and meet[edgefor[u]] == 0)
        meet[edgefor[u]] = sz;
      cmpver[y].push_back(u);
    }
    cmpver[x].clear();
  }
  fill(backedge, backedge + m + 10, inf);
  for (int i = 0; i < 20; i++)
    for (int j = 1; j <= n; j++) parn[i][j] = -1;
  dfsdet(1);
  for (int i = 0; i < m; i++)
    if (!used[i]) {
      int x = a[i], y = b[i];
      int c = lca(x, y);
      bechecked[y].insert(len[i]);
      bechecked[x].insert(len[i]);
      rem[c].push_back(len[i]);
    }
  dfs_mst(1, -1);
  for (int i = 0; i < m; i++) {
    if (pos[i] == 2)
      cout << (backedge[i] >= inf ? 0 : backedge[i]) - 1 << ' ';
    else if (pos[i] == 1)
      cout << len[i] - 1 << ' ';
    else
      cout << meet[i] - 1 << ' ';
  }
  return 0;
}
