#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int x, y, z, f;
} e[200010];
struct P {
  int x, y;
  P() {}
  P(int x = 0, int y = 0) : x(x), y(y) {}
};
vector<P> g[200010];
int i, j, k, n, m, Ans[200010], f[200010][19], mx[200010][19], mn[200010][19],
    F[200010], d[200010], f1, f2;
bool b[200010];
inline int Max(int x, int y) { return x < y ? y : x; }
inline int Min(int x, int y) { return x < y ? x : y; }
inline bool Cmp(Edge a, Edge b) { return a.z < b.z; }
inline int Find(int x) { return F[x] == x ? x : F[x] = Find(F[x]); }
inline void Dfs(int x, int y) {
  f[x][0] = y;
  d[x] = d[y] + 1;
  for (int i = 0; i < g[x].size(); i++) {
    P Tmp = g[x][i];
    if (Tmp.x != y) {
      mx[Tmp.x][0] = Tmp.y;
      Dfs(Tmp.x, x);
    }
  }
}
inline int Query(int x, int y) {
  int Ans = 0;
  if (d[x] < d[y]) swap(x, y);
  for (int i = 19 - 1; i >= 0; i--)
    if (d[f[x][i]] >= d[y]) Ans = Max(Ans, mx[x][i]), x = f[x][i];
  if (x == y) return Ans;
  for (int i = 19 - 1; i >= 0; i--)
    if (f[x][i] != f[y][i])
      Ans = Max(Ans, Max(mx[x][i], mx[y][i])), x = f[x][i], y = f[y][i];
  return Max(Ans, Max(mx[x][0], mx[y][0]));
}
inline void Update(int x, int y, int z) {
  if (d[x] < d[y]) swap(x, y);
  for (int i = 19 - 1; i >= 0; i--)
    if (d[f[x][i]] >= d[y]) mn[x][i] = Min(mn[x][i], z), x = f[x][i];
  if (x == y) return;
  for (int i = 19 - 1; i >= 0; i--)
    if (f[x][i] != f[y][i])
      mn[x][i] = Min(mn[x][i], z), mn[y][i] = Min(mn[y][i], z), x = f[x][i],
      y = f[y][i];
  mn[x][0] = Min(mn[x][0], z);
  mn[y][0] = Min(mn[y][0], z);
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++)
    scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z), e[i].f = i;
  sort(e + 1, e + m + 1, Cmp);
  for (i = 1; i <= n; i++) F[i] = i;
  for (i = 1; i <= m; i++) {
    f1 = Find(e[i].x);
    f2 = Find(e[i].y);
    if (f1 == f2) continue;
    b[i] = 1;
    F[f1] = f2;
    g[e[i].x].push_back(P(e[i].y, e[i].z));
    g[e[i].y].push_back(P(e[i].x, e[i].z));
  }
  Dfs(1, 0);
  for (j = 1; j < 19; j++)
    for (i = 1; i <= n; i++)
      f[i][j] = f[f[i][j - 1]][j - 1],
      mx[i][j] = Max(mx[i][j - 1], mx[f[i][j - 1]][j - 1]);
  memset(mn, 127, sizeof(mn));
  for (i = 1; i <= m; i++)
    if (!b[i])
      Ans[e[i].f] = Query(e[i].x, e[i].y) - 1, Update(e[i].x, e[i].y, e[i].z);
  for (j = 19 - 1; j; j--)
    for (i = 1; i <= n; i++)
      mn[i][j - 1] = Min(mn[i][j - 1], mn[i][j]),
                mn[f[i][j - 1]][j - 1] = Min(mn[f[i][j - 1]][j - 1], mn[i][j]);
  for (i = 1; i <= m; i++)
    if (b[i])
      Ans[e[i].f] =
          (f[e[i].x][0] == e[i].y ? mn[e[i].x][0] : mn[e[i].y][0]) - 1;
  for (i = 1; i <= m; i++)
    if (Ans[i] >= 1e9)
      printf("-1 ");
    else
      printf("%d ", Ans[i]);
  return 0;
}
