#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(int x) {
  write(x);
  puts("");
}
const int N = 2e5 + 5, INF = 1e9;
struct Edge {
  int u, v, w, id;
} E[N];
int n, m, pa[N];
inline bool cmp(Edge A, Edge B) { return A.w < B.w; }
inline void init() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++) pa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read(), w = read();
    E[i] = (Edge){u, v, w, i};
  }
  sort(E + 1, E + 1 + m, cmp);
}
struct edge {
  int link, next, val;
} e[N << 1];
bool mark[N];
int head[N], tot, cnt;
int getfa(int x) { return (pa[x] == x) ? x : pa[x] = getfa(pa[x]); }
inline void add_edge(int u, int v, int w) {
  e[++tot] = (edge){v, head[u], w};
  head[u] = tot;
}
inline void insert(int u, int v, int w) {
  add_edge(u, v, w);
  add_edge(v, u, w);
}
inline void kruscal() {
  for (int i = 1; i <= m; i++) {
    int p = getfa(E[i].u), q = getfa(E[i].v);
    if (p != q) {
      pa[p] = q;
      ++cnt;
      insert(E[i].u, E[i].v, E[i].w);
      mark[i] = 1;
      if (cnt == n - 1) break;
    }
  }
}
int mn[N][20], mx[N][20], fa[N][20], dep[N];
void dfs(int u, int Fa) {
  fa[u][0] = Fa;
  dep[u] = dep[Fa] + 1;
  for (int i = 1; i < 20; i++)
    fa[u][i] = fa[fa[u][i - 1]][i - 1],
    mx[u][i] = max(mx[fa[u][i - 1]][i - 1], mx[u][i - 1]);
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].link;
    if (v != Fa) {
      mx[v][0] = e[i].val;
      dfs(v, u);
    }
  }
}
inline int query(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  int delta = dep[u] - dep[v], ans = 0;
  for (int i = 0; i < 20; i++)
    if (delta & (1 << i)) ans = max(ans, mx[u][i]), u = fa[u][i];
  if (u == v) return ans;
  for (int i = 19; i >= 0; i--)
    if (fa[u][i] != fa[v][i])
      ans = max(ans, max(mx[v][i], mx[u][i])), u = fa[u][i], v = fa[v][i];
  return max(ans, max(mx[v][0], mx[u][0]));
}
inline void update(int u, int v, int w) {
  if (dep[u] < dep[v]) swap(u, v);
  for (int i = 19; i >= 0; i--)
    if (dep[fa[u][i]] >= dep[v]) mn[u][i] = min(mn[u][i], w), u = fa[u][i];
  if (u == v) return;
  for (int i = 19; i >= 0; i--)
    if (fa[u][i] != fa[v][i])
      mn[u][i] = min(mn[u][i], w), mn[v][i] = min(mn[v][i], w), u = fa[u][i],
      v = fa[v][i];
  mn[u][0] = min(mn[u][0], w);
  mn[v][0] = min(mn[v][0], w);
}
int ans[N];
inline void solve() {
  kruscal();
  dfs(1, 0);
  memset(mn, 127, sizeof mn);
  for (int i = 1; i <= m; i++)
    if (!mark[i])
      ans[E[i].id] = query(E[i].u, E[i].v) - 1, update(E[i].u, E[i].v, E[i].w);
  for (int i = 19; i; i--)
    for (int j = 1; j <= n; j++)
      mn[j][i - 1] = min(mn[j][i - 1], mn[j][i]),
                mn[fa[j][i - 1]][i - 1] =
                    min(mn[fa[j][i - 1]][i - 1], mn[j][i]);
  for (int i = 1; i <= m; i++)
    if (mark[i])
      ans[E[i].id] =
          ((fa[E[i].u][0] == E[i].v) ? mn[E[i].u][0] : mn[E[i].v][0]) - 1;
  for (int i = 1; i <= m; i++) {
    if (ans[i] >= INF)
      write(-1);
    else
      write(ans[i]);
    putchar(' ');
  }
}
int main() {
  init();
  solve();
  return 0;
}
