#include <bits/stdc++.h>
using namespace std;
struct S {
  int u, v, w, ne;
  int dep;
} e[210000 << 1], tree[210000 << 1];
int n, m, fir[210000], fa[210000], sum, sumt, firt[210000], root;
int father[210000][20], maxx[210000][20], minn[210000][20], pan, dep[210000],
    ans[210000];
bool f_tree[210000];
bool cmp(S x, S y) { return x.w < y.w; }
void add(int u, int v, int w, int d) {
  e[++sum].ne = fir[u];
  fir[u] = sum;
  e[sum].v = v;
  e[sum].u = u;
  e[sum].w = w;
  e[sum].dep = d;
}
void add2(int u, int v, int c) {
  tree[++sumt].ne = firt[u];
  firt[u] = sumt;
  tree[sumt].v = v;
  tree[sumt].u = u;
  tree[sumt].w = c;
}
int getfa(int x) {
  if (x != fa[x]) {
    fa[x] = getfa(fa[x]);
    return fa[x];
  }
  return fa[x];
}
void kruskal() {
  sort(e + 1, e + sum + 1, cmp);
  for (int i = 1; i <= sum; i++) {
    int f1 = getfa(e[i].u), f2 = getfa(e[i].v);
    if (f1 != f2) {
      fa[f1] = f2;
      add2(e[i].u, e[i].v, e[i].w);
      add2(e[i].v, e[i].u, e[i].w);
      f_tree[e[i].dep] = true;
    }
  }
}
void dfs(int x, int fath) {
  if (x != root) dep[x] = dep[fath] + 1;
  father[x][0] = fath;
  int st = log2(dep[x]);
  for (int i = 1; i <= st; i++) {
    maxx[x][i] = max(maxx[x][i - 1], maxx[father[x][i - 1]][i - 1]);
    father[x][i] = father[father[x][i - 1]][i - 1];
  }
  for (int i = firt[x]; i; i = tree[i].ne) {
    int to = tree[i].v;
    if (to != fath) {
      maxx[to][0] = tree[i].w;
      dfs(to, x);
    }
  }
}
void modify(int x, int y, int w) {
  if (dep[x] < dep[y]) swap(x, y);
  while (dep[x] != dep[y]) {
    int st = log2(dep[x] - dep[y]);
    minn[x][st] = min(minn[x][st], w);
    x = father[x][st];
  }
  if (x == y) return;
  while (father[x][0] != father[y][0]) {
    int st = log2(dep[x]);
    while (father[x][st] == father[y][st]) st--;
    minn[x][st] = min(minn[x][st], w);
    minn[y][st] = min(minn[y][st], w);
    x = father[x][st];
    y = father[y][st];
  }
  minn[x][0] = min(minn[x][0], w);
  minn[y][0] = min(minn[y][0], w);
}
void search() {
  int st = 19;
  for (int dep = st; dep >= 1; dep--) {
    for (int i = 1; i <= n; i++) {
      minn[i][dep - 1] = min(minn[i][dep - 1], minn[i][dep]);
      minn[father[i][dep - 1]][dep - 1] =
          min(minn[father[i][dep - 1]][dep - 1], minn[i][dep]);
    }
  }
}
int lca(int x, int y) {
  int re = -INT_MAX;
  if (dep[x] < dep[y]) swap(x, y);
  while (dep[x] != dep[y]) {
    int st = log2(dep[x] - dep[y]);
    re = max(re, maxx[x][st]);
    x = father[x][st];
  }
  if (x == y) {
    return re;
  }
  while (father[x][0] != father[y][0]) {
    int st = log2(dep[x]);
    while (father[x][st] == father[y][st]) st--;
    re = max(re, maxx[x][st]);
    re = max(re, maxx[y][st]);
    x = father[x][st];
    y = father[y][st];
  }
  re = max(re, maxx[x][0]);
  re = max(re, maxx[y][0]);
  return re;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    add(u, v, c, i);
    add(v, u, c, i);
  }
  kruskal();
  root = getfa(1);
  dfs(root, 0);
  memset(minn, 0x7f, sizeof(minn));
  pan = minn[0][0];
  for (int i = 1; i <= sum; i++)
    if (!f_tree[e[i].dep]) {
      modify(e[i].u, e[i].v, e[i].w);
    }
  search();
  for (int i = 1; i <= sum; i++) {
    int u = e[i].u, v = e[i].v;
    if (f_tree[e[i].dep]) {
      if (dep[u] < dep[v]) swap(u, v);
      if (minn[u][0] != pan)
        ans[e[i].dep] = minn[u][0] - 1;
      else
        ans[e[i].dep] = -1;
    } else {
      ans[e[i].dep] = lca(u, v) - 1;
    }
  }
  for (int i = 1; i <= m; i++) printf("%d ", ans[i]);
}
