#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int inf = 0x3f3f3f3f;
struct edge {
  int u, v, w, id;
  bool operator<(const edge &temp) const { return w < temp.w; }
} e[N];
int n, m;
int root[N];
int find(int x) { return x == root[x] ? x : root[x] = find(root[x]); }
vector<edge> G[N];
bool vis[N];
int par[N][20], f[N][20], dep[N];
int r[N];
int ans[N];
void dfs(int u, int fa, int d) {
  dep[u] = dep[fa] + 1;
  par[u][0] = fa;
  f[u][0] = d;
  for (int i = 1; i <= 18; i++) {
    par[u][i] = par[par[u][i - 1]][i - 1];
    f[u][i] = max(f[u][i - 1], f[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, f[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(f[x][i], f[y][i]));
      x = par[x][i];
      y = par[y][i];
    }
  }
  d = max(d, max(f[x][0], f[y][0]));
  return par[x][0];
}
void solve(int x, int u, int d) {
  x = find(x);
  while (dep[x] > dep[u]) {
    ans[r[x]] = min(ans[r[x]], d);
    int y = find(par[x][0]);
    root[x] = y;
    x = find(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);
  for (int i = 1; i <= n; i++) root[i] = i;
  for (int i = 1; i <= m; i++) {
    int x = e[i].u, y = e[i].v;
    int rx = find(x), ry = find(y);
    if (rx != ry) {
      root[ry] = rx;
      vis[i] = 1;
      G[x].push_back(e[i]);
      G[e[i].v].push_back((edge){e[i].v, e[i].u, e[i].w, e[i].id});
    }
  }
  dfs(1, 0, 0);
  memset(ans, inf, sizeof ans);
  for (int i = 1; i <= n; i++) root[i] = i;
  for (int i = 1; i <= m; i++) {
    if (!vis[i]) {
      int x = e[i].u, y = e[i].v, u = lca(x, y, ans[e[i].id]);
      ans[e[i].id]--;
      solve(x, u, e[i].w - 1);
      solve(y, u, e[i].w - 1);
    }
  }
  for (int i = 1; i <= m; i++)
    printf("%d%c", ans[i] == inf ? -1 : ans[i], i == m ? '\n' : ' ');
  return 0;
}
