#include <bits/stdc++.h>
using namespace std;
int n, m;
bool on_tree[200001];
vector<pair<int, pair<int, pair<int, int> > > > Edge;
struct DSU_Template {
  int p[200001];
  int anc(int x) {
    if (p[x] == x) return x;
    return p[x] = anc(p[x]);
  }
  void join(int u, int v) { p[anc(u)] = anc(v); }
} DSU;
vector<pair<int, int> > adj[200001];
int h[200001], nChild[200001], nxt[200001], par[200001];
struct LCA_template {
  int p[200001][21], mx[200001][21];
  void init() {
    for (int j = 1; j <= 20; j++)
      for (int i = 1; i <= n; i++) {
        p[i][j] = p[p[i][j - 1]][j - 1];
        mx[i][j] = max(mx[i][j - 1], mx[p[i][j - 1]][j - 1]);
      }
  }
  pair<int, int> get_p(int u, int v) {
    if (h[u] > h[v]) swap(u, v);
    int cur_mx = 0;
    for (int j = 20; j >= 0; j--)
      if (h[p[v][j]] >= h[u]) {
        cur_mx = max(cur_mx, mx[v][j]);
        v = p[v][j];
      }
    if (u == v) return pair<int, int>(u, cur_mx);
    for (int j = 20; j >= 0; j--)
      if (p[u][j] != p[v][j]) {
        cur_mx = max(cur_mx, mx[v][j]);
        cur_mx = max(cur_mx, mx[u][j]);
        u = p[u][j];
        v = p[v][j];
      }
    return pair<int, int>(p[u][0], max(cur_mx, max(mx[u][0], mx[v][0])));
  }
} LCA;
struct HLD_Template {
  int top = 0;
  int pos[200001], org[200001], head[200001], Seg[800001];
  void decompose(int u, int cur_h, int p) {
    if (u == 1)
      for (int i = 1; i <= 4 * n; i++) Seg[i] = 1000000010;
    pos[u] = ++top;
    org[top] = u;
    head[u] = cur_h;
    if (nxt[u] != -1) decompose(nxt[u], cur_h, u);
    for (pair<int, int> cur_p : adj[u]) {
      int v = cur_p.first;
      if (v != nxt[u] && v != p) decompose(v, v, u);
    }
  }
  void update_Seg(int node, int l, int r, int ll, int rr, int val) {
    if (ll > rr) return;
    if (r < ll || rr < l) return;
    if (ll <= l && r <= rr) {
      Seg[node] = min(Seg[node], val);
      return;
    }
    int mi = (l + r) / 2;
    update_Seg(node * 2, l, mi, ll, rr, val);
    update_Seg(node * 2 + 1, mi + 1, r, ll, rr, val);
  }
  int query_Seg(int node, int l, int r, int ll, int rr,
                int cur_ans = 1000000010) {
    if (ll > rr) return 1000000010;
    if (r < ll || rr < l) return 1000000010;
    cur_ans = min(cur_ans, Seg[node]);
    if (ll <= l && r <= rr) {
      return cur_ans;
    }
    int mi = (l + r) / 2;
    int a = query_Seg(node * 2, l, mi, ll, rr, cur_ans);
    int b = query_Seg(node * 2 + 1, mi + 1, r, ll, rr, cur_ans);
    return min(a, b);
  }
  void update(int u, int v, int val) {
    pair<int, int> cur_p = LCA.get_p(u, v);
    int l = cur_p.first;
    for (; head[u] != head[l]; u = par[head[u]])
      update_Seg(1, 1, n, pos[head[u]], pos[u], val);
    update_Seg(1, 1, n, pos[l] + 1, pos[u], val);
    for (; head[v] != head[l]; v = par[head[v]])
      update_Seg(1, 1, n, pos[head[v]], pos[v], val);
    update_Seg(1, 1, n, pos[l] + 1, pos[v], val);
  }
  int query(int u, int v) {
    pair<int, int> cur_p = LCA.get_p(u, v);
    int l = cur_p.first;
    int ans = 1000000010;
    for (; head[u] != head[l]; u = par[head[u]])
      ans = min(ans, query_Seg(1, 1, n, pos[head[u]], pos[u]));
    ans = min(ans, query_Seg(1, 1, n, pos[l] + 1, pos[u]));
    for (; head[v] != head[l]; v = par[head[v]])
      ans = min(ans, query_Seg(1, 1, n, pos[head[v]], pos[v]));
    ans = min(ans, query_Seg(1, 1, n, pos[l] + 1, pos[v]));
    return ans;
  }
} HLD;
void dfs(int u, int p) {
  nChild[u] = 1;
  int cur_mx = 0;
  nxt[u] = -1;
  for (pair<int, int> cur_p : adj[u]) {
    int v = cur_p.first, w = cur_p.second;
    if (v != p) {
      par[v] = u;
      h[v] = h[u] + 1;
      LCA.p[v][0] = u;
      LCA.mx[v][0] = w;
      dfs(v, u);
      nChild[u] += nChild[v];
      if (nChild[v] > cur_mx) {
        cur_mx = nChild[v];
        nxt[u] = v;
      }
    }
  }
}
int ans[200001];
int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) DSU.p[i] = i;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    Edge.push_back(pair<int, pair<int, pair<int, int> > >(
        w, pair<int, pair<int, int> >(i, pair<int, int>(u, v))));
  }
  sort(Edge.begin(), Edge.end());
  for (int i = 0; i < m; i++) {
    int u = Edge[i].second.second.first, v = Edge[i].second.second.second,
        id = Edge[i].second.first, w = Edge[i].first;
    if (DSU.anc(u) != DSU.anc(v)) {
      DSU.join(u, v);
      on_tree[i] = true;
      adj[u].push_back(pair<int, int>(v, w));
      adj[v].push_back(pair<int, int>(u, w));
    }
  }
  h[1] = 1;
  dfs(1, -1);
  LCA.init();
  HLD.decompose(1, 1, -1);
  for (int i = 0; i < m; i++) {
    int u = Edge[i].second.second.first, v = Edge[i].second.second.second,
        id = Edge[i].second.first, w = Edge[i].first;
    if (!on_tree[i]) HLD.update(u, v, w);
  }
  for (int i = 0; i < m; i++) {
    int u = Edge[i].second.second.first, v = Edge[i].second.second.second,
        id = Edge[i].second.first;
    if (!on_tree[i])
      ans[id] = LCA.get_p(u, v).second - 1;
    else
      ans[id] = HLD.query(u, v) - 1;
  }
  for (int i = 1; i <= m; i++) {
    if (ans[i] > 1e9) ans[i] = -1;
    cout << ans[i] << ' ';
  }
}
