#include <bits/stdc++.h>
using namespace std;
int p[200005];
int fn(int x) {
  if (p[x] == -1)
    return x;
  else
    return p[x] = fn(p[x]);
}
bool un(int x, int y) {
  x = fn(x);
  y = fn(y);
  if (x == y) return false;
  p[x] = y;
  return true;
}
vector<pair<int, int> > g[200005];
vector<pair<int, int> > ug[200005];
bool used[200005];
int sz[200005];
void dfssz(int u, int p = -1) {
  sz[u] = 1;
  for (auto v : g[u]) {
    if (v.first == p) continue;
    dfssz(v.first, u);
    sz[u] += sz[v.first];
  }
}
bool big[200005];
bool vert[200005];
multiset<int> cons;
void add(int u, int p) {
  for (auto v : g[u]) {
    if (v.first == p || big[v.first]) continue;
    add(v.first, u);
  }
  for (auto v : ug[u]) {
    if (vert[v.first]) {
      cons.erase(cons.find(v.second));
    } else {
      cons.insert(v.second);
    }
  }
  vert[u] = true;
}
void rem(int u, int p) {
  vert[u] = false;
  for (auto v : g[u]) {
    if (v.first == p || big[v.first]) continue;
    rem(v.first, u);
  }
}
int ans[200005];
void dfs1(int u, int p = -1, int num = -1, bool keep = true) {
  int mx = 0;
  int mindex = -1;
  int msc = -1;
  for (auto v : g[u]) {
    if (v.first == p) continue;
    if (sz[v.first] > mx) {
      mx = sz[v.first];
      mindex = v.first;
      msc = v.second;
    }
  }
  for (auto v : g[u]) {
    if (v.first == p || v.first == mindex) continue;
    dfs1(v.first, u, v.second, false);
  }
  if (mindex != -1) {
    dfs1(mindex, u, msc, true);
    big[mindex] = true;
  }
  add(u, p);
  if (num != -1) ans[num] = cons.empty() ? -1 : *cons.begin() - 1;
  if (mindex != -1) big[mindex] = false;
  if (!keep) {
    cons.clear();
    rem(u, p);
  }
}
int par[200005][18];
int dep[200005];
int mxe[200005][18];
int realw[200005];
void dfs2(int u, int p = 0) {
  par[u][0] = p;
  dep[u] = dep[p] + 1;
  for (int i = 1; i < 18; i++) {
    par[u][i] = par[par[u][i - 1]][i - 1];
    mxe[u][i] = max(mxe[u][i - 1], mxe[par[u][i - 1]][i - 1]);
  }
  for (auto v : g[u]) {
    if (v.first == p) continue;
    mxe[v.first][0] = realw[v.second];
    dfs2(v.first, u);
  }
}
int query(int u, int v) {
  if (dep[u] > dep[v]) swap(u, v);
  int ret = 0;
  for (int i = 17; i >= 0; i--) {
    if (dep[u] <= dep[par[v][i]]) {
      ret = max(ret, mxe[v][i]);
      v = par[v][i];
    }
  }
  if (u == v) return ret;
  for (int i = 17; i >= 0; i--) {
    if (par[u][i] != par[v][i]) {
      ret = max(ret, mxe[u][i]);
      ret = max(ret, mxe[v][i]);
      u = par[u][i];
      v = par[v][i];
    }
  }
  ret = max(ret, mxe[u][0]);
  ret = max(ret, mxe[v][0]);
  return ret;
}
int main() {
  memset(p, -1, sizeof(p));
  int n, m;
  scanf("%d%d", &n, &m);
  vector<tuple<int, int, int, int> > ev;
  for (int i = 0; i < m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    ev.emplace_back(u, v, c, i);
  }
  sort(ev.begin(), ev.end(),
       [](tuple<int, int, int, int> x, tuple<int, int, int, int> y) {
         return get<2>(x) < get<2>(y);
       });
  for (int i = 0; i < m; i++) {
    realw[get<3>(ev[i])] = get<2>(ev[i]);
    if (un(get<0>(ev[i]), get<1>(ev[i]))) {
      g[get<0>(ev[i])].emplace_back(get<1>(ev[i]), get<3>(ev[i]));
      g[get<1>(ev[i])].emplace_back(get<0>(ev[i]), get<3>(ev[i]));
      used[get<3>(ev[i])] = true;
    } else {
      ug[get<0>(ev[i])].emplace_back(get<1>(ev[i]), get<2>(ev[i]));
      ug[get<1>(ev[i])].emplace_back(get<0>(ev[i]), get<2>(ev[i]));
      used[get<3>(ev[i])] = false;
    }
  }
  dfssz(1);
  dfs1(1);
  dfs2(1);
  for (int i = 0; i < m; i++) {
    if (!used[get<3>(ev[i])]) {
      ans[get<3>(ev[i])] = query(get<0>(ev[i]), get<1>(ev[i])) - 1;
    }
  }
  for (int i = 0; i < m; i++) {
    printf("%d ", ans[i]);
  }
  printf("\n");
  return 0;
}
