#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
const int ff = 11;
long long mod = 1e9 + 7;
double eps = 0.00000001;
double PI = acos(-1);
int n, m, fa[maxn], par[maxn][20], max_w[maxn][20], dep[maxn], in_mst[maxn],
    ans[maxn], r[maxn];
struct edge {
  int u, v, w, id;
} e[maxn];
vector<edge> G[maxn];
bool cmp(edge a, edge b) { return a.w < b.w; }
int query(int x) { return fa[x] == x ? fa[x] : fa[x] = query(fa[x]); }
void dfs(int u, int fa, int d) {
  dep[u] = dep[fa] + 1;
  par[u][0] = fa;
  max_w[u][0] = d;
  for (int i = 1; i <= 18; i++) {
    par[u][i] = par[par[u][i - 1]][i - 1];
    max_w[u][i] = max(max_w[u][i - 1], max_w[par[u][i - 1]][i - 1]);
  }
  for (int i = 0; i < G[u].size(); i++) {
    edge temp_e = G[u][i];
    int v = temp_e.v;
    if (v == fa) continue;
    r[v] = temp_e.id;
    dfs(v, u, temp_e.w);
  }
}
int lca(int x, int y, int &d) {
  d = 0;
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 18; i >= 0; i--) {
    if (dep[par[x][i]] >= dep[y]) {
      d = max(d, max_w[x][i]);
      x = par[x][i];
    }
  }
  if (x == y) return x;
  for (int i = 18; i >= 0; i--) {
    if (par[x][i] != par[y][i]) {
      d = max(d, max(max_w[x][i], max_w[y][i]));
      x = par[x][i];
      y = par[y][i];
    }
  }
  d = max(d, max(max_w[x][0], max_w[y][0]));
  return par[x][0];
}
void slove(int x, int u, int d) {
  x = query(x);
  while (dep[x] > dep[u]) {
    ans[r[x]] = min(ans[r[x]], d);
    int y = query(par[x][0]);
    fa[x] = y;
    x = query(x);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    e[i] = edge{u, v, w, i};
  }
  sort(e + 1, e + 1 + m, cmp);
  memset(in_mst, 0, sizeof(in_mst));
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int x = e[i].u, y = e[i].v;
    int fx = query(x), fy = query(y);
    if (fx != fy) {
      fa[fy] = fx;
      in_mst[e[i].id] = 1;
      G[x].push_back(e[i]);
      edge tmp = e[i];
      swap(e[i].u, e[i].v);
      G[y].push_back(e[i]);
    }
  }
  dfs(1, 0, 0);
  memset(ans, inf, sizeof(ans));
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    if (!in_mst[e[i].id]) {
      int x = e[i].u, y = e[i].v, u = lca(x, y, ans[e[i].id]);
      ans[e[i].id]--;
      slove(x, u, e[i].w - 1);
      slove(y, u, e[i].w - 1);
    }
  }
  for (int i = 1; i <= m; i++) {
    if (ans[i] == inf)
      printf("-1 ");
    else
      printf("%d ", ans[i]);
  }
  cout << endl;
}
