#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<pair<int, int>> adj[N], ds;
int pa[N], dep[N], up[N], sp[20][N], mx[20][N], ans[N];
bool mark[N];
vector<int> them[N], xoa[N];
int ux[N], vx[N], cx[N];
int tg[N];
int getpa(int u) { return (pa[u] < 0) ? u : (pa[u] = getpa(pa[u])); }
void dung(int u) {
  for (auto &p : adj[u]) {
    int v = p.first;
    int id = p.second;
    if (pa[u] == v) continue;
    pa[v] = u;
    dep[v] = dep[u] + 1;
    up[v] = id;
    dung(v);
  }
}
multiset<int> ms[N];
int sh[N];
void duyet(int u) {
  for (auto &p : adj[u]) {
    int v = p.first;
    int id = p.second;
    if (pa[u] == v) continue;
    duyet(v);
    int uu = u, vv = v;
    if (ms[sh[u]].size() < ms[sh[v]].size()) swap(sh[u], sh[v]);
    for (auto &val : ms[sh[v]]) ms[sh[u]].insert(val);
    ms[sh[v]].clear();
  }
  for (auto &v : them[u]) ms[sh[u]].insert(v);
  if (up[u] > 0 && !ms[sh[u]].empty()) ans[up[u]] = *(ms[sh[u]].begin());
  for (auto &v : xoa[u]) ms[sh[u]].erase(ms[sh[u]].find(v));
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", ux + i, vx + i, cx + i);
    ds.push_back({cx[i], i});
    ans[i] = -1;
    mark[i] = false;
  }
  sort(ds.begin(), ds.end());
  for (int i = 1; i <= n; i++) pa[i] = -1;
  set<int> dinh;
  map<pair<int, int>, int> mp;
  for (int i = 0; i < ds.size();) {
    int val = ds[i].first;
    while (i < ds.size() && ds[i].first == val) {
      int u = getpa(ux[ds[i].second]);
      int v = getpa(vx[ds[i].second]);
      if (u != v) {
        mark[ds[i].second] = true;
        if (pa[u] > pa[v]) swap(u, v);
        pa[u] += pa[v];
        pa[v] = u;
      }
      i++;
    }
  }
  for (int i = 1; i <= m; i++) {
    if (!mark[i]) continue;
    adj[ux[i]].push_back({vx[i], i});
    adj[vx[i]].push_back({ux[i], i});
  }
  for (int i = 1; i <= n; i++) pa[i] = 0;
  dung(1);
  for (int i = 1; i <= n; i++) {
    sp[0][i] = pa[i];
    mx[0][i] = cx[up[i]];
  }
  int cs = 0;
  for (int g = 2; g < n; g <<= 1, cs++) {
    int moi = cs + 1;
    for (int i = 1; i <= n; i++)
      if (sp[cs][i] == 0) {
        sp[moi][i] = 0;
        mx[moi][i] = mx[cs][i];
      } else {
        sp[moi][i] = sp[cs][sp[cs][i]];
        mx[moi][i] = max(mx[cs][i], mx[cs][sp[cs][i]]);
      }
  }
  for (int i = 1; i <= m; i++)
    if (!mark[i]) {
      int u = ux[i];
      int v = vx[i];
      int uu = u, vv = v;
      int res = 0;
      if (dep[u] > dep[v]) {
        swap(u, v);
        swap(uu, vv);
      }
      if (dep[u] < dep[v]) {
        for (int j = cs; j >= 0; j--)
          if (sp[j][v] > 0 && dep[sp[j][v]] > dep[u]) {
            res = max(res, mx[j][v]);
            v = sp[j][v];
          }
        res = max(res, mx[0][v]);
        uu = v;
        v = sp[0][v];
      }
      if (v == u) {
        them[vv].push_back(cx[i] - 1);
        xoa[uu].push_back(cx[i] - 1);
      } else {
        for (int j = cs; j >= 0; j--)
          if (sp[j][v] != sp[j][u]) {
            res = max(res, max(mx[j][u], mx[j][v]));
            u = sp[j][u];
            v = sp[j][v];
          }
        res = max(res, max(mx[0][u], mx[0][v]));
        them[vx[i]].push_back(cx[i] - 1);
        them[ux[i]].push_back(cx[i] - 1);
        xoa[u].push_back(cx[i] - 1);
        xoa[v].push_back(cx[i] - 1);
      }
      ans[i] = res - 1;
    }
  for (int i = 1; i <= n; i++) sh[i] = i;
  duyet(1);
  for (int i = 1; i <= m; i++) printf("%d ", ans[i]);
}
