#include <bits/stdc++.h>
using namespace std;
vector<pair<pair<int, int>, pair<int, int> > > edge;
bool vis[200001];
int p[200001];
int ans[200001];
int root(int a) { return p[a] == a ? a : (p[a] = root(p[a])); }
void merge(int a, int b) {
  a = root(a), b = root(b);
  if (a != b) p[a] = b;
}
vector<pair<pair<int, int>, int> > g[200001];
int rmq[18][200001];
int par[18][200001], depth[200005];
int idx[200001];
void dfs(int u, int p, int d, int c = 0, int j = 0) {
  idx[u] = j;
  depth[u] = d;
  par[0][u] = p;
  rmq[0][u] = c;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first.first, c = g[u][i].first.second, idx = g[u][i].second;
    if (v == p) continue;
    dfs(v, u, d + 1, c, idx);
  }
}
int LCA(int a, int b) {
  if (depth[b] > depth[a]) swap(a, b);
  int diff = depth[a] - depth[b];
  for (int i = 17; i >= 0; i--) {
    if ((diff >> i) & 1) a = par[i][a];
  }
  if (a == b) return a;
  for (int i = 17; i >= 0; i--) {
    if (par[i][a] != par[i][b]) a = par[i][a], b = par[i][b];
  }
  return par[0][a];
}
int getMax(int a, int b) {
  int d = depth[a] - depth[b];
  if (d <= 0) return 0;
  int ans = 0;
  for (int i = 17; i >= 0; i--) {
    if ((d >> i) & 1) {
      ans = max(ans, rmq[i][a]);
      a = par[i][a];
    }
  }
  return ans;
}
int st[200001], en[200001], idx2[200001], sz[200001], T = 1;
vector<int> add[200001], del[200001];
multiset<int> cur;
multiset<int>::iterator its;
void dfs_sz(int u, int p) {
  sz[u] = add[u].size() + del[u].size();
  st[u] = T++;
  idx2[T - 1] = u;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first.first;
    if (v == p) continue;
    dfs_sz(v, u);
    sz[u] += sz[v];
  }
  en[u] = T - 1;
}
void dfs2(int u, int p, bool keep) {
  int mx = -1, bigChild = -1;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first.first;
    if (v == p) continue;
    if (sz[v] > mx) {
      mx = sz[v];
      bigChild = v;
    }
  }
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first.first;
    if (v == p || v == bigChild) continue;
    dfs2(v, u, 0);
  }
  if (bigChild != -1) dfs2(bigChild, u, 1);
  for (int k = 0; k < add[u].size(); k++) {
    cur.insert(add[u][k]);
  }
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first.first;
    if (v == p || v == bigChild) continue;
    for (int j = st[v]; j <= en[v]; j++) {
      for (int k = 0; k < add[idx2[j]].size(); k++) {
        cur.insert(add[idx2[j]][k]);
      }
    }
    for (int j = st[v]; j <= en[v]; j++) {
      for (int k = 0; k < del[idx2[j]].size(); k++) {
        cur.erase(cur.find(del[idx2[j]][k]));
      }
    }
  }
  for (int k = 0; k < del[u].size(); k++) {
    cur.erase(cur.find(del[u][k]));
  }
  if (cur.empty())
    ans[idx[u]] = -1;
  else
    ans[idx[u]] = *cur.begin() - 1;
  if (keep == 0) cur.clear();
}
int main() {
  int n, m, a, b, c;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) p[i] = i;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    edge.push_back(pair<pair<int, int>, pair<int, int> >(pair<int, int>(c, i),
                                                         pair<int, int>(a, b)));
  }
  sort(edge.begin(), edge.end());
  for (int i = 0; i < edge.size(); i++) {
    int a = edge[i].second.first, b = edge[i].second.second,
        c = edge[i].first.first;
    int j = edge[i].first.second;
    if (root(a) != root(b)) {
      merge(a, b);
      vis[j] = 1;
      g[a].push_back(pair<pair<int, int>, int>(pair<int, int>(b, c), j));
      g[b].push_back(pair<pair<int, int>, int>(pair<int, int>(a, c), j));
    }
  }
  dfs(1, 0, 0);
  for (int j = 1; j <= 17; j++) {
    for (int i = 1; i <= n; i++) {
      if (par[j - 1][i] != 0) {
        par[j][i] = par[j - 1][par[j - 1][i]];
        rmq[j][i] = max(rmq[j - 1][i], rmq[j - 1][par[j - 1][i]]);
      }
    }
  }
  for (int i = 0; i < edge.size(); i++) {
    int a = edge[i].second.first, b = edge[i].second.second,
        c = edge[i].first.first;
    int j = edge[i].first.second;
    if (!vis[j]) {
      int lca = LCA(a, b);
      int mx = max(getMax(a, lca), getMax(b, lca));
      ans[j] = mx - 1;
      if (lca == a) swap(a, b);
      if (lca == b) {
        add[a].push_back(c);
        del[lca].push_back(c);
      } else {
        add[a].push_back(c);
        add[b].push_back(c);
        del[lca].push_back(c);
        del[lca].push_back(c);
      }
    }
  }
  dfs_sz(1, 0);
  dfs2(1, 0, 1);
  for (int i = 1; i <= m; i++) printf("%d ", ans[i]);
}
