#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535897932384626433832795l;
template <typename T>
inline auto sqr(T x) -> decltype(x * x) {
  return x * x;
}
template <typename T1, typename T2>
inline bool umx(T1& a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool umn(T1& a, T2 b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int N = 200100;
vector<pair<pair<int, int>, pair<int, int> > > e;
pair<int, int> pr[N];
int h[N];
int idx[N];
int root[N];
int sz[N];
int p[N];
vector<pair<int, int> > a[N];
vector<pair<int, int> > g[N];
int cost[N];
int rnd;
int cnt;
int vert[N];
int ans[N];
const int INF = 1e9 + 100;
void dfs(int v, int h1, pair<int, int> pr1) {
  ;
  h[v] = h1;
  pr[v] = pr1;
  vert[cnt++] = v;
  for (int i = int(0); i < int(((int)(a[v]).size())); ++i) {
    int to = a[v][i].first;
    ;
    if (to != pr1.first) {
      dfs(to, h1 + 1, make_pair(v, a[v][i].second));
    }
  }
}
int get(int a) {
  if (a == p[a]) return a;
  return p[a] = get(p[a]);
}
bool uni(int l, int r) {
  l = get(l), r = get(r);
  if (l == r) return false;
  if (rnd ^= 1) swap(l, r);
  if (h[root[l]] < h[root[r]]) {
    root[r] = root[l];
  }
  p[l] = r;
  return true;
}
bool mst[N];
int ord[4 * N];
int szord;
void process(int u, int v, int res) {
  u = root[get(u)], v = root[get(v)];
  szord = 0;
  while (u != v) {
    if (h[u] > h[v]) swap(u, v);
    ord[szord++] = v;
    umn(ans[pr[v].second], res - 1);
    v = root[get(pr[v].first)];
  }
  ord[szord++] = v;
  for (int i = int(0); i < int(szord - 1); ++i) uni(ord[i], ord[i + 1]);
}
int main() {
  cout.setf(ios::showpoint | ios::fixed);
  cout.precision(20);
  int n, m;
  cin >> n >> m;
  for (int i = int(0); i < int(m); ++i) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    u--, v--;
    cost[i] = c;
    g[u].push_back(make_pair(v, i));
    g[v].push_back(make_pair(u, i));
    e.push_back(make_pair(make_pair(c, i), make_pair(u, v)));
  }
  for (int i = int(0); i < int(n); ++i) p[i] = i;
  for (int i = int(0); i < int(n); ++i) pr[i] = make_pair(-1, -1);
  memset(mst, 0, sizeof(mst));
  for (int i = int(0); i < int(n); ++i) {
    h[i] = 0;
    idx[i] = i;
    root[i] = i;
    sz[i] = 1;
  }
  sort((e).begin(), (e).end());
  for (int i = int(0); i < int(m); ++i) ans[i] = INF;
  for (int i = int(0); i < int(((int)(e).size())); ++i) {
    int u = e[i].second.first, v = e[i].second.second;
    if (idx[u] != idx[v]) {
      ;
      if (sz[idx[u]] < sz[idx[v]]) swap(u, v);
      sz[idx[u]] += sz[idx[v]];
      cnt = 0;
      dfs(v, h[u] + 1, make_pair(u, e[i].first.second));
      for (int j = int(0); j < int(cnt); ++j) {
        for (int k = int(0); k < int(((int)(g[vert[j]]).size())); ++k) {
          int to = g[vert[j]][k].first;
          if (idx[to] == idx[u] && g[vert[j]][k].second != e[i].first.second) {
            ans[g[vert[j]][k].second] = e[i].first.first - 1;
          }
        }
      }
      for (int j = int(0); j < int(cnt); ++j) {
        idx[vert[j]] = idx[u];
        if (h[vert[j]] < h[root[get(vert[j])]]) {
          root[get(vert[j])] = vert[j];
        }
      }
      mst[e[i].first.second] = 1;
      a[u].push_back(make_pair(v, e[i].first.second));
      a[v].push_back(make_pair(u, e[i].first.second));
    } else {
      process(u, v, e[i].first.first);
    }
  }
  for (int i = int(0); i < int(m); ++i) {
    if (ans[i] == INF) ans[i] = -1;
    printf("%d ", ans[i]);
  }
  cout << endl;
  return 0;
}
