#include <bits/stdc++.h>
const int MAXN = 250000 + 10;
const int INF = 0x3f3f3f3f;
struct Edge {
  int u, v, c, id, in;
} edges[MAXN];
struct E {
  int v, w;
  E(int _v, int _w) : v(_v), w(_w) {}
};
struct Node {
  Node* ch[2];
  int imin, omin, fomin;
} nil[4 * MAXN], *ntot, *r;
int n, m;
int f[MAXN];
int ans[MAXN];
std::vector<E> G[MAXN];
int father[MAXN], top[MAXN], ws[MAXN], siz[MAXN];
int pos[MAXN], tpos[MAXN], wstpos[MAXN], dep[MAXN], tot;
bool cmp(Edge p, Edge q) { return p.c < q.c; }
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
Node* New() {
  ++ntot;
  ntot->ch[0] = ntot->ch[1] = nil;
  ntot->omin = INF;
  ntot->imin = 0;
  ntot->fomin = INF;
  return ntot;
}
void update(Node* x) {
  x->imin = std::max(x->ch[0]->imin, x->ch[1]->imin);
  x->omin = std::min(std::min(x->ch[0]->omin, x->ch[1]->omin), x->omin);
}
void insert(Node*& x, int l, int r, int p, int t) {
  if (x == nil) {
    x = New();
  }
  if (l < r) {
    int mid = l + r >> 1;
    if (p <= mid)
      insert(x->ch[0], l, mid, p, t);
    else
      insert(x->ch[1], mid + 1, r, p, t);
    update(x);
  } else {
    x->imin = t;
  }
}
int dfs(int x) {
  siz[x] = 1;
  for (int i = 0; i < G[x].size(); ++i) {
    if (G[x][i].v != father[x]) {
      dep[G[x][i].v] = dep[x] + 1;
      father[G[x][i].v] = x;
      siz[x] += dfs(G[x][i].v);
      if (siz[G[x][i].v] > siz[ws[x]]) {
        ws[x] = G[x][i].v;
      }
    }
  }
  return siz[x];
}
void dfs2(int x) {
  top[x] = x == ws[father[x]] ? top[father[x]] : x;
  pos[x] = ++tot;
  insert(r, 1, 2 * n, tot, 0);
  for (int i = 0; i < G[x].size(); ++i) {
    if (G[x][i].v == ws[x]) {
      tpos[G[x][i].v] = ++tot;
      wstpos[x] = tot;
      insert(r, 1, 2 * n, tot, G[x][i].w);
      dfs2(G[x][i].v);
      break;
    }
  }
  for (int i = 0; i < G[x].size(); ++i) {
    if (G[x][i].v != ws[x] && G[x][i].v != father[x]) {
      tpos[G[x][i].v] = ++tot;
      insert(r, 1, 2 * n, tot, G[x][i].w);
      dfs2(G[x][i].v);
    }
  }
}
void modify(Node*& x, int l, int r, int cl, int cr, int t) {
  if (x == nil) {
    x = New();
  }
  if (cl <= l && cr >= r) {
    x->fomin = std::min(x->fomin, t);
    x->omin = std::min(x->fomin, x->omin);
    return;
  }
  int mid = l + r >> 1;
  if (cl <= mid) {
    modify(x->ch[0], l, mid, cl, cr, t);
  }
  if (cr > mid) {
    modify(x->ch[1], mid + 1, r, cl, cr, t);
  }
  update(x);
}
int qimin(Node* x, int l, int r, int cl, int cr) {
  if (x == nil) {
    x = New();
  }
  if (cl <= l && cr >= r) {
    return x->imin;
  }
  int mid = l + r >> 1;
  int rans = 0;
  if (cl <= mid) {
    rans = std::max(qimin(x->ch[0], l, mid, cl, cr), rans);
  }
  if (cr > mid) {
    rans = std::max(qimin(x->ch[1], mid + 1, r, cl, cr), rans);
  }
  return rans;
}
int qomin(Node* x, int l, int r, int cl, int cr) {
  if (x == nil) {
    x = New();
  }
  if (cl <= l && cr >= r) {
    return std::min(x->omin, x->fomin);
  }
  int mid = l + r >> 1;
  int rans = INF;
  if (cl <= mid) {
    rans = std::min(qomin(x->ch[0], l, mid, cl, cr), rans);
  }
  if (cr > mid) {
    rans = std::min(qomin(x->ch[1], mid + 1, r, cl, cr), rans);
  }
  rans = std::min(x->fomin, rans);
  return rans;
}
int query(int u, int v, int op) {
  int ans = op ? INF : 0;
  while (top[u] != top[v]) {
    int& x = dep[top[u]] > dep[top[v]] ? u : v;
    if (!op)
      ans = std::max(qimin(r, 1, 2 * n, tpos[top[x]], pos[x]), ans);
    else {
      ans = std::min(qomin(r, 1, 2 * n, tpos[top[x]], tpos[x]), ans);
    }
    x = father[top[x]];
  }
  if (dep[u] > dep[v]) std::swap(u, v);
  if (!op)
    ans = std::max(qimin(r, 1, 2 * n, pos[u], pos[v]), ans);
  else if (u != v)
    ans = std::min(qomin(r, 1, 2 * n, wstpos[u], tpos[v]), ans);
  return ans;
}
void ch(int u, int v, int w) {
  while (top[u] != top[v]) {
    int& x = dep[top[u]] > dep[top[v]] ? u : v;
    modify(r, 1, 2 * n, tpos[top[x]], tpos[x], w);
    x = father[top[x]];
  }
  if (dep[u] > dep[v]) std::swap(u, v);
  if (u != v) modify(r, 1, 2 * n, wstpos[u], tpos[v], w);
}
int main() {
  r = ntot = nil->ch[0] = nil->ch[1] = nil;
  nil->omin = nil->fomin = INF;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &edges[i].u, &edges[i].v, &edges[i].c);
    edges[i].id = i;
  }
  std::sort(edges + 1, edges + 1 + m, cmp);
  for (int i = 1; i <= n; ++i) {
    f[i] = i;
  }
  for (int i = 1, u, v; i <= m; ++i) {
    u = edges[i].u;
    v = edges[i].v;
    if (find(u) != find(v)) {
      f[f[u]] = f[v];
      G[u].push_back(E(v, edges[i].c));
      G[v].push_back(E(u, edges[i].c));
      edges[i].in = 1;
    }
  }
  dfs(1);
  dfs2(1);
  for (int i = 1; i <= m; ++i) {
    if (!edges[i].in) {
      ch(edges[i].u, edges[i].v, edges[i].c);
    }
  }
  for (int i = 1; i <= m; ++i) {
    if (!edges[i].in) {
      ans[edges[i].id] = query(edges[i].u, edges[i].v, 0);
    } else {
      ans[edges[i].id] = query(edges[i].u, edges[i].v, 1);
    }
  }
  for (int i = 1; i <= m; ++i) {
    if (ans[i] == INF) ans[i] = 0;
    printf("%d ", ans[i] - 1);
  }
  return 0;
}
