#include <bits/stdc++.h>
using namespace std;
const int MaxN = 200010;
bool is_in_MST[MaxN];
int jump[MaxN][18], max_l[MaxN][18], fa[MaxN];
int result[MaxN], fir[MaxN], total, rep[MaxN], n, m, depth[MaxN];
struct Edge {
  int u, v, value, id;
} edge[MaxN];
struct AdjacencyList {
  int end, length, next, id;
} AL, alis[2 * MaxN];
bool comp(const Edge &a, const Edge &b) { return a.value < b.value; }
void build(int u, int v, int l, int p) {
  alis[++total].next = fir[u];
  fir[u] = total;
  alis[total].end = v;
  alis[total].length = l;
  alis[total].id = p;
}
int query(int u, int v) {
  int ret = -1;
  if (depth[u] < depth[v]) swap(u, v);
  for (int i = 17; i >= 0; --i)
    if (depth[jump[u][i]] >= depth[v]) {
      ret = max(ret, max_l[u][i]);
      u = jump[u][i];
    }
  if (u == v) return ret;
  for (int i = 17; i >= 0; --i)
    if (jump[u][i] != jump[v][i]) {
      ret = max(ret, max_l[u][i]);
      ret = max(ret, max_l[v][i]);
      u = jump[u][i];
      v = jump[v][i];
    }
  ret = max(ret, max_l[u][0]);
  ret = max(ret, max_l[v][0]);
  return ret;
}
void DFS_multiplication(int x) {
  for (int u = fir[x]; u; u = alis[u].next)
    if (!depth[alis[u].end]) {
      depth[alis[u].end] = depth[x] + 1;
      jump[alis[u].end][0] = x;
      max_l[alis[u].end][0] = alis[u].length;
      rep[alis[u].end] = alis[u].id;
      DFS_multiplication(alis[u].end);
    }
}
int get_parent(int x) {
  if (fa[x] == x) return x;
  return fa[x] = get_parent(fa[x]);
}
void update(int u, int v, int value) {
  int lca, origin_u = u, origin_v = v;
  if (depth[u] < depth[v]) swap(u, v);
  for (int i = 17; i >= 0; --i)
    if (depth[jump[u][i]] >= depth[v]) u = jump[u][i];
  for (int i = 17; i >= 0; --i)
    if (jump[u][i] != jump[v][i]) {
      u = jump[u][i];
      v = jump[v][i];
    }
  if (u != v)
    lca = jump[u][0];
  else
    lca = u;
  u = get_parent(origin_u);
  v = get_parent(origin_v);
  for (; depth[u] > depth[lca]; u = get_parent(u)) {
    result[rep[u]] = value - 1;
    fa[u] = jump[u][0];
  }
  for (; depth[v] > depth[lca]; v = get_parent(v)) {
    result[rep[v]] = value - 1;
    fa[v] = jump[v][0];
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].value);
    edge[i].id = i;
  }
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 0; i <= 17; ++i)
    for (int j = 0; j <= n; ++j) max_l[j][i] = -1;
  sort(edge + 1, edge + 1 + m, comp);
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= m; ++i)
    if (get_parent(edge[i].u) != get_parent(edge[i].v)) {
      fa[get_parent(edge[i].u)] = get_parent(edge[i].v);
      is_in_MST[i] = 1;
      build(edge[i].u, edge[i].v, edge[i].value, edge[i].id);
      build(edge[i].v, edge[i].u, edge[i].value, edge[i].id);
    }
  depth[1] = 1;
  DFS_multiplication(1);
  for (int i = 1; i <= 17; ++i)
    for (int j = 1; j <= n; ++j) {
      jump[j][i] = jump[jump[j][i - 1]][i - 1];
      max_l[j][i] = max(max_l[j][i - 1], max_l[jump[j][i - 1]][i - 1]);
    }
  for (int i = 1; i <= m; ++i) result[i] = -1;
  for (int i = 0; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= m; ++i)
    if (!is_in_MST[i]) {
      result[edge[i].id] = query(edge[i].u, edge[i].v) - 1;
      update(edge[i].u, edge[i].v, edge[i].value);
    }
  for (int i = 1; i <= m; ++i) printf("%d ", result[i]);
  printf("\n");
  return 0;
}
