#include <bits/stdc++.h>
using namespace std;
struct edge {
  int a, b, c, inx;
  bool operator<(const edge& rhs) const { return c < rhs.c; }
};
struct ed {
  int to, d, inx;
};
vector<edge> mst;
vector<ed> a[200000 + 9];
int fa[200000 + 9];
int p[200000 + 9][21 + 9];
int dpmax[200000 + 9][21 + 9];
int dpmin[200000 + 9][21 + 9];
int dep[200000 + 9];
int ans[200000 + 9];
int fe[200000 + 9];
int findpar(int x) {
  if (x != fa[x]) {
    return fa[x] = findpar(fa[x]);
  }
  return x;
}
void dfs(int u, int prev, int val) {
  p[u][0] = prev;
  dpmax[u][0] = val;
  for (int i = 1; i < 21; i++) {
    if (p[u][i - 1] != -1) {
      p[u][i] = p[p[u][i - 1]][i - 1];
      dpmax[u][i] = max(dpmax[u][i - 1], dpmax[p[u][i - 1]][i - 1]);
    } else {
      p[u][i] = -1;
      dpmax[u][i] = 0;
    }
  }
  for (auto v : a[u]) {
    if (v.to == prev) {
      continue;
    }
    dep[v.to] = dep[u] + 1;
    fe[v.to] = v.inx;
    dfs(v.to, u, v.d);
  }
}
int lca(int u, int v) {
  if (dep[u] < dep[v]) {
    swap(u, v);
  }
  for (int i = 21 - 1; i >= 0; i--) {
    if (dep[u] - (1 << i) >= dep[v]) {
      u = p[u][i];
    }
  }
  if (u == v) {
    return u;
  }
  for (int i = 21 - 1; i >= 0; i--) {
    if (p[u][i] != -1 && p[u][i] != p[v][i]) {
      u = p[u][i], v = p[v][i];
    }
  }
  return p[u][0];
}
int doit(int u, int v, int val) {
  int ans = 0;
  for (int i = 21 - 1; i >= 0; i--) {
    if (dep[u] - (1 << i) >= dep[v]) {
      dpmin[u][i] = min(dpmin[u][i], val);
      ans = max(ans, dpmax[u][i]);
      u = p[u][i];
    }
  }
  return ans - 1;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    mst.push_back({x, y, z, i});
  }
  sort(mst.begin(), mst.end());
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int X = findpar(mst[i].a);
    int Y = findpar(mst[i].b);
    if (X != Y) {
      a[mst[i].a].push_back({mst[i].b, mst[i].c, mst[i].inx});
      a[mst[i].b].push_back({mst[i].a, mst[i].c, mst[i].inx});
      mst[i].inx = 0;
      fa[X] = Y;
    }
  }
  memset(dpmin, 127, sizeof(dpmin));
  dfs(1, -1, 0);
  int LCA;
  for (int i = 0; i < m; i++) {
    if (!mst[i].inx) {
      continue;
    }
    int u = mst[i].a, v = mst[i].b;
    LCA = lca(u, v);
    ans[mst[i].inx] = max(doit(u, LCA, mst[i].c), doit(v, LCA, mst[i].c));
  }
  for (int i = 21 - 1; i >= 0; i--) {
    for (int j = 1; j <= n; j++) {
      dpmin[j][i - 1] = min(dpmin[j][i - 1], dpmin[j][i]);
      dpmin[p[j][i - 1]][i - 1] = min(dpmin[p[j][i - 1]][i - 1], dpmin[j][i]);
    }
  }
  for (int i = 1; i <= n; i++) {
    ans[fe[i]] = dpmin[i][0] - 1;
  }
  for (int i = 1; i <= m; i++) {
    printf("%d ", (ans[i] > 1000000000) ? -1 : ans[i]);
  }
  return 0;
}
