#include <bits/stdc++.h>
using namespace std;
template <typename TH>
void _dbg(const char* sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <typename TH, typename... TA>
void _dbg(const char* sdbg, TH h, TA... t) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, t...);
}
const int maxn = 2e5 + 5;
const int inf = 1e9 + 5;
const int logn = 18;
struct edge {
  int u, v, c, id;
} e[maxn];
int res[maxn], n, m, id[maxn];
int lab[maxn];
int finds(int u) { return lab[u] < 0 ? u : lab[u] = finds(lab[u]); }
bool Merge(int u, int v) {
  u = finds(u);
  v = finds(v);
  if (u == v) return 0;
  if (lab[u] > lab[v]) swap(u, v);
  lab[u] += lab[v];
  lab[v] = u;
  return 1;
}
void enter() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> e[i].u >> e[i].v >> e[i].c;
    e[i].id = i;
  }
  sort(e + 1, e + m + 1,
       [](const edge& x, const edge& y) { return x.c < y.c; });
}
bool inMST[maxn];
vector<int> adj[maxn];
int P[maxn][logn], Max[maxn][logn];
int Min[maxn][logn], h[maxn];
void dfs(int u, int par) {
  for (int c : adj[u]) {
    int next = e[c].u + e[c].v - u;
    if (next == par) continue;
    P[next][0] = u;
    for (int i = 1; i < logn; ++i) P[next][i] = P[P[next][i - 1]][i - 1];
    Max[next][0] = e[c].c;
    for (int i = 1; i < logn; ++i) {
      Max[next][i] = max(Max[next][i - 1], Max[P[next][i - 1]][i - 1]);
    }
    h[next] = h[u] + 1;
    id[next] = e[c].id;
    dfs(next, u);
  }
}
int lca(int u, int v) {
  if (h[u] < h[v]) swap(u, v);
  int k = h[u] - h[v];
  for (int i = 0; i < logn; ++i)
    if ((k >> i) & 1) u = P[u][i];
  if (u == v) return u;
  for (int i = logn - 1; i >= 0; --i)
    if (P[u][i] != P[v][i]) u = P[u][i], v = P[v][i];
  return P[u][0];
}
int queryMax(int u, int v) {
  int res = 0;
  if (h[u] < h[v]) swap(u, v);
  int k = h[u] - h[v];
  for (int i = 0; i < logn; ++i)
    if ((k >> i) & 1) res = max(res, Max[u][i]), u = P[u][i];
  if (u == v) return res;
  for (int i = logn - 1; i >= 0; --i)
    if (P[u][i] != P[v][i])
      res = max(res, Max[u][i]), res = max(res, Max[v][i]), u = P[u][i],
      v = P[v][i];
  res = max(res, Max[u][0]);
  res = max(res, Max[v][0]);
  return res;
}
void update(int u, int v, int val) {
  int k = h[u] - h[v];
  for (int i = 0; i < logn; ++i) {
    if ((k >> i) & 1) {
      Min[u][i] = min(Min[u][i], val);
      u = P[u][i];
    }
  }
}
void solve() {
  fill_n(lab, maxn, -1);
  for (int i = 1; i <= m; ++i) {
    if (Merge(e[i].u, e[i].v)) {
      adj[e[i].u].push_back(i);
      adj[e[i].v].push_back(i);
      (e[i].u, e[i].v, e[i].c);
      inMST[i] = 1;
    }
  }
  dfs(1, -1);
  fill_n(&Min[0][0], maxn * logn, inf);
  for (int i = 1; i <= m; ++i) {
    if (inMST[i] == 0) {
      res[e[i].id] = queryMax(e[i].u, e[i].v) - 1;
      int LCA = lca(e[i].u, e[i].v);
      update(e[i].u, LCA, e[i].c);
      update(e[i].v, LCA, e[i].c);
    } else
      (e[i].id);
  }
  for (int i = logn - 1; i > 0; --i) {
    for (int j = 1; j <= n; ++j) {
      Min[j][i - 1] = min(Min[j][i], Min[j][i - 1]);
      Min[P[j][i - 1]][i - 1] = min(Min[j][i], Min[P[j][i - 1]][i - 1]);
    }
  }
  for (int j = 2; j <= n; ++j) {
    res[id[j]] = Min[j][0] - 1;
  }
  for (int i = 1; i <= m; ++i) {
    if (res[i] > 1e9)
      cout << -1 << " ";
    else
      cout << res[i] << " ";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  enter();
  solve();
}
