#include <bits/stdc++.h>
using std::max;
using std::min;
using std::swap;
const int N = 200200;
int n, m;
namespace JS {
int anc[N];
void init(int n) {
  for (int i = 1; i <= n; i++) anc[i] = i;
}
int F(int x) { return x == anc[x] ? x : anc[x] = F(anc[x]); }
void merge(int a, int b) {
  a = F(a), b = F(b);
  anc[a] = b;
}
}  // namespace JS
using namespace JS;
int q1[N], q2[N], cost[N], I[N], use[N];
int info[N], x[2 * N], to[2 * N], w[2 * N], ec;
int cmp(int a, int b) { return cost[a] < cost[b]; }
void add(int p, int q, int cost) {
  x[++ec] = info[p], to[info[p] = ec] = q, w[ec] = cost;
}
void kruskal() {
  int i, t;
  for (i = 1; i <= m; i++) I[i] = i;
  std::sort(I + 1, I + m + 1, cmp);
  for (i = 1, t = I[i]; i <= m; i++, t = I[i])
    if (F(q1[t]) != F(q2[t]))
      use[t] = 1, merge(q1[t], q2[t]), add(q1[t], q2[t], cost[t]),
      add(q2[t], q1[t], cost[t]);
}
int fa[19][N], mx[19][N], mn[19][N], dep[N];
void dfs(int u, int f) {
  int e, v;
  fa[0][u] = f, dep[u] = dep[f] + 1;
  for (e = info[u], v = to[e]; e; e = x[e], v = to[e])
    if (v != f) mx[0][v] = w[e], dfs(v, u);
}
int query(int p, int q) {
  int ans = 0, i;
  if (dep[p] < dep[q]) swap(p, q);
  for (i = 18; i >= 0; i--)
    if (dep[fa[i][p]] >= dep[q])
      ans = max(mx[i][p], ans), assert(fa[i][p]), assert(mx[i][p]),
      p = fa[i][p];
  if (p == q) return ans;
  for (i = 18; i >= 0; i--)
    if (fa[i][p] != fa[i][q])
      ans = max(ans, max(mx[i][p], mx[i][q])), assert(mx[i][q]),
      assert(mx[i][p]), p = fa[i][p], q = fa[i][q];
  ans = max(ans, max(mx[0][p], mx[0][q]));
  return ans;
}
void update(int p, int q, int val) {
  int i;
  if (dep[p] < dep[q]) swap(p, q);
  for (i = 18; i >= 0; i--)
    if (dep[fa[i][p]] >= dep[q]) mn[i][p] = min(val, mn[i][p]), p = fa[i][p];
  if (p == q) return;
  for (i = 18; i >= 0; i--)
    if (fa[i][p] != fa[i][q])
      mn[i][p] = min(val, mn[i][p]), mn[i][q] = min(val, mn[i][q]),
      p = fa[i][p], q = fa[i][q];
  mn[0][p] = min(mn[0][p], val), mn[0][q] = min(mn[0][q], val);
  return;
}
int main() {
  int i, t, p, q;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) scanf("%d%d%d", q1 + i, q2 + i, cost + i);
  init(n);
  kruskal();
  dfs(1, 0);
  for (i = 1; i <= 18; i++)
    for (p = 1; p <= n; p++)
      fa[i][p] = fa[i - 1][fa[i - 1][p]],
      mx[i][p] = max(mx[i - 1][p], mx[i - 1][fa[i - 1][p]]);
  for (i = 0; i <= 18; i++)
    for (p = 1; p <= n; p++)
      if (fa[i][p]) assert(mx[i][p]);
  memset(mn, 0x3f, sizeof(mn));
  for (i = 1; i <= m; i++)
    if (!use[i]) update(q1[i], q2[i], cost[i]);
  for (i = 18; i >= 1; i--)
    for (p = 1; p <= n; p++)
      mn[i - 1][p] = min(mn[i - 1][p], mn[i][p]),
             mn[i - 1][fa[i - 1][p]] = min(mn[i - 1][fa[i - 1][p]], mn[i][p]);
  for (i = 1; i <= m; i++) {
    p = q1[i], q = q2[i];
    if (use[i]) {
      t = dep[p] > dep[q] ? mn[0][p] - 1 : mn[0][q] - 1;
      printf("%d ", t == 0x3f3f3f3f - 1 ? -1 : t);
      continue;
    }
    t = query(q1[i], q2[i]);
    printf("%d ", t - 1);
  }
  return 0;
}
