#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long f = 1, x = 0;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return f * x;
}
int n, m, fa[200003], st[200003], re[200003], num, as[200003];
struct edge {
  int from, to, vlu, id;
} mem[200003 << 1];
struct LCT {
  int son[2], fa, mn, mx, vlu, add;
  bool rev;
} T[200003 << 2];
bool nroot(int x) { return T[T[x].fa].son[0] == x || T[T[x].fa].son[1] == x; }
void pushup(int x) {
  int l = T[x].son[0], r = T[x].son[1];
  T[x].mx = max(T[l].mx, max(T[r].mx, T[x].vlu));
}
void pushdown(int x) {
  int l = T[x].son[0], r = T[x].son[1];
  if (T[x].add != 2e9) {
    T[l].mn = min(T[l].mn, T[x].add);
    T[l].add = min(T[l].add, T[x].add);
    T[r].mn = min(T[r].mn, T[x].add);
    T[r].add = min(T[r].add, T[x].add);
    T[x].add = 2e9;
  }
  if (T[x].rev) {
    T[x].rev = 0;
    swap(T[l].son[0], T[l].son[1]);
    T[l].rev ^= 1;
    swap(T[r].son[1], T[r].son[0]);
    T[r].rev ^= 1;
  }
}
void fdown(int x) {
  if (nroot(x)) fdown(T[x].fa);
  pushdown(x);
}
void rotate(int x) {
  int y = T[x].fa, z = T[y].fa, p = x == T[y].son[1];
  T[y].son[p] = T[x].son[p ^ 1];
  T[T[y].son[p]].fa = y;
  if (nroot(y)) T[z].son[T[z].son[1] == y] = x;
  T[x].fa = z;
  T[y].fa = x;
  T[x].son[p ^ 1] = y;
  pushup(y), pushup(x);
}
void splay(int x) {
  fdown(x);
  while (nroot(x)) {
    int y = T[x].fa, z = T[y].fa;
    if (nroot(y)) {
      if ((x == T[y].son[1]) ^ (y == T[z].son[0]))
        rotate(y), rotate(x);
      else
        rotate(x), rotate(x);
    } else
      rotate(x);
  }
}
void access(int x) {
  int y = 0;
  while (x) {
    splay(x);
    T[x].son[1] = y;
    pushup(x);
    x = T[y = x].fa;
  }
}
void mroot(int x) {
  access(x);
  splay(x);
  T[x].rev ^= 1;
  swap(T[x].son[0], T[x].son[1]);
}
void link(int x, int y) {
  mroot(x);
  T[x].fa = y;
}
bool cmp(edge a, edge b) { return a.vlu < b.vlu; }
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
int main() {
  T[0].mx = -2e9;
  T[0].mn = 2e9;
  n = read(), m = read();
  num = n;
  for (int i = 1; i <= m; ++i)
    mem[i].to = read(), mem[i].from = read(), mem[i].vlu = read(),
    mem[i].id = i;
  for (int i = 1; i <= n; ++i) fa[i] = i, T[i].mx = -2e9, T[i].mn = 2e9;
  sort(mem + 1, mem + 1 + m, cmp);
  for (int i = 1; i <= m; ++i) {
    int x = getfa(mem[i].from), y = getfa(mem[i].to);
    if (x != y) {
      re[i] = ++num;
      T[num].vlu = T[num].mx = mem[i].vlu;
      T[num].mn = 2e9;
      link(num, mem[i].from), link(mem[i].to, num);
      fa[x] = y;
    }
  }
  for (int i = 1; i <= m; ++i)
    if (!re[i]) {
      int x = mem[i].from, y = mem[i].to;
      mroot(x);
      access(y);
      splay(y);
      as[mem[i].id] = T[y].mx - 1;
      T[y].add = min(T[y].add, mem[i].vlu);
      T[y].mn = min(T[y].mn, mem[i].vlu);
    }
  for (int i = 1; i <= m; ++i)
    if (re[i]) {
      mroot(re[i]);
      if (T[re[i]].mn == 2e9)
        as[mem[i].id] = -1;
      else
        as[mem[i].id] = T[re[i]].mn - 1;
    }
  for (int i = 1; i <= m; ++i) cout << as[i] << " ";
  return 0;
}
