#include <bits/stdc++.h>
using namespace std;
int n, m;
struct Edge {
  int x, y, second, idx;
  bool operator<(const Edge &ot) const { return second < ot.second; }
} E[200002], nonMST[200002];
vector<int> V[200002];
bool onMst[200002];
pair<int, int> anc[200002][20];
int M, r, f[200002], lev[200002], Idx[200002];
int ans[200002];
int root(int x) {
  if (!f[x]) return x;
  return f[x] = root(f[x]);
}
void findMST() {
  for (int i = 1; i <= m; ++i)
    if (root(E[i].x) != root(E[i].y)) {
      onMst[E[i].idx] = i;
      f[root(E[i].y)] = root(E[i].x);
      V[E[i].x].push_back(E[i].y);
      V[E[i].y].push_back(E[i].x);
    } else
      nonMST[++M] = E[i];
  lev[r = 1] = 1;
}
void dfs(int nod) {
  for (int son : V[nod])
    if (!lev[son]) {
      lev[son] = lev[nod] + 1;
      dfs(son);
    }
}
void compAnc() {
  dfs(1);
  for (int i = 1; i <= m; ++i)
    if (onMst[E[i].idx]) {
      if (lev[E[i].x] > lev[E[i].y]) swap(E[i].x, E[i].y);
      anc[E[i].y][0] = {E[i].x, E[i].second};
      Idx[E[i].y] = E[i].idx;
    }
  for (int j = 1; j < 20; ++j)
    for (int i = 1; i <= n; ++i)
      anc[i][j] = pair<int, int>{
          anc[anc[i][j - 1].first][j - 1].first,
          max(anc[i][j - 1].second, anc[anc[i][j - 1].first][j - 1].second)};
}
int maxWPath(int x, int y) {
  if (lev[y] > lev[x]) swap(x, y);
  int lift = lev[x] - lev[y], ret = 0;
  for (int i = 0; (1 << i) <= lift; ++i)
    if (lift & (1 << i)) {
      ret = max(ret, anc[x][i].second);
      x = anc[x][i].first;
    }
  for (int i = 20 - 1; i >= 0; --i)
    if (anc[x][i].first != anc[y][i].first) {
      ret = max(ret, max(anc[x][i].second, anc[y][i].second));
      x = anc[x][i].first;
      y = anc[y][i].first;
    }
  if (x == y) return ret;
  ret = max(ret, max(anc[x][0].second, anc[y][0].second));
  return ret;
}
void compAns() {
  memset(f, 0, sizeof(f));
  for (int i = 1; i <= M; ++i) {
    int x = nonMST[i].x, y = nonMST[i].y;
    ans[nonMST[i].idx] = maxWPath(x, y) - 1;
    while (x != y) {
      if (lev[x] < lev[y]) swap(x, y);
      if (!f[x]) {
        f[x] = anc[x][0].first;
        ans[Idx[x]] = nonMST[i].second - 1;
      }
      x = root(x);
    }
  }
}
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].second);
    E[i].idx = i;
    ans[i] = -1;
  }
  sort(E + 1, E + m + 1);
  findMST();
  compAnc();
  compAns();
  for (int i = 1; i <= m; ++i) printf("%d ", ans[i]);
  return 0;
}
