#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 15;
const int M = 20;
bool used[N];
vector<int> adj[N];
int a[N], b[N], w[N];
int out[N];
int dsu[N];
int f(int x) { return dsu[x] == x ? x : dsu[x] = f(dsu[x]); }
bool merge(int id) {
  int u = f(a[id]), v = f(b[id]);
  if (u != v) {
    dsu[u] = v;
    return true;
  }
  return false;
}
int anc[N][M], wa[N][M];
int depth[N];
void construct(int node, int par) {
  for (int i = 1; i < M; ++i) {
    anc[node][i] = anc[anc[node][i - 1]][i - 1];
    wa[node][i] = max(wa[node][i - 1], wa[anc[node][i - 1]][i - 1]);
  }
  for (int eid : adj[node]) {
    int ch = a[eid] ^ b[eid] ^ node;
    if (ch != par) {
      depth[ch] = depth[node] + 1;
      anc[ch][0] = node;
      wa[ch][0] = w[eid];
      construct(ch, node);
    }
  }
}
pair<int, int> getAnc(int x, int y) {
  if (depth[x] < depth[y]) swap(x, y);
  int diff = depth[x] - depth[y], ret = 0;
  for (int i = 0; i < M; ++i) {
    if (diff & (1 << i)) {
      ret = max(ret, wa[x][i]);
      x = anc[x][i];
    }
  }
  if (x != y) {
    for (int i = M - 1; i >= 0; --i) {
      if (anc[x][i] != anc[y][i]) {
        ret = max(ret, max(wa[x][i], wa[y][i]));
        x = anc[x][i];
        y = anc[y][i];
      }
    }
    ret = max(ret, max(wa[x][0], wa[y][0]));
    x = anc[x][0];
  }
  return {ret, x};
}
multiset<int> g[N];
vector<int> ed[N];
void dfs(int node, int par) {
  for (int eid : adj[node]) {
    int ch = a[eid] ^ b[eid] ^ node;
    if (ch != par) {
      dfs(ch, node);
      out[eid] = g[ch].size() == 0 ? -1 : (*g[ch].begin()) - 1;
      if (g[node].size() < g[ch].size()) {
        swap(g[node], g[ch]);
      }
      for (int x : g[ch]) g[node].insert(x);
    }
  }
  for (int val : ed[node]) {
    assert(g[node].find(val) != g[node].end());
    g[node].erase(g[node].find(val));
    assert(g[node].find(val) != g[node].end());
    g[node].erase(g[node].find(val));
  }
}
void solve2() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<pair<int, int> > edges;
  for (int i = 0; i < m; ++i) {
    scanf("%d %d %d ", &a[i], &b[i], &w[i]);
    edges.push_back({w[i], i});
  }
  for (int i = n; i; --i) dsu[i] = i;
  sort(edges.begin(), edges.end());
  for (int i = 0; i < m; ++i) {
    int id = edges[i].second;
    if (merge(id)) {
      used[id] = true;
      adj[a[id]].push_back(id);
      adj[b[id]].push_back(id);
    }
  }
  construct(1, 0);
  for (int i = 0; i < m; ++i) {
    if (!used[i]) {
      pair<int, int> tmp = getAnc(a[i], b[i]);
      out[i] = tmp.first - 1;
      g[a[i]].insert(w[i]);
      g[b[i]].insert(w[i]);
      ed[tmp.second].push_back(w[i]);
    }
  }
  dfs(1, 0);
  for (int i = 0; i < m; ++i) {
    printf("%d\n", out[i]);
  }
}
void solve() { solve2(); }
int main() {
  solve();
  return 0;
}
