#include <bits/stdc++.h>
using namespace std;
const int maxn = 2E5 + 77;
const int log2maxn = 20;
const int INF = 1E9 + 77;
struct SEG {
  SEG *ch[2];
  int v;
  SEG() {
    ch[0] = ch[1] = NULL;
    v = INF;
  }
} * segs[maxn];
void segSetVal(SEG *&tr, int L, int R, int qL, int qR, int v) {
  if (!tr) tr = new SEG;
  if (L == qL && R == qR) {
    tr->v = min(tr->v, v);
    return;
  }
  int M = L + ((R - L) >> 1);
  if (qR <= M)
    segSetVal(tr->ch[0], L, M, qL, qR, v);
  else if (qL > M)
    segSetVal(tr->ch[1], M + 1, R, qL, qR, v);
  else {
    segSetVal(tr->ch[0], L, M, qL, M, v);
    segSetVal(tr->ch[1], M + 1, R, M + 1, qR, v);
  }
}
int segQuery(SEG *tr, int L, int R, int p) {
  if (!tr) return INF;
  if (L == R) return tr->v;
  int M = L + ((R - L) >> 1);
  if (p <= M)
    return min(tr->v, segQuery(tr->ch[0], L, M, p));
  else
    return min(tr->v, segQuery(tr->ch[1], M + 1, R, p));
}
struct REDGE {
  int u, v, w, id;
  bool inMst;
  void read(int i) {
    scanf("%d%d%d", &u, &v, &w);
    id = i;
    inMst = false;
  }
  bool operator<(const REDGE &o) const { return w < o.w; }
} redges[maxn];
struct EDGE {
  int v, w, id, next;
} edges[maxn * 2];
int cntEdges = 1, head[maxn];
void addEdge(int from, int to, int w, int id) {
  edges[cntEdges] = (EDGE){to, w, id, head[from]};
  head[from] = cntEdges++;
}
int pa[maxn];
int getPa(int x) {
  if (x != pa[x]) pa[x] = getPa(pa[x]);
  return pa[x];
}
int N, M;
int size[maxn], edgeNo[maxn], H[maxn], ff[maxn][log2maxn], mw[maxn][log2maxn];
void dfs1(int u, int f) {
  size[u] = 1;
  for (int k = head[u]; k; k = edges[k].next) {
    int v = edges[k].v;
    if (v == f) continue;
    H[v] = H[u] + 1;
    ff[v][0] = u;
    mw[v][0] = edges[k].w;
    for (int i = 1; i < log2maxn; ++i) {
      ff[v][i] = ff[ff[v][i - 1]][i - 1];
      mw[v][i] = max(mw[v][i - 1], mw[ff[v][i - 1]][i - 1]);
    }
    edgeNo[v] = edges[k].id;
    dfs1(v, u);
    size[u] += size[v];
  }
}
int segNo[maxn], segIdx[maxn], segSize[maxn], cntSegs = 0, fa[maxn];
void dfs2(int u, int f) {
  int ms = -1, mv = 0;
  for (int k = head[u]; k; k = edges[k].next) {
    int v = edges[k].v;
    if (v == f) continue;
    if (size[v] > ms) {
      ms = size[v];
      mv = v;
    }
  }
  if (!mv) return;
  segNo[mv] = segNo[u];
  segIdx[mv] = ++segSize[segNo[u]];
  fa[mv] = fa[u];
  dfs2(mv, u);
  for (int k = head[u]; k; k = edges[k].next) {
    int v = edges[k].v;
    if (v == f || v == mv) continue;
    segNo[v] = ++cntSegs;
    segIdx[v] = ++segSize[cntSegs];
    fa[v] = u;
    dfs2(v, u);
  }
}
int ans[maxn];
int query1(int u, int v) {
  int ret = -1;
  if (H[u] < H[v]) {
    swap(u, v);
  }
  for (int i = log2maxn - 1; i >= 0; --i)
    if (H[ff[u][i]] >= H[v]) {
      ret = max(ret, mw[u][i]);
      u = ff[u][i];
    }
  if (u == v) return ret;
  for (int i = log2maxn - 1; i >= 0; --i)
    if (ff[u][i] != ff[v][i]) {
      ret = max(ret, mw[u][i]);
      ret = max(ret, mw[v][i]);
      u = ff[u][i];
      v = ff[v][i];
    }
  ret = max(ret, mw[u][0]);
  ret = max(ret, mw[v][0]);
  return ret;
}
void solve(int u, int v, int w) {
  while (segNo[u] != segNo[v]) {
    if (H[fa[u]] < H[fa[v]]) swap(u, v);
    segSetVal(segs[segNo[u]], 1, segSize[segNo[u]], 1, segIdx[u], w);
    u = fa[u];
  }
  if (H[u] > H[v]) swap(u, v);
  if (u != v)
    segSetVal(segs[segNo[u]], 1, segSize[segNo[u]], segIdx[u] + 1, segIdx[v],
              w);
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; ++i) {
    redges[i].read(i);
  }
  sort(redges, redges + M);
  for (int i = 1; i <= N; ++i) pa[i] = i;
  for (int i = 0; i < M; ++i) {
    int u = redges[i].u, v = redges[i].v, w = redges[i].w, pu = getPa(u),
        pv = getPa(v);
    if (pu == pv) continue;
    redges[i].inMst = true;
    pa[pv] = pu;
    addEdge(u, v, w, redges[i].id);
    addEdge(v, u, w, redges[i].id);
  }
  H[1] = 1;
  dfs1(1, 0);
  segNo[1] = 0;
  segIdx[1] = 1;
  segSize[0] = 1;
  dfs2(1, 0);
  for (int i = 0; i < M; ++i)
    if (!redges[i].inMst) {
      int u = redges[i].u, v = redges[i].v, w = redges[i].w, id = redges[i].id;
      ans[id] = query1(u, v);
      solve(u, v, w);
    }
  for (int i = 2; i <= N; ++i) {
    ans[edgeNo[i]] = segQuery(segs[segNo[i]], 1, segSize[segNo[i]], segIdx[i]);
  }
  for (int i = 0; i < M; ++i)
    if (ans[i] >= INF)
      ans[i] = -1;
    else
      --ans[i];
  printf("%d", ans[0]);
  for (int i = 1; i < M; ++i) printf(" %d", ans[i]);
  printf("\n");
}
