#include <bits/stdc++.h>
using namespace std;
struct Disjoint_Set_Union {
  int a[200005];
  Disjoint_Set_Union(int n) {
    for (int i = 1; i <= n; i++) a[i] = i;
  }
  int get_root(int i) {
    if (a[i] == i)
      return i;
    else
      return a[i] = get_root(a[i]);
  }
  void merge(int i, int j) { a[get_root(i)] = get_root(j); }
  bool in_same_set(int i, int j) { return get_root(i) == get_root(j); }
};
struct Edge {
  int i;
  int j;
  int w;
};
bool operator<(Edge a, Edge b) {
  if (a.w != b.w) return a.w < b.w;
  if (a.i != b.i) return a.i < b.i;
  return a.j < b.j;
}
struct Node {
  int l, r;
  int x;
  int tag;
  Node *lc, *rc;
};
Node* new_tree(int l, int r) {
  Node* p = new Node;
  p->l = l;
  p->r = r;
  p->x = 1e9 + 1;
  p->tag = 1e9 + 1;
  int mid = (l + r) / 2;
  p->lc = l == r ? nullptr : new_tree(l, mid);
  p->rc = l == r ? nullptr : new_tree(mid + 1, r);
  return p;
}
void update_tag(Node* p) {
  p->x = min(p->x, p->tag);
  if (p->l != p->r) {
    p->lc->tag = min(p->lc->tag, p->tag);
    p->rc->tag = min(p->rc->tag, p->tag);
  }
}
void change(Node* p, int l, int r, int x) {
  update_tag(p);
  if (p->l == l && p->r == r)
    p->tag = min(p->tag, x);
  else {
    if (p->lc->r >= r)
      change(p->lc, l, r, x);
    else if (p->rc->l <= l)
      change(p->rc, l, r, x);
    else {
      change(p->lc, l, p->lc->r, x);
      change(p->rc, p->rc->l, r, x);
    }
    p->x = min(p->x, x);
  }
}
int query(Node* p, int i) {
  update_tag(p);
  if (p->l == i && p->r == i)
    return p->x;
  else if (p->lc->r >= i)
    return query(p->lc, i);
  else
    return query(p->rc, i);
}
vector<Edge> mst_G[200005];
set<Edge> mst_edge;
void get_mst(const vector<Edge>& edge, int n) {
  vector<Edge> edge2 = edge;
  sort(edge2.begin(), edge2.end());
  Disjoint_Set_Union dsu(n);
  for (auto e : edge2) {
    int i = e.i;
    int j = e.j;
    if (!dsu.in_same_set(i, j)) {
      dsu.merge(i, j);
      mst_G[i].push_back(e);
      mst_G[j].push_back({j, i, e.w});
      mst_edge.insert(e);
      mst_edge.insert({j, i, e.w});
    }
  }
}
int st[200005][20];
int max_st[200005][20];
int depth[200005];
void get_st(int i, int father) {
  for (int k = 1; 1 << k <= depth[i]; k++) {
    st[i][k] = st[st[i][k - 1]][k - 1];
    max_st[i][k] = max(max_st[i][k - 1], max_st[st[i][k - 1]][k - 1]);
  }
  for (auto e : mst_G[i]) {
    int j = e.j;
    int w = e.w;
    if (j != father) {
      st[j][0] = i;
      max_st[j][0] = w;
      depth[j] = depth[i] + 1;
      get_st(j, i);
    }
  }
}
int size[200005];
int heavy_son[200005];
void get_heavy_son(int i) {
  size[i] = 1;
  for (auto e : mst_G[i])
    if (e.j != st[i][0]) {
      int j = e.j;
      get_heavy_son(j);
      size[i] += size[j];
      if (size[j] > size[heavy_son[i]]) heavy_son[i] = j;
    }
}
int chain_top[200005];
int new_id[200005];
void get_new_id(int i, int top) {
  static int now = 0;
  chain_top[i] = top;
  now++;
  new_id[i] = now;
  if (heavy_son[i] != 0) {
    get_new_id(heavy_son[i], top);
    for (auto e : mst_G[i])
      if (e.j != st[i][0] && e.j != heavy_son[i]) get_new_id(e.j, e.j);
  }
}
int get_lca(int i, int j) {
  while (chain_top[i] != chain_top[j]) {
    if (depth[chain_top[i]] < depth[chain_top[j]]) swap(i, j);
    i = st[chain_top[i]][0];
  }
  return depth[i] > depth[j] ? j : i;
}
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<Edge> edge;
  for (int _ = 1; _ <= m; _++) {
    int i, j, w;
    cin >> i >> j >> w;
    edge.push_back({i, j, w});
  }
  get_mst(edge, n);
  get_st(1, 0);
  get_heavy_son(1);
  get_new_id(1, 1);
  Node* p = new_tree(1, n);
  for (auto e : edge)
    if (!mst_edge.count(e)) {
      int i = e.i;
      int j = e.j;
      int w = e.w;
      int lca = get_lca(i, j);
      int k;
      for (k = i; chain_top[k] != chain_top[lca]; k = st[chain_top[k]][0])
        change(p, new_id[chain_top[k]], new_id[k], w);
      if (k != lca) change(p, new_id[lca] + 1, new_id[k], w);
      for (k = j; chain_top[k] != chain_top[lca]; k = st[chain_top[k]][0])
        change(p, new_id[chain_top[k]], new_id[k], w);
      if (k != lca) change(p, new_id[lca] + 1, new_id[k], w);
    }
  for (auto e : edge) {
    if (mst_edge.count(e)) {
      int ans = depth[e.i] > depth[e.j] ? query(p, new_id[e.i])
                                        : query(p, new_id[e.j]);
      if (ans > 1e9)
        cout << "-1 ";
      else
        cout << ans - 1 << ' ';
    } else {
      int lca = get_lca(e.i, e.j);
      int ans = 0;
      for (int k = e.i; depth[k] > depth[lca];
           k = st[k][(int)log2(depth[k] - depth[lca])])
        ans = max(ans, max_st[k][(int)log2(depth[k] - depth[lca])]);
      for (int k = e.j; depth[k] > depth[lca];
           k = st[k][(int)log2(depth[k] - depth[lca])])
        ans = max(ans, max_st[k][(int)log2(depth[k] - depth[lca])]);
      cout << ans - 1 << ' ';
    }
  }
}
