#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int INF = 0x3f3f3f3f;
struct Edge {
  int u, v, w, id;
  Edge() {}
  Edge(int _u, int _v, int _w) : u(_u), v(_v), w(_w) {}
  bool operator<(const Edge& rhs) const { return w < rhs.w; }
};
Edge eg[maxn];
int n, m, pre[maxn], dep[maxn], mx[maxn][20], p[maxn][20], _min[maxn][20];
vector<Edge> G[maxn];
bool flag[maxn];
void add(int u, int v, int w) { G[u].push_back(Edge(u, v, w)); }
int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); }
void dfs(int u, int fa) {
  for (int i = 0; i < G[u].size(); ++i) {
    int v = G[u][i].v;
    if (v == fa) continue;
    dep[v] = dep[u] + 1;
    p[v][0] = u;
    mx[v][0] = G[u][i].w;
    dfs(v, u);
  }
}
void initLCA() {
  for (int i = 1; i <= 18; ++i)
    for (int j = 1; j <= n; ++j) {
      p[j][i] = p[p[j][i - 1]][i - 1];
      mx[j][i] = max(mx[j][i - 1], mx[p[j][i - 1]][i - 1]);
    }
}
int LCA(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  int ans = 0;
  for (int i = 18; i >= 0; --i) {
    if (dep[p[a][i]] >= dep[b]) {
      ans = max(ans, mx[a][i]);
      a = p[a][i];
    }
  }
  if (a == b) return ans;
  for (int i = 18; i >= 0; --i)
    if (p[a][i] != p[b][i]) {
      ans = max(ans, mx[a][i]);
      ans = max(ans, mx[b][i]);
      a = p[a][i];
      b = p[b][i];
    }
  return max(ans, max(mx[a][0], mx[b][0]));
}
void update(int x, int y, int val) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 18; i >= 0; --i) {
    if (dep[p[x][i]] >= dep[y]) {
      _min[x][i] = min(_min[x][i], val);
      x = p[x][i];
    }
  }
  if (x == y) return;
  for (int i = 18; i >= 0; --i) {
    if (p[x][i] != p[y][i]) {
      _min[x][i] = min(_min[x][i], val);
      _min[y][i] = min(_min[y][i], val);
      x = p[x][i];
      y = p[y][i];
    }
  }
  _min[x][0] = min(_min[x][0], val);
  _min[y][0] = min(_min[y][0], val);
}
int ans[maxn];
void solve() {
  memset(ans, INF, sizeof ans);
  memset(_min, INF, sizeof _min);
  for (int i = 0; i < m; ++i) {
    if (!flag[i]) {
      ans[eg[i].id] = LCA(eg[i].u, eg[i].v) - 1;
      update(eg[i].u, eg[i].v, eg[i].w - 1);
    }
  }
  for (int i = 17; i >= 0; --i)
    for (int j = 1; j <= n; ++j) {
      _min[j][i] = min(_min[j][i], _min[j][i + 1]);
      _min[p[j][i]][i] = min(_min[p[j][i]][i], _min[j][i + 1]);
    }
  for (int i = 0; i < m; ++i) {
    if (flag[i]) {
      int u = eg[i].u, v = eg[i].v;
      if (p[u][0] == v) {
        ans[eg[i].id] = _min[u][0];
      } else
        ans[eg[i].id] = _min[v][0];
    }
  }
  for (int i = 0; i < m; ++i)
    if (ans[i] == INF)
      cout << -1 << " ";
    else
      cout << ans[i] << " ";
  puts("");
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d%d", &eg[i].u, &eg[i].v, &eg[i].w);
    eg[i].id = i;
  }
  sort(eg, eg + m);
  for (int i = 0; i <= n; ++i) pre[i] = i;
  for (int i = 0; i < m; ++i) {
    int u = eg[i].u, v = eg[i].v;
    int uu = find(u), vv = find(v);
    if (uu != vv) {
      flag[i] = 1;
      pre[uu] = vv;
      add(u, v, eg[i].w);
      add(v, u, eg[i].w);
    }
  }
  dep[1] = 1;
  dfs(1, -1);
  initLCA();
  solve();
  return 0;
}
