#include <bits/stdc++.h>
using namespace std;
const int N = 200010, M = 19, INF = 1e9;
struct Edge {
  int x, y, z, f;
};
Edge e[N];
struct P {
  int x, y;
  P(int x = 0, int y = 0) : x(x), y(y) {}
};
vector<P> g[N];
int n, m, Ans[N], f[N][M], mx[N][M], mn[N][M], F[N], d[N], f1, f2;
bool b[N];
bool Cmp(Edge a, Edge b) { return a.z < b.z; }
int Find(int x) { return F[x] == x ? x : F[x] = Find(F[x]); }
void Dfs(int x, int y) {
  f[x][0] = y;
  d[x] = d[y] + 1;
  for (auto Tmp : g[x])
    if (Tmp.x != y) {
      mx[Tmp.x][0] = Tmp.y;
      Dfs(Tmp.x, x);
    }
}
int Query(int x, int y) {
  int Ans = 0;
  if (d[x] < d[y]) swap(x, y);
  for (int i = M - 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 = M - 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]));
}
void Update(int x, int y, int z) {
  if (d[x] < d[y]) swap(x, y);
  for (int i = M - 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 = M - 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 (int 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 (int i = 1; i <= n; i++) F[i] = i;
  for (int 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 (int j = 1; j < M; j++)
    for (int 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 (int 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 (int j = M - 1; j; j--)
    for (int 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 (int i = 1; i <= m; i++)
    if (b[i]) {
      if (f[e[i].x][0] == e[i].y)
        Ans[e[i].f] = mn[e[i].x][0] - 1;
      else
        Ans[e[i].f] = mn[e[i].y][0] - 1;
    }
  for (int i = 1; i <= m; i++)
    if (Ans[i] >= INF)
      printf("-1 ");
    else
      printf("%d ", Ans[i]);
  return 0;
}
