#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> W;
vector<vector<pair<int, int>>> G, MST;
vector<pair<int, int>> E;
vector<int> par[20], val[20], val0[20], dep;
vector<int> cost;
void bt(int R) {
  for (auto p : MST[R]) {
    if (par[0][p.first] != -1) continue;
    par[0][p.first] = R;
    for (int k = 1; k < 20; k++)
      par[k][p.first] = par[k - 1][par[k - 1][p.first]];
    dep[p.first] = dep[R] + 1;
    bt(p.first);
  }
}
int main() {
  cin >> N >> M;
  G.resize(N);
  E.resize(M);
  W.resize(M);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v >> W[i];
    G[u - 1].push_back(make_pair(v - 1, i));
    G[v - 1].push_back(make_pair(u - 1, i));
    E[i].first = u - 1;
    E[i].second = v - 1;
  }
  MST.resize(N);
  vector<bool> Ein_mst(M, false);
  vector<bool> in_mst(N, false);
  in_mst[0] = true;
  int last = 0;
  set<pair<int, int>> SE;
  for (int i = 0; i < N - 1; i++) {
    for (auto p : G[last]) SE.insert(make_pair(W[p.second], p.second));
    while (!SE.empty()) {
      if (in_mst[E[begin(SE)->second].first] &&
          in_mst[E[begin(SE)->second].second]) {
        SE.erase(begin(SE));
        continue;
      } else
        break;
    }
    int nxte = begin(SE)->second;
    MST[E[nxte].first].push_back(make_pair(E[nxte].second, nxte));
    MST[E[nxte].second].push_back(make_pair(E[nxte].first, nxte));
    SE.erase(begin(SE));
    Ein_mst[nxte] = true;
    if (!in_mst[E[nxte].first]) {
      in_mst[E[nxte].first] = true;
      last = E[nxte].first;
    } else {
      in_mst[E[nxte].second] = true;
      last = E[nxte].second;
    }
  }
  dep.resize(N, 0);
  for (int i = 0; i < 20; i++) {
    par[i].resize(N, -1);
    val[i].resize(N, 1000000010);
    val0[i].resize(N, 0);
  }
  for (int k = 0; k < 20; k++) par[k][0] = 0;
  cost.resize(M, 0);
  bt(0);
  for (int i = 0; i < M; i++) {
    if (!Ein_mst[i]) continue;
    if (dep[E[i].first] > dep[E[i].second])
      val0[0][E[i].first] = W[i];
    else
      val0[0][E[i].second] = W[i];
  }
  for (int i = 1; i < 20; i++)
    for (int j = 0; j < N; j++)
      val0[i][j] = max(val0[i - 1][j], val0[i - 1][par[i - 1][j]]);
  for (int i = 0; i < M; i++) {
    int u = E[i].first, v = E[i].second;
    if (dep[u] > dep[v]) swap(u, v);
    if (Ein_mst[i]) continue;
    for (int k = 19; k >= 0; k--)
      if (dep[par[k][v]] >= dep[u]) {
        val[k][v] = min(val[k][v], W[i]);
        v = par[k][v];
        if (v == 0) break;
      }
    for (int k = 19; k >= 0; k--)
      if (par[k][v] != par[k][u]) {
        val[k][v] = min(val[k][v], W[i]);
        v = par[k][v];
        val[k][u] = min(val[k][u], W[i]);
        u = par[k][u];
      }
    if (v != u) {
      val[0][v] = min(val[0][v], W[i]);
      v = par[0][v];
      val[0][u] = min(val[0][u], W[i]);
      u = par[0][u];
    }
  }
  for (int k = 19; k > 0; k--)
    for (int j = 0; j < N; j++) {
      val[k - 1][j] = min(val[k - 1][j], val[k][j]);
      val[k - 1][par[k - 1][j]] = min(val[k - 1][par[k - 1][j]], val[k][j]);
    }
  for (int i = 0; i < M; i++) {
    int u = E[i].first, v = E[i].second;
    if (dep[u] > dep[v]) swap(u, v);
    if (Ein_mst[i]) {
      cost[i] = val[0][v];
      if (val[0][v] == 1000000010) cost[i] = 0;
      continue;
    }
    for (int k = 19; k >= 0; k--)
      if (dep[par[k][v]] >= dep[u]) {
        cost[i] = max(cost[i], val0[k][v]);
        v = par[k][v];
      }
    for (int k = 19; k >= 0; k--)
      if (par[k][v] != par[k][u]) {
        cost[i] = max(cost[i], val0[k][v]);
        v = par[k][v];
        cost[i] = max(cost[i], val0[k][u]);
        u = par[k][u];
      }
    if (v != u) {
      cost[i] = max(cost[i], val0[0][v]);
      v = par[0][v];
      cost[i] = max(cost[i], val0[0][u]);
      u = par[0][u];
    }
  }
  for (int i = 0; i < M; i++) cout << cost[i] - 1 << "\n";
}
