#include <bits/stdc++.h>
using namespace std;
const int N = 200055;
const int M = 200055;
const int inf = 0x3f3f3f3f;
vector<int> NT, to[N], W[N];
struct edge {
  int u, v, w, id;
} E[M];
bool cmp(const edge &A, const edge &B) { return A.w < B.w; }
bool cmpid(const edge &A, const edge &B) { return A.id < B.id; }
int n, m, xx;
int fa[N], son[N], seq[N], dfn[N], pl[N], size[N], dep[N], top[N], inc = 0,
                                                                   w[N], ans[N];
struct node {
  int ls, rs;
  int mx, flag;
} tr[2 * N];
int root, tail = 0;
int getfa(int x) {
  if (pl[x] == x)
    return pl[x];
  else
    return pl[x] = getfa(pl[x]);
}
void dfs(int u, int f) {
  dep[u] = dep[f] + 1;
  fa[u] = f;
  size[u] = 1;
  int sz = to[u].size();
  for (int i = 0; i < sz; i++) {
    int v = to[u][i];
    if (v == f) continue;
    w[v] = W[u][i];
    dfs(v, u);
    size[u] += size[v];
    if (size[v] > size[son[u]]) son[u] = v;
  }
}
void gettp(int u, int f, int tp) {
  top[u] = tp;
  inc++;
  seq[inc] = u;
  dfn[u] = inc;
  if (son[u]) gettp(son[u], u, tp);
  int sz = to[u].size();
  for (int i = 0; i < sz; i++) {
    int v = to[u][i];
    if (v == f || v == son[u]) continue;
    gettp(v, u, v);
  }
}
void update(int nd) {
  int ls = tr[nd].ls;
  int rs = tr[nd].rs;
  tr[nd].mx = max(tr[ls].mx, tr[rs].mx);
}
void build(int &nd, int lf, int rg) {
  nd = ++tail;
  tr[nd].flag = inf;
  if (lf == rg) {
    tr[nd].mx = E[w[seq[lf]]].w;
    return;
  }
  int mid = (lf + rg) >> 1;
  build(tr[nd].ls, lf, mid);
  build(tr[nd].rs, mid + 1, rg);
  update(nd);
}
int modify(int nd, int lf, int rg, int L, int R, int val) {
  if (lf > rg) return -inf;
  if (L <= lf && rg <= R) {
    tr[nd].flag = min(val, tr[nd].flag);
    return tr[nd].mx;
  }
  int mid = (lf + rg) >> 1;
  int ret = -inf;
  if (L <= mid) ret = max(ret, modify(tr[nd].ls, lf, mid, L, R, val));
  if (R > mid) ret = max(ret, modify(tr[nd].rs, mid + 1, rg, L, R, val));
  return ret;
}
int modify(int u, int v, int ww) {
  int ret = -inf;
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]]) swap(u, v);
    ret = max(ret, modify(root, 1, n, dfn[top[u]], dfn[u], ww));
    u = fa[top[u]];
  }
  if (dep[u] < dep[v]) swap(u, v);
  ret = max(ret, modify(root, 1, n, dfn[son[v]], dfn[u], ww));
  return ret;
}
int query(int nd, int lf, int rg, int pos) {
  if (lf == rg) return tr[nd].flag;
  int mid = (lf + rg) >> 1;
  int ret = tr[nd].flag;
  if (pos <= mid)
    ret = min(ret, query(tr[nd].ls, lf, mid, pos));
  else
    ret = min(ret, query(tr[nd].rs, mid + 1, rg, pos));
  return ret;
}
void getans(int u, int f) {
  int sz = to[u].size();
  for (int i = 0; i < sz; i++) {
    int v = to[u][i];
    if (v == f) continue;
    ans[w[v]] = query(root, 1, n, dfn[v]) - 1;
    getans(v, u);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w);
    E[i].id = i;
  }
  sort(E + 1, E + m + 1, cmp);
  for (int i = 1; i <= n; i++) pl[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = E[i].u;
    int v = E[i].v;
    int fx = getfa(u);
    int fy = getfa(v);
    if (fx != fy) {
      pl[fx] = fy;
      to[u].push_back(v);
      to[v].push_back(u);
      W[u].push_back(E[i].id);
      W[v].push_back(E[i].id);
    } else {
      NT.push_back(E[i].id);
    }
  }
  sort(E + 1, E + m + 1, cmpid);
  dfs(1, 1);
  gettp(1, 1, 1);
  build(root, 1, n);
  int sz = NT.size();
  for (int i = 0; i < sz; i++) {
    int tmp = NT[i];
    int u = E[tmp].u;
    int v = E[tmp].v;
    ans[E[tmp].id] = modify(u, v, E[tmp].w) - 1;
  }
  getans(1, 1);
  for (int i = 1; i <= m; i++)
    if (ans[i] >= inf - 1)
      printf("-1 ");
    else
      printf("%d ", ans[i]);
  return 0;
}
