#include <bits/stdc++.h>
using namespace std;
const int K = 2;
const double PI = acos(-1);
template <class T>
inline T bigmod(T a, T b, T M) {
  long long ret = 1;
  for (; b > 0; b >>= 1) {
    if (b & 1) ret = (ret * a) % M;
    a = (a * a) % M;
  }
  return ret;
}
template <class T>
inline T gcd(T a, T b) {
  while (b) {
    a = a % b;
    swap(a, b);
  }
  return a;
}
template <class T>
inline T lcm(T a, T b) {
  return (a / gcd(a, b)) * b;
}
template <class T>
inline T poww(T a, T b) {
  long long ret = 1;
  for (; b > 0; b >>= 1) {
    if (b & 1) ret = ret * a;
    a = a * a;
  }
  return (T)ret;
}
pair<int, int> exEuclid(int a, int b) {
  if (b == 0)
    return pair<int, int>(1, 0);
  else {
    pair<int, int> r = exEuclid(b, a % b);
    return pair<int, int>(r.second, r.first - r.second * (a / b));
  }
}
int modInv(int a, int m) {
  pair<int, int> ret = exEuclid(a, m);
  return ((ret.first % m) + m) % m;
}
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
int n, m, x, y, z;
struct edge {
  int u, v, id, w;
  edge(int _u, int _v, int _w, int _id) {
    u = _u;
    v = _v;
    w = _w;
    id = _id;
  }
};
bool comp(edge a, edge b) { return a.w < b.w; }
vector<edge> vec, nvec;
int par[2 * 100010], pa[2 * 100010][40], del[2 * 100010], rnk[2 * 100010],
    mark[2 * 100010], tmp[2 * 100010], ans[2 * 100010], depth[2 * 100010],
    mx[2 * 100010][40], sub[2 * 100010];
