#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int u, v, c, ind;
  Edge() {}
  Edge(int _u, int _v, int _c, int _ind) {
    u = _u;
    v = _v;
    c = _c;
    ind = _ind;
  }
};
bool MSTEdge[200011];
int pa[200011];
int sz[200011];
Edge edge[200011];
int n, m;
int ans[200011];
bool func(Edge a, Edge b) { return a.c < b.c; }
int first(int x) {
  if (x == pa[x]) return x;
  return pa[x] = first(pa[x]);
}
bool join(int u, int v) {
  u = first(u);
  v = first(v);
  if (u != v) {
    if (sz[u] < sz[v]) swap(u, v);
    sz[u] += sz[v];
    pa[v] = u;
    return 1;
  }
  return 0;
}
vector<pair<int, int> > tree[200011];
vector<pair<int, int> > badGraph[200011];
void makeSpanningTree() {
  vector<Edge> E;
  for (int i = 0; i < m; i++) {
    E.push_back(edge[i]);
  }
  for (int i = 1; i <= n; i++) {
    pa[i] = i;
    sz[i] = 1;
  }
  sort(E.begin(), E.end(), func);
  for (int i = 0; i < m; i++) {
    if (join(E[i].u, E[i].v)) {
      MSTEdge[E[i].ind] = 1;
      tree[E[i].u].push_back(make_pair(E[i].v, E[i].ind));
      tree[E[i].v].push_back(make_pair(E[i].u, E[i].ind));
    } else {
      MSTEdge[E[i].ind] = 0;
      badGraph[E[i].u].push_back(make_pair(E[i].v, E[i].ind));
      badGraph[E[i].v].push_back(make_pair(E[i].u, E[i].ind));
    }
  }
}
int dp[200011][20];
int sdp[200011][20];
int d[200011];
void init(int v, int p, int w) {
  if (p != -1) {
    d[v] = d[p] + 1;
    dp[v][0] = p;
    sdp[v][0] = w;
  }
  for (int i = 0; i < tree[v].size(); i++) {
    if (tree[v][i].first != p) {
      init(tree[v][i].first, v, edge[tree[v][i].second].c);
    }
  }
}
void build() {
  for (int j = 1; j < 20; j++) {
    for (int i = 1; i <= n; i++) {
      dp[i][j] = dp[dp[i][j - 1]][j - 1];
      sdp[i][j] = max(sdp[i][j - 1], sdp[dp[i][j - 1]][j - 1]);
    }
  }
}
int LCA(int u, int v) {
  if (d[u] < d[v]) swap(u, v);
  int diff = d[u] - d[v];
  for (int i = 0; i < 20; i++) {
    if (diff & (1 << i)) u = dp[u][i];
  }
  if (u != v) {
    for (int i = 19; i >= 0; i--) {
      if (dp[u][i] != dp[v][i]) {
        u = dp[u][i];
        v = dp[v][i];
      }
    }
    u = dp[u][0];
  }
  return u;
}
int maxValue(int L, int u) {
  int diff = d[u] - d[L];
  int ans = 0;
  for (int i = 0; i < 20; i++) {
    if (diff & (1 << i)) {
      ans = max(ans, sdp[u][i]);
      u = dp[u][i];
    }
  }
  return ans;
}
int maxPathValue(int u, int v) {
  int L = LCA(u, v);
  return max(maxValue(L, u), maxValue(L, v));
}
void calcForNonMSTEdges() {
  for (int i = 0; i < m; i++) {
    if (!MSTEdge[i]) {
      ans[i] = maxPathValue(edge[i].u, edge[i].v) - 1;
    }
  }
}
int pa_edge[200011];
bool del[200011];
int curval[200011];
int tot = 0;
std::vector<int> V;
void dfs(int v, int p) {
  sz[v] = 1;
  tot++;
  V.push_back(v);
  for (int i = 0; i < tree[v].size(); i++) {
    if (!del[tree[v][i].first] and tree[v][i].first != p) {
      dfs(tree[v][i].first, v);
      sz[v] += sz[tree[v][i].first];
    }
  }
}
int centroid(int v, int p) {
  int mx = -1;
  for (int i = 0; i < tree[v].size(); i++) {
    if (tree[v][i].first != p and !del[tree[v][i].first]) {
      if (mx == -1) {
        mx = tree[v][i].first;
      } else {
        if (sz[mx] < sz[tree[v][i].first]) mx = tree[v][i].first;
      }
    }
  }
  if (mx == -1 or sz[mx] <= tot / 2) return v;
  return centroid(mx, v);
}
int mark[200011];
void marknodes(int v, int p, int second) {
  mark[v] = second;
  curval[v] = INT_MAX;
  for (int i = 0; i < tree[v].size(); i++) {
    if (tree[v][i].first != p and !del[tree[v][i].first]) {
      marknodes(tree[v][i].first, v, second);
    }
  }
}
void dfscalc(int v, int p, int id) {
  for (int i = 0; i < tree[v].size(); i++) {
    if (tree[v][i].first != p and !del[tree[v][i].first]) {
      dfscalc(tree[v][i].first, v, tree[v][i].second);
      curval[v] = min(curval[v], curval[tree[v][i].first]);
    }
  }
  if (id != -1 and curval[v] != INT_MAX) {
    ans[id] = min(ans[id], curval[v] - 1);
  }
}
void calcForMSTEdges(int v, int id) {
  tot = 0;
  V.clear();
  dfs(v, -1);
  int root = centroid(v, -1);
  del[root] = 1;
  int tmp = 1;
  for (int i = 0; i < tree[root].size(); i++) {
    if (!del[tree[root][i].first]) {
      marknodes(tree[root][i].first, root, tmp++);
    }
  }
  curval[root] = INT_MAX;
  mark[root] = tmp++;
  int x, y;
  for (int i = 0; i < V.size(); i++) {
    x = V[i];
    for (int j = 0; j < badGraph[x].size(); j++) {
      y = badGraph[x][j].first;
      if (mark[x] != 0 and mark[y] != 0 and mark[x] != mark[y]) {
        curval[x] = min(curval[x], edge[badGraph[x][j].second].c);
        curval[y] = min(curval[y], edge[badGraph[x][j].second].c);
      }
    }
  }
  for (int i = 0; i < V.size(); i++) mark[V[i]] = 0;
  mark[root] = 0;
  dfscalc(root, -1, -1);
  for (int i = 0; i < tree[root].size(); i++) {
    if (!del[tree[root][i].first]) {
      calcForMSTEdges(tree[root][i].first, tree[root][i].second);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> edge[i].u >> edge[i].v >> edge[i].c;
    edge[i].ind = i;
  }
  makeSpanningTree();
  init(1, -1, -1);
  build();
  for (int i = 0; i < m; i++) {
    ans[i] = INT_MAX;
  }
  calcForNonMSTEdges();
  calcForMSTEdges(1, -1);
  for (int i = 0; i < m; i++) {
    if (ans[i] == INT_MAX) ans[i] = -1;
    if (ans[i] == -1) assert(MSTEdge[i]);
    cout << ans[i] << " ";
  }
}
