#include <bits/stdc++.h>
using namespace std;
int n, m, num, opt;
int f[200010], mxlen[200010][20], mnlen[200010][20];
int dep[200010], fa[200010][20], ans[200010];
bool use[200010], vis[200010];
vector<int> v[200010];
vector<int> d[200010];
struct Edge {
  int from, to, dis, id;
  bool friend operator<(Edge a, Edge b) { return a.dis < b.dis; }
} e[200010 << 1];
int find(int x) { return (f[x] == x) ? x : f[x] = find(f[x]); }
void kruskal(int n, int m) {
  int cnt = 0;
  memset(use, 0, sizeof(use));
  for (int i = 1; i <= n; ++i) f[i] = i;
  for (int i = 1; i <= m; ++i) {
    int from = e[i].from, to = e[i].to;
    int fa = find(from), fb = find(to);
    if (fa != fb) {
      cnt++;
      use[i] = 1;
      v[from].push_back(to);
      v[to].push_back(from);
      d[from].push_back(e[i].dis);
      d[to].push_back(e[i].dis);
      if (cnt == n - 1) return;
      f[fa] = fb;
    }
  }
}
void dfs(int u, int step) {
  vis[u] = 1;
  dep[u] = step;
  for (int i = 0; i < v[u].size(); ++i) {
    int to = v[u][i];
    if (!vis[to]) {
      fa[to][0] = u;
      mxlen[to][0] = d[u][i];
      dfs(to, step + 1);
    }
  }
}
int getmaxlen(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int ans = -0x3f3f3f3f;
  for (int i = 18; i >= 0; --i) {
    if (dep[fa[x][i]] >= dep[y]) {
      ans = max(ans, mxlen[x][i]);
      x = fa[x][i];
    }
  }
  if (x == y) return ans;
  for (int i = 18; i >= 0; --i) {
    if (fa[x][i] != fa[y][i]) {
      ans = max(ans, max(mxlen[x][i], mxlen[y][i]));
      x = fa[x][i];
      y = fa[y][i];
    }
  }
  ans = max(ans, max(mxlen[x][0], mxlen[y][0]));
  return ans;
}
void update(int x, int y, int val) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 18; i >= 0; --i) {
    if (dep[fa[x][i]] >= dep[y]) {
      mnlen[x][i] = min(val, mnlen[x][i]);
      x = fa[x][i];
    }
  }
  if (x == y) return;
  for (int i = 18; i >= 0; --i) {
    if (fa[x][i] != fa[y][i]) {
      mnlen[x][i] = min(mnlen[x][i], val);
      mnlen[y][i] = min(mnlen[y][i], val);
      x = fa[x][i];
      y = fa[y][i];
    }
  }
  mnlen[x][0] = min(mnlen[x][0], val);
  mnlen[y][0] = min(mnlen[y][0], val);
}
int main() {
  num = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &e[i].from, &e[i].to, &e[i].dis);
    e[i].id = i;
  }
  sort(e + 1, e + m + 1);
  kruskal(n, m);
  memset(vis, 0, sizeof(vis));
  fa[1][0] = 0;
  dep[0] = -1;
  dfs(1, 0);
  for (int j = 1; j <= 18; ++j)
    for (int i = 1; i <= n; ++i) {
      fa[i][j] = fa[fa[i][j - 1]][j - 1];
      mxlen[i][j] = max(mxlen[i][j - 1], mxlen[fa[i][j - 1]][j - 1]);
    }
  memset(ans, 0x3f3f3f3f, sizeof(ans));
  memset(mnlen, 0x3f3f3f3f, sizeof(mnlen));
  for (int i = 1; i <= m; ++i) {
    if (!use[i]) {
      ans[e[i].id] = getmaxlen(e[i].from, e[i].to) - 1;
      update(e[i].from, e[i].to, e[i].dis - 1);
    }
  }
  for (int j = 17; j >= 0; --j)
    for (int i = 1; i <= n; ++i) {
      mnlen[i][j] = min(mnlen[i][j], mnlen[i][j + 1]);
      mnlen[fa[i][j]][j] = min(mnlen[fa[i][j]][j], mnlen[i][j + 1]);
    }
  for (int i = 1; i <= m; ++i) {
    if (use[i]) {
      if (fa[e[i].from][0] == e[i].to)
        ans[e[i].id] = mnlen[e[i].from][0];
      else
        ans[e[i].id] = mnlen[e[i].to][0];
    }
  }
  for (int i = 1; i <= m; ++i) {
    printf("%d ", ans[i] == 0x3f3f3f3f ? -1 : ans[i]);
  }
  puts("");
  return 0;
}
