#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 7, inf = 1e9 + 7;
struct data {
  int u, v, w, id;
  friend bool operator<(data a, data b) { return a.w < b.w; }
} s[N];
int fa[N], parent[N], c[N][2], mm[N][2], w[N][2], tag[N];
int n, m, rev[N], used[N], sz, q[N], top, ans[N];
void init() {
  for (int i = 0; i < N; ++i)
    mm[i][0] = w[i][0] = -inf, mm[i][1] = w[i][1] = tag[i] = inf, parent[i] = i;
}
bool isroot(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; }
void update(int x) {
  int l = c[x][0], r = c[x][1];
  mm[x][0] = max(w[x][0], max(mm[l][0], mm[r][0]));
  mm[x][1] = min(w[x][1], min(mm[l][1], mm[r][1]));
}
void pushdown(int x) {
  int l = c[x][0], r = c[x][1];
  if (tag[x] != inf) {
    tag[l] = min(tag[l], tag[x]);
    tag[r] = min(tag[r], tag[x]);
    if (l > n) {
      w[l][1] = min(w[l][1], tag[l]);
      mm[l][1] = min(mm[l][1], tag[l]);
    }
    if (r > n) {
      w[r][1] = min(w[r][1], tag[r]);
      mm[r][1] = min(mm[r][1], tag[r]);
    }
    tag[x] = inf;
  }
  if (rev[x]) {
    rev[x] ^= 1;
    rev[l] ^= 1;
    rev[r] ^= 1;
    swap(c[x][0], c[x][1]);
  }
}
void rotate(int x) {
  int y = fa[x], z = fa[y], l, r;
  if (c[y][0] == x)
    l = 0;
  else
    l = 1;
  r = l ^ 1;
  if (!isroot(y)) c[z][c[z][1] == y] = x;
  fa[x] = z;
  fa[y] = x;
  fa[c[x][r]] = y;
  c[y][l] = c[x][r];
  c[x][r] = y;
  update(y);
  update(x);
}
void splay(int x) {
  q[++top] = x;
  for (int i = x; !isroot(i); i = fa[i]) q[++top] = fa[i];
  while (top) pushdown(q[top--]);
  while (!isroot(x)) {
    int y = fa[x], z = fa[y];
    if (!isroot(y)) {
      if (c[y][0] == x ^ c[z][0] == y)
        rotate(x);
      else
        rotate(y);
    }
    rotate(x);
  }
}
void access(int x) {
  for (int t = 0; x; t = x, x = fa[x]) splay(x), c[x][1] = t, update(x);
}
void makeroot(int x) {
  access(x);
  splay(x);
  rev[x] ^= 1;
}
void link(int x, int y) {
  makeroot(x);
  fa[x] = y;
}
void split(int x, int y) {
  makeroot(x);
  access(y);
  splay(y);
}
int find(int x) { return parent[x] == x ? x : parent[x] = find(parent[x]); }
void kruskal() {
  sort(s + 1, s + m + 1);
  for (int i = 1; i <= m; ++i) {
    int u = s[i].u, v = s[i].v;
    int p1 = find(u), p2 = find(v);
    if (p1 == p2) continue;
    used[i] = true;
    parent[p1] = p2;
    ++sz;
    mm[sz][0] = w[sz][0] = s[i].w;
    link(u, sz);
    link(v, sz);
  }
}
int main() {
  init();
  scanf("%d%d", &n, &m);
  sz = n;
  for (int i = 1; i <= m; ++i)
    scanf("%d%d%d", &s[i].u, &s[i].v, &s[i].w), s[i].id = i;
  kruskal();
  for (int i = 1; i <= m; ++i)
    if (!used[i]) {
      split(s[i].u, s[i].v);
      ans[s[i].id] = mm[s[i].v][0] - 1;
      tag[s[i].v] = min(tag[s[i].v], s[i].w);
    }
  for (int i = 1; i <= m; ++i)
    if (used[i]) {
      split(s[i].u, s[i].v);
      ans[s[i].id] = mm[s[i].v][1];
      if (ans[s[i].id] == inf)
        ans[s[i].id] = -1;
      else
        ans[s[i].id]--;
    }
  for (int i = 1; i <= m; ++i) printf("%d%c", ans[i], i == m ? '\n' : ' ');
  return 0;
}
