#include <bits/stdc++.h>
using namespace std;
int N, M, ans[200011];
int depth[200011], p[200011][19], largest[200011][19];
vector<vector<int>> adj[200011];
int id[200011];
priority_queue<pair<int, int>> pq[200011];
void merge_pq(int a, int b) {
  if (pq[id[a]].size() > pq[id[b]].size()) swap(a, b);
  while (pq[id[a]].size()) {
    pq[id[b]].push(pq[id[a]].top());
    pq[id[a]].pop();
  }
  id[a] = id[b];
}
bool used[200011];
int edges[200011][3], container[200011];
int find(int x) { return x == p[x][0] ? x : p[x][0] = find(p[x][0]); }
void merge(int a, int b) { p[find(a)][0] = find(b); }
bool comp(int a, int b) { return edges[a][2] < edges[b][2]; }
int lca(int a, int b) {
  if (depth[a] > depth[b]) swap(a, b);
  int diff = depth[b] - depth[a];
  for (int i = 0; i < 19; i++)
    if (diff & (1 << i)) b = p[b][i];
  if (a == b) return a;
  for (int i = 19 - 1; i >= 0; i--) {
    if (p[a][i] != p[b][i]) {
      a = p[a][i];
      b = p[b][i];
    }
  }
  return p[a][0];
}
void dfs(int x, int v) {
  p[x][0] = v;
  depth[x] = depth[v] + 1;
  for (auto e : adj[x]) {
    if (e[1] != v && used[e[2]]) {
      dfs(e[1], x);
      largest[e[1]][0] = e[0];
    }
  }
}
void dfs2(int x, int v) {
  int parentEdge = -1;
  for (auto e : adj[x]) {
    if (e[1] == v)
      parentEdge = e[2];
    else if (used[e[2]]) {
      dfs2(e[1], x);
      merge_pq(x, e[1]);
    } else {
      pq[id[x]].push(make_pair(-e[0], depth[lca(x, e[1])]));
    }
  }
  while (pq[id[x]].size() && pq[id[x]].top().second >= depth[x])
    pq[id[x]].pop();
  if (parentEdge != -1 && pq[id[x]].size())
    ans[parentEdge] = -pq[id[x]].top().first;
}
int main() {
  if (fopen("data.in", "r")) freopen("data.in", "r", stdin);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> edges[i][0] >> edges[i][1] >> edges[i][2];
    edges[i][0]--;
    edges[i][1]--;
    container[i] = i;
    adj[edges[i][0]].push_back({edges[i][2], edges[i][1], i});
    adj[edges[i][1]].push_back({edges[i][2], edges[i][0], i});
  }
  sort(container, container + M, comp);
  for (int i = 0; i < N; i++) p[i][0] = i;
  for (int i = 0; i < M; i++) {
    if (find(edges[container[i]][0]) != find(edges[container[i]][1])) {
      merge(edges[container[i]][0], edges[container[i]][1]);
      used[container[i]] = true;
    }
  }
  dfs(0, 0);
  for (int x = 1; x < 19; x++) {
    for (int i = 0; i < N; i++) {
      p[i][x] = p[p[i][x - 1]][x - 1];
      largest[i][x] = max(largest[i][x - 1], largest[p[i][x - 1]][x - 1]);
    }
  }
  for (int i = 0; i < N; i++) id[i] = i;
  dfs2(0, 0);
  for (int i = 0; i < M; i++) {
    if (!used[i]) {
      int c = lca(edges[i][0], edges[i][1]);
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 19; k++) {
          if ((depth[edges[i][j]] - depth[c]) & (1 << k)) {
            ans[i] = max(ans[i], largest[edges[i][j]][k]);
            edges[i][j] = p[edges[i][j]][k];
          }
        }
      }
    }
    cout << ans[i] - 1 << " ";
  }
  cout << endl;
}
