#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int x, y, val, id;
  bool operator<(const Edge e) const { return val < e.val; }
};
int n, m;
Edge e[200010];
int tid[200010];
int ans[200010];
bool vis[200010];
void init() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++)
    scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].val), e[i].id = i;
}
int fa[200010];
int find(int x) {
  if (x != fa[x]) fa[x] = find(fa[x]);
  return fa[x];
}
void kruskal() {
  memset(vis, 0, sizeof(vis));
  sort(e, e + m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  int sum = 0;
  for (int i = 0; i < m; i++) {
    int lx = find(e[i].x), ly = find(e[i].y);
    if (lx != ly) {
      fa[lx] = ly;
      vis[e[i].id] = true;
      sum++;
      if (sum == n - 1) break;
    }
  }
}
vector<Edge> adj[200010];
int gra[200010][22 + 2], disMax[200010][22 + 2];
int dep[200010];
void initLCA() {
  for (int i = 0; i < m; i++)
    if (vis[e[i].id] == true) {
      adj[e[i].x].push_back((Edge){e[i].x, e[i].y, e[i].val, e[i].id});
      adj[e[i].y].push_back((Edge){e[i].y, e[i].x, e[i].val, e[i].id});
    }
  dep[0] = -1;
  dep[1] = 1;
}
void dfs(int u, int pre) {
  int len = adj[u].size();
  for (int i = 1; i <= 22; i++) {
    gra[u][i] = gra[gra[u][i - 1]][i - 1];
    disMax[u][i] = max(disMax[u][i - 1], disMax[gra[u][i - 1]][i - 1]);
    if (!gra[u][i]) break;
  }
  for (int i = 0; i < len; i++)
    if (adj[u][i].y != pre) {
      int to = adj[u][i].y;
      dep[to] = dep[u] + 1;
      gra[to][0] = u;
      tid[to] = adj[u][i].id;
      disMax[to][0] = adj[u][i].val;
      dfs(to, u);
    }
}
int LCA(int x, int y) {
  int Maxx = 0;
  if (dep[x] > dep[y]) swap(x, y);
  for (int i = 22; i >= 0; i--)
    if (dep[gra[y][i]] >= dep[x]) {
      Maxx = max(Maxx, disMax[y][i]);
      y = gra[y][i];
    }
  for (int i = 22; i >= 0; i--)
    if (gra[x][i] != gra[y][i]) {
      Maxx = max(Maxx, max(disMax[x][i], disMax[y][i]));
      x = gra[x][i];
      y = gra[y][i];
    }
  if (x != y) Maxx = max(Maxx, max(disMax[x][0], disMax[y][0]));
  return Maxx;
}
void GetAns() {
  int u, v, val, id;
  for (int i = 0; i <= n; i++) fa[i] = i;
  for (int i = 0; i < m; i++)
    if (!vis[e[i].id]) {
      u = e[i].x;
      v = e[i].y;
      val = e[i].val;
      id = e[i].id;
      ans[id] = LCA(u, v);
      for (;;) {
        u = find(u);
        v = find(v);
        if (u == v) break;
        if (dep[u] < dep[v]) swap(u, v);
        ans[tid[u]] = val;
        fa[u] = find(gra[u][0]);
      }
    }
}
void Print() {
  for (int i = 0; i < m; i++) printf("%d ", ans[i] - 1);
  return;
}
int main() {
  init();
  kruskal();
  initLCA();
  dfs(1, 0);
  GetAns();
  Print();
  return 0;
}
