#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
const int inf = (1 << 30);
const int N = 201000;
int n, m, f[N], u, v, w, dep[N], ret[N];
vector<vector<int> > E, Ew;
vector<pair<int, int> > e[N];
int mark[N], p[N][20], pe[N][20], pid[N];
void dfs(int u, int f) {
  dep[u] = dep[f] + 1;
  for (auto w : e[u]) {
    int v = w.first;
    if (v == f) continue;
    p[v][0] = u;
    pid[v] = w.second;
    pe[v][0] = Ew[pid[v]][0];
    dfs(v, u);
  }
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int lca(int u, int v) {
  if (dep[u] > dep[v]) swap(u, v);
  int mv = 0;
  for (int i = 18 - 1; i >= 0; i--)
    if (dep[p[v][i]] >= dep[u]) mv = max(mv, pe[v][i]), v = p[v][i];
  if (u == v) return mv;
  for (int i = 18 - 1; i >= 0; i--)
    if (p[v][i] != p[u][i])
      mv = max(mv, max(pe[u][i], pe[v][i])), u = p[u][i], v = p[v][i];
  return max(mv, max(pe[u][0], pe[v][0]));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i < n + 1; i++) f[i] = i;
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &u, &v, &w);
    E.push_back(vector<int>{w, u, v, i});
    Ew.push_back(vector<int>{w, u, v, i});
  }
  sort((E).begin(), (E).end());
  for (int i = 0; i < m; i++) {
    int id = E[i][3], u = E[i][2], v = E[i][1];
    if (find(u) != find(v)) {
      mark[id] = 1;
      e[u].push_back(make_pair(v, id));
      e[v].push_back(make_pair(u, id));
      f[find(u)] = find(v);
    }
  }
  dfs(1, 0);
  for (int j = 1; j < 18; j++) {
    for (int i = 1; i < n + 1; i++) {
      p[i][j] = p[p[i][j - 1]][j - 1];
      pe[i][j] = max(pe[i][j - 1], pe[p[i][j - 1]][j - 1]);
    }
  }
  for (int i = 1; i < n + 1; i++) f[i] = i;
  for (int i = 0; i < m; i++) ret[i] = inf;
  for (int i = 0; i < m; i++) {
    int id = E[i][3], u = E[i][2], v = E[i][1];
    if (!mark[id]) {
      ret[id] = lca(u, v);
      while (1) {
        u = find(u);
        v = find(v);
        if (u == v) break;
        if (dep[v] > dep[u]) swap(u, v);
        ret[pid[u]] = E[i][0];
        f[u] = find(p[u][0]);
      }
    }
  }
  for (int i = 0; i < m; i++) {
    if (ret[i] == inf) ret[i] = 0;
    printf("%d\n", ret[i] - 1);
  }
}
