#include <bits/stdc++.h>
using namespace std;
int root[200005];
int pre[200005][20], fapre[200005], maxx[200005][20];
struct UEdge {
  int u, v, dis, no;
  bool friend operator<(UEdge x1, UEdge x2) { return x1.dis < x2.dis; }
} mps[200005];
struct Edge {
  int to, dis, no;
} temp;
int n, m;
vector<Edge> vec[200005];
int deep[200005];
int ans[200005];
int du[200005];
bool life[200005];
int findr(int x) {
  if (root[x] == x)
    return x;
  else
    return root[x] = findr(root[x]);
}
void dfs(int now, int nums, int fa, int dis, int no) {
  deep[now] = nums;
  pre[now][1] = fa;
  fapre[now] = no;
  maxx[now][1] = dis;
  for (int i = 2; pre[pre[now][i - 1]][i - 1]; i++) {
    pre[now][i] = pre[pre[now][i - 1]][i - 1];
    maxx[now][i] = max(maxx[now][i - 1], maxx[pre[now][i - 1]][i - 1]);
  }
  for (int i = 0; i < vec[now].size(); i++) {
    if (vec[now][i].to != fa)
      dfs(vec[now][i].to, nums + 1, now, vec[now][i].dis, vec[now][i].no);
  }
}
int askroot(int u, int v) {
  int wtf, flag;
  if (deep[u] > deep[v]) {
    wtf = u;
    u = v;
    v = wtf;
  }
  while (u != v) {
    flag = 19;
    if (deep[u] == deep[v]) {
      while (flag > 1 && pre[u][flag] == pre[v][flag]) flag--;
      u = pre[u][flag];
      v = pre[v][flag];
    } else {
      while (flag > 0 && deep[pre[v][flag]] < deep[u]) flag--;
      v = pre[v][flag];
    }
  }
  return u;
}
int gg1(int u, int v) {
  int now = 0, flag, wtf;
  wtf = askroot(u, v);
  while (u != wtf) {
    flag = 19;
    while (deep[pre[u][flag]] < deep[wtf]) flag--;
    now = max(now, maxx[u][flag]);
    u = pre[u][flag];
  }
  while (v != wtf) {
    flag = 19;
    while (deep[pre[v][flag]] < deep[wtf]) flag--;
    now = max(now, maxx[v][flag]);
    v = pre[v][flag];
  }
  return now - 1;
}
void go(int x, int rt, int nowans) {
  x = findr(x);
  while (deep[x] > deep[rt]) {
    ans[fapre[x]] = nowans - 1;
    root[x] = pre[x][1];
    x = pre[x][1];
    x = findr(x);
  }
}
void gg2(int u, int v, int nowans) {
  int n0, n1, wtf;
  wtf = askroot(u, v);
  go(u, wtf, nowans);
  go(v, wtf, nowans);
}
int main() {
  int u, v;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) root[i] = i;
  for (int i = 0; i < m; i++) {
    ans[i] = -1;
    mps[i].no = i;
    scanf("%d%d%d", &mps[i].u, &mps[i].v, &mps[i].dis);
  }
  sort(mps, mps + m);
  for (int i = 0; i < m; i++) {
    u = findr(mps[i].u);
    v = findr(mps[i].v);
    if (u != v) {
      root[u] = v;
      temp.dis = mps[i].dis;
      temp.no = mps[i].no;
      temp.to = mps[i].v;
      vec[mps[i].u].push_back(temp);
      temp.to = mps[i].u;
      vec[mps[i].v].push_back(temp);
    } else
      life[mps[i].no] = 1;
  }
  dfs(1, 1, 0, 1000000007, -1);
  for (int i = 0; i < m; i++) {
    if (life[mps[i].no]) ans[mps[i].no] = gg1(mps[i].u, mps[i].v);
  }
  for (int i = 1; i <= n; i++) root[i] = i;
  for (int i = 0; i < m; i++) {
    if (life[mps[i].no]) gg2(mps[i].u, mps[i].v, mps[i].dis);
  }
  for (int i = 0; i < m; i++) printf("%d ", ans[i]);
  return 0;
}
