#include <bits/stdc++.h>
using namespace std;
const int N = 200001;
int n, m, tot, f[N], ans[N], fa[N][19], maxn[N][19], minm[N], dep[N], head[N],
    v[N << 1], c[N << 1], nxt[N << 1], id[N << 1], fr[N];
bool tr[N];
struct edge {
  int x, y, z, id;
  edge(int x = 0, int y = 0, int z = 0, int id = 0)
      : x(x), y(y), z(z), id(id) {}
  bool operator<(const edge &b) const { return z < b.z; }
} e[N];
void addedge(int x, int y, int z, int idx) {
  v[++tot] = y;
  c[tot] = z;
  id[tot] = idx;
  nxt[tot] = head[x];
  head[x] = tot;
}
int gf(int x) { return f[x] == x ? x : f[x] = gf(f[x]); }
void dfs(int u) {
  for (int i = 1; i <= 18; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (int i = 1; i <= 18; i++)
    maxn[u][i] = max(maxn[u][i - 1], maxn[fa[u][i - 1]][i - 1]);
  for (int i = head[u]; ~i; i = nxt[i]) {
    if (v[i] != fa[u][0]) {
      dep[v[i]] = dep[u] + 1;
      fa[v[i]][0] = u;
      fr[v[i]] = id[i];
      maxn[v[i]][0] = c[i];
      dfs(v[i]);
    }
  }
}
int getmax(int x, int y, int &lca) {
  if (dep[x] < dep[y]) swap(x, y);
  int ddep = dep[x] - dep[y], ans = 0;
  for (int i = 0; i <= 18; i++)
    if (ddep & (1 << i)) {
      ans = max(ans, maxn[x][i]);
      x = fa[x][i];
    }
  if (x == y) {
    lca = x;
    return ans;
  }
  for (int i = 18; i >= 0; i--) {
    if (fa[x][i] == fa[y][i]) continue;
    ans = max(ans, max(maxn[x][i], maxn[y][i]));
    x = fa[x][i];
    y = fa[y][i];
  }
  lca = fa[x][0];
  return max(ans, max(maxn[x][0], maxn[y][0]));
}
void cover(int s, int t, int z) {
  s = gf(s);
  while (dep[s] > dep[t]) {
    ans[fr[s]] = z;
    f[s] = gf(fa[s][0]);
    s = gf(s);
  }
}
int main() {
  memset(head, -1, sizeof(head));
  memset(ans, -1, sizeof(ans));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
  for (int i = 1; i <= m; i++) e[i].id = i;
  sort(e + 1, e + m + 1);
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = 1; i <= m; i++) {
    int fx = gf(e[i].x), fy = gf(e[i].y);
    if (fx != fy) {
      f[fx] = fy;
      addedge(e[i].x, e[i].y, e[i].z, e[i].id);
      addedge(e[i].y, e[i].x, e[i].z, e[i].id);
      tr[i] = true;
    }
  }
  dfs(1);
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = 1; i <= m; i++) {
    if (!tr[i]) {
      int lca;
      ans[e[i].id] = getmax(e[i].x, e[i].y, lca) - 1;
      cover(e[i].x, lca, e[i].z - 1);
      cover(e[i].y, lca, e[i].z - 1);
    }
  }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
