#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int INF = 2e9;
struct Node {
  int U, V, W, idx;
  bool operator<(Node other) const { return W < other.W; }
};
int n, m, timer = 0;
int parent[N], level[N];
vector<pair<int, int> > edge[N];
int sz[N], tin[N], tout[N], nxt[N];
int up[N][25], val[N][25];
int ST[4 * N], lazy[4 * N];
int ans[N];
vector<Node> all, e1, e2;
int find_set(int u) {
  if (u == parent[u]) return u;
  return parent[u] = find_set(parent[u]);
}
bool union_set(int u, int v) {
  u = find_set(u);
  v = find_set(v);
  if (u == v) return false;
  if (level[u] < level[v]) swap(u, v);
  parent[v] = u;
  if (level[u] == level[v]) ++level[u];
  return true;
}
void getSize(int u) {
  sz[u] = 1;
  for (auto &adj : edge[u]) {
    int v = adj.second;
    if (v == parent[u]) swap(adj, edge[u].back());
    v = adj.second;
    if (v == parent[u]) continue;
    parent[v] = u;
    getSize(v);
    sz[u] += sz[v];
    if (sz[v] > sz[edge[u][0].second]) {
      swap(adj, edge[u][0]);
    }
  }
  if (edge[u].back().second == parent[u]) edge[u].pop_back();
}
void getEuler(int u, int w) {
  up[u][0] = parent[u];
  for (int j = 1; j <= 20; ++j) up[u][j] = up[up[u][j - 1]][j - 1];
  val[u][0] = w;
  for (int j = 1; j <= 20; ++j)
    val[u][j] = max(val[u][j - 1], val[up[u][j - 1]][j - 1]);
  tin[u] = ++timer;
  for (pair<int, int> adj : edge[u]) {
    int v = adj.second;
    int w = adj.first;
    nxt[v] = (v == edge[u][0].second ? nxt[u] : v);
    getEuler(v, w);
  }
  tout[u] = timer;
}
bool ancestor(int u, int v) { return tin[u] <= tin[v] && tout[v] <= tout[u]; }
int lca(int u, int v) {
  if (ancestor(u, v)) return u;
  if (ancestor(v, u)) return v;
  for (int j = 20; j >= 0; --j) {
    if (!ancestor(up[u][j], v)) {
      u = up[u][j];
    }
  }
  return up[u][0];
}
int getVal(int u, int v) {
  if (u == v) return 0;
  int res = 0;
  for (int j = 20; j >= 0; --j) {
    if (!ancestor(up[u][j], v)) {
      res = max(res, val[u][j]);
      u = up[u][j];
    }
  }
  return max(res, val[u][0]);
}
int getNode(int u, int v) {
  for (int j = 20; j >= 0; --j)
    if (!ancestor(up[u][j], v)) u = up[u][j];
  return u;
}
void push(int node, int seg_l, int seg_r) {
  if (lazy[node] == INF) return;
  if (seg_l != seg_r) {
    lazy[node << 1] = min(lazy[node << 1], lazy[node]);
    lazy[node << 1 | 1] = min(lazy[node << 1 | 1], lazy[node]);
    ST[node << 1] = min(ST[node << 1], lazy[node << 1]);
    ST[node << 1 | 1] = min(ST[node << 1 | 1], lazy[node << 1 | 1]);
  }
  lazy[node] = INF;
}
void update(int node, int seg_l, int seg_r, int l, int r, int cur) {
  push(node, seg_l, seg_r);
  if (r < seg_l || l > seg_r) {
    return;
  }
  if (l <= seg_l && seg_r <= r) {
    lazy[node] = cur;
    ST[node] = min(ST[node], cur);
    push(node, seg_l, seg_r);
  } else {
    int seg_mid = (seg_l + seg_r) >> 1;
    update(node << 1, seg_l, seg_mid, l, r, cur);
    update(node << 1 | 1, seg_mid + 1, seg_r, l, r, cur);
    ST[node] = min(ST[node << 1], ST[node << 1 | 1]);
  }
}
int get(int node, int seg_l, int seg_r, int pos) {
  push(node, seg_l, seg_r);
  if (pos < seg_l || pos > seg_r) {
    return INF;
  }
  if (seg_l == seg_r) {
    return ST[node];
  } else {
    int seg_mid = (seg_l + seg_r) >> 1;
    return min(get(node << 1, seg_l, seg_mid, pos),
               get(node << 1 | 1, seg_mid + 1, seg_r, pos));
  }
}
void updatePath(int u, int v, int w) {
  while (true) {
    if (nxt[u] == nxt[v]) {
      if (tin[u] > tin[v]) swap(u, v);
      update(1, 1, n, tin[u], tin[v], w);
      break;
    } else {
      if (tin[nxt[u]] < tin[nxt[v]]) swap(u, v);
      update(1, 1, n, tin[nxt[u]], tin[u], w);
      u = parent[nxt[u]];
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    all.push_back({u, v, w, i});
  }
  sort(all.begin(), all.end());
  for (int i = 1; i <= n; ++i) parent[i] = i;
  for (auto e : all) {
    int u = e.U;
    int v = e.V;
    int w = e.W;
    if (union_set(u, v)) {
      edge[u].push_back({w, v});
      edge[v].push_back({w, u});
      e1.push_back(e);
    } else {
      e2.push_back(e);
    }
  }
  for (int i = 1; i <= n; ++i) parent[i] = 0;
  for (int i = 1; i <= 4 * n; ++i) {
    ST[i] = lazy[i] = INF;
  }
  getSize(1);
  getEuler(1, 0);
  tout[0] = 1e9;
  for (auto e : e2) {
    int u = e.U;
    int v = e.V;
    int w = e.W;
    int root = lca(u, v);
    ans[e.idx] = max(getVal(u, root), getVal(v, root));
    int childU = getNode(u, root);
    int childV = getNode(v, root);
    if (childU != root) updatePath(u, childU, w);
    if (childV != root) updatePath(v, childV, w);
  }
  for (auto e : e1) {
    int u = e.U;
    int v = e.V;
    if (u != parent[v]) swap(u, v);
    ans[e.idx] = get(1, 1, n, tin[v]);
    if (ans[e.idx] == INF) ans[e.idx] = 0;
  }
  for (int i = 1; i <= m; ++i) cout << ans[i] - 1 << " ";
}