vector<pair<int, pair<int, int> > > tree[2 * 100010], nontree[2 * 100010];
int find(int u) {
  if (par[u] == u) return u;
  return par[u] = find(par[u]);
}
void dfs(int u, int from, int dep, int we) {
  depth[u] = dep;
  pa[u][0] = from;
  mx[u][0] = we;
  for (int i = (0); i < (tree[u].size()); i++) {
    int v = tree[u][i].first;
    if (from == v) continue;
    dfs(v, u, dep + 1, tree[u][i].second.first);
  }
}
int LCA(int p, int q) {
  if (depth[q] > depth[p]) swap(p, q);
  int lg = 0;
  while (1) {
    int next = lg + 1;
    if ((1 << next) > depth[p]) break;
    lg++;
  }
  for (int i = lg; i >= 0; i--) {
    if (depth[p] - (1 << i) >= depth[q]) p = pa[p][i];
  }
  if (p == q) return p;
  for (int i = lg; i >= 0; i--) {
    if (pa[p][i] != -1 && pa[p][i] != pa[q][i]) {
      p = pa[p][i];
      q = pa[q][i];
    }
  }
  return pa[p][0];
}
int getmax(int p, int q) {
  int ret = -1;
  int lg = 0;
  if (p == q) return ret;
  int dif = depth[p] - depth[q];
  while (1) {
    int next = lg + 1;
    if ((1 << next) > dif) break;
    lg++;
  }
  for (int i = lg; i >= 0; i--) {
    if (depth[p] - (1 << i) >= depth[q]) {
      ret = max(ret, mx[p][i]);
      p = pa[p][i];
    }
  }
  return ret;
}
void outoftree() {
  memset(pa, -1, sizeof pa);
  dfs(1, -1, 0, -1);
  for (int j = 1; 1 << j <= n; j++) {
    for (int i = 1; i <= n; i++) {
      if (pa[i][j - 1] != -1) {
        pa[i][j] = pa[pa[i][j - 1]][j - 1];
        mx[i][j] = max(mx[pa[i][j - 1]][j - 1], mx[i][j - 1]);
      }
    }
  }
  for (int i = (0); i < (nvec.size()); i++) {
    int lca = LCA(nvec[i].u, nvec[i].v);
    ans[nvec[i].id] = max(getmax(nvec[i].u, lca), getmax(nvec[i].v, lca)) - 1;
  }
}
int nn;
vector<int> va;
void dfs1(int u, int from) {
  sub[u] = 1;
  nn++;
  for (int i = (0); i < (tree[u].size()); i++) {
    int v = tree[u][i].first;
    if (v == from || del[v]) continue;
    dfs1(v, u);
    sub[u] += sub[v];
  }
}
int dfs2(int u, int from) {
  for (int i = (0); i < (tree[u].size()); i++) {
    int v = tree[u][i].first;
    if (v == from || del[v]) continue;
    if (sub[v] > nn / 2) return dfs2(v, u);
  }
  return u;
}
void dfs3(int u, int from, int tt) {
  mark[u] = tt;
  tmp[u] = 1061109567;
  va.push_back(u);
  for (int i = (0); i < (tree[u].size()); i++) {
    int v = tree[u][i].first;
    if (v == from || del[v]) continue;
    dfs3(v, u, tt);
  }
}
void dfs4(int u, int from, int idx) {
  for (int i = (0); i < (tree[u].size()); i++) {
    int v = tree[u][i].first;
    if (v == from || del[v]) continue;
    dfs4(v, u, tree[u][i].second.second);
    tmp[u] = min(tmp[u], tmp[v]);
  }
  if (from != -1 && tmp[u] != 1061109567) {
    ans[idx] = min(ans[idx], tmp[u] - 1);
  }
}
void decompose(int root, int from) {
  nn = 0;
  dfs1(root, root);
  int centriod = dfs2(root, root);
  del[centriod] = 1;
  va.clear();
  int tt = 1;
  mark[centriod] = tt;
  tmp[centriod] = 1061109567;
  va.push_back(centriod);
  for (int i = (0); i < (tree[centriod].size()); i++) {
    int v = tree[centriod][i].first;
    if (!del[v]) {
      tt++;
      dfs3(v, centriod, tt);
    }
  }
  for (int i = (0); i < (va.size()); i++) {
    int u = va[i];
    for (int j = (0); j < (nontree[u].size()); j++) {
      int v = nontree[u][j].first;
      if (mark[u] && mark[v] && mark[u] != mark[v]) {
        tmp[u] = min(tmp[u], nontree[u][j].second.first);
        tmp[v] = min(tmp[v], nontree[u][j].second.first);
      }
    }
  }
  dfs4(centriod, -1, -1);
  for (int i = (0); i < (va.size()); i++) mark[va[i]] = 0;
  for (int i = (0); i < (tree[centriod].size()); i++) {
    int v = tree[centriod][i].first;
    if (!del[v]) decompose(v, centriod);
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = (1); i <= (m); i++) {
    scanf("%d %d %d", &x, &y, &z);
    vec.push_back(edge(x, y, z, i));
  }
  sort((vec).begin(), (vec).end(), comp);
  for (int i = (1); i <= (n); i++) par[i] = i;
  for (int i = (0); i < (m); i++) {
    int uu = vec[i].u, vv = vec[i].v, ww = vec[i].w, idx = vec[i].id;
    int pu = find(uu);
    int pv = find(vv);
    if (pu != pv) {
      if (rnk[pu] < rnk[pv]) {
        par[pu] = pv;
      } else if (rnk[pv] < rnk[pu]) {
        par[pv] = pu;
      } else {
        par[pv] = pu;
        rnk[pu]++;
      }
      tree[uu].push_back(make_pair(vv, pair<int, int>(ww, idx)));
      tree[vv].push_back(make_pair(uu, pair<int, int>(ww, idx)));
    } else {
      nontree[uu].push_back(make_pair(vv, pair<int, int>(ww, idx)));
      nontree[vv].push_back(make_pair(uu, pair<int, int>(ww, idx)));
      nvec.push_back(edge(uu, vv, ww, idx));
    }
  }
  memset(ans, 63, sizeof ans);
  outoftree();
  decompose(1, -1);
  for (int i = (1); i <= (m); i++) {
    if (ans[i] == 1061109567) ans[i] = -1;
    printf("%d ", ans[i]);
  }
  printf("\n");
  return 0;
}
