With your help, Heidi has prepared a plan of trap placement and defence. Yet suddenly, the Doctor popped out of the TARDIS and told her that he had spied on the Daleks' preparations, and there is more of them than ever. Desperate times require desperate measures, so Heidi is going to risk meeting with the Daleks and she will consider placing a trap along any Corridor.

This means she needs your help again in calculating E_{max}(c) – the largest e ≤ 10^9 such that if we changed the energy requirement of c to e, then the Daleks might use c in their invasion – but this time for all Time Corridors.

Input

First line: number n of destinations, number m of corridors (2 ≤ n ≤ 10^5, n - 1 ≤ m ≤ 10^6). The next m lines: destinations a, b and energy e (1 ≤ a, b ≤ n, a ≠ b, 0 ≤ e ≤ 10^9).

No pair \\{a, b\} will repeat. The graph is guaranteed to be connected. It is not guaranteed that all energy requirements e are distinct, or that the minimum spanning tree is unique.

Output

Output m lines, each containing one integer: E_{max}(c_i) for the i-th Corridor c_i from the input.

Example

Input

3 3
1 2 8
2 3 3
3 1 4


Output

4
8
8
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
struct edge {
  int u, v, cost, id;
};
int pa[20][100005], maxi[20][100005];
int depth[100005];
int tot, eid[100005];
int fa[100005], dep[100005], sz[100005], wson[100005], top[100005],
    spos[100005], tpos[100005];
edge es[1000005], backup[1000005];
int V, E;
int p[100005], r[100005];
bool use[1000005], uu[1000005];
bool cmp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }
struct segtree {
  int mini[4 * 100005], lazy[4 * 100005];
  void pushup(int k) { mini[k] = min(mini[k * 2], mini[k * 2 + 1]); }
  void pushdown(int k) {
    if (lazy[k] == -1) return;
    for (int i = k * 2; i <= k * 2 + 1; i++) {
      lazy[i] = lazy[k];
      mini[i] = lazy[k];
    }
    lazy[k] = -1;
  }
  void build(int k, int l, int r) {
    lazy[k] = -1;
    mini[k] = 1000000000;
    if (l == r) return;
    int mid = (l + r) / 2;
    build(k * 2, l, mid);
    build(k * 2 + 1, mid + 1, r);
  }
  void update(int k, int l, int r, int x, int y, int v) {
    if (l > y || x > r) return;
    if (l >= x && r <= y) {
      lazy[k] = v;
      mini[k] = v;
      return;
    }
    pushdown(k);
    int mid = (l + r) / 2;
    update(k * 2, l, mid, x, y, v);
    update(k * 2 + 1, mid + 1, r, x, y, v);
    pushup(k);
  }
  int query(int k, int l, int r, int x, int y) {
    if (x > r || l > y) return 1000000000;
    if (l >= x && r <= y) return mini[k];
    pushdown(k);
    int mid = (l + r) / 2;
    return min(query(k * 2, l, mid, x, y), query(k * 2 + 1, mid + 1, r, x, y));
  }
} tree;
struct ee {
  int to, cost, id;
};
vector<ee> G[100005];
void dfs1(int v, int p, int d) {
  dep[v] = d;
  fa[v] = p;
  sz[v] = 1;
  for (int i = 0; i < (int)G[v].size(); i++) {
    int to = G[v][i].to;
    if (to == p) continue;
    eid[to] = G[v][i].id;
    dfs1(to, v, d + 1);
    if (sz[to] > sz[wson[v]]) wson[v] = to;
    sz[v] += sz[to];
  }
}
void dfs2(int v, int p, int num) {
  top[v] = num;
  spos[v] = ++tot;
  tpos[tot] = v;
  if (wson[v]) dfs2(wson[v], v, num);
  for (int i = 0; i < (int)G[v].size(); i++) {
    int to = G[v][i].to;
    if (to == p || to == wson[v]) continue;
    dfs2(to, v, to);
  }
}
void hld_init() {
  tot = 0;
  memset(wson, 0, sizeof(wson));
  dfs1(1, 1, 1);
  dfs2(1, 0, 1);
  tree.build(1, 1, V);
}
void hld_update(int u, int v, int w) {
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]]) swap(u, v);
    tree.update(1, 1, V, spos[top[u]], spos[u], w);
    u = fa[top[u]];
  }
  if (dep[u] < dep[v]) swap(u, v);
  tree.update(1, 1, V, spos[v] + 1, spos[u], w);
}
int hld_query(int u, int v) {
  int res = 1000000000;
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]]) swap(u, v);
    res = min(res, tree.query(1, 1, V, spos[top[u]], spos[u]));
    u = fa[top[u]];
  }
  if (dep[u] < dep[v]) swap(u, v);
  res = min(res, tree.query(1, 1, V, spos[v], spos[u]));
  return res;
}
void init_dsu(int n) {
  for (int i = 1; i <= n; i++) {
    p[i] = i;
    r[i] = 0;
  }
}
int find(int x) {
  if (p[x] == x)
    return x;
  else
    return p[x] = find(p[x]);
}
void unite(int x, int y, int w, int id) {
  int xx = x, yy = y;
  x = find(x);
  y = find(y);
  if (x == y) return;
  G[xx].push_back((ee){yy, w, id});
  G[yy].push_back((ee){xx, w, id});
  if (r[x] < r[y])
    p[x] = y;
  else {
    p[y] = x;
    if (r[x] == r[y]) r[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int kruskal() {
  sort(es, es + E, cmp);
  init_dsu(V);
  int res = 0;
  for (int i = 0; i < E; i++) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      use[e.id] = true;
      uu[i] = true;
      unite(e.u, e.v, e.cost, e.id);
      res += e.cost;
    }
  }
  return res;
}
void dfs(int v, int p, int d, int cost) {
  pa[0][v] = p;
  maxi[0][v] = cost;
  depth[v] = d;
  for (int i = 0; i < (int)G[v].size(); i++)
    if (G[v][i].to != p) dfs(G[v][i].to, v, d + 1, G[v][i].cost);
}
void init(int V) {
  dfs(1, -1, 0, -1000000000);
  for (int k = 0; k + 1 < 20; k++) {
    for (int v = 1; v <= V; v++) {
      if (pa[k][v] < 0) {
        pa[k + 1][v] = -1;
        maxi[k + 1][v] = maxi[k][v];
      } else {
        pa[k + 1][v] = pa[k][pa[k][v]];
        maxi[k + 1][v] = max(maxi[k][v], maxi[k][pa[k][v]]);
      }
    }
  }
}
pair<int, int> get(int v, int x) {
  int res = -1000000000;
  for (int k = 0; k < 20; k++)
    if ((x >> k) & 1) {
      res = max(res, maxi[k][v]);
      v = pa[k][v];
    }
  return pair<int, int>(v, res);
}
int get_ans(int u, int v) {
  if (depth[u] > depth[v]) swap(u, v);
  pair<int, int> p = get(v, depth[v] - depth[u]);
  v = p.first;
  int ans = p.second;
  if (u == v) return ans;
  for (int k = 20 - 1; k >= 0; k--) {
    if (pa[k][u] != pa[k][v]) {
      ans = max(ans, maxi[k][u]);
      ans = max(ans, maxi[k][v]);
      u = pa[k][u];
      v = pa[k][v];
    }
  }
  ans = max(ans, maxi[0][u]);
  ans = max(ans, maxi[0][v]);
  assert(ans != -1000000000);
  return ans;
}
int ans[1000005];
int main() {
  scanf("%d%d", &V, &E);
  memset(use, false, sizeof(use));
  memset(uu, false, sizeof(uu));
  for (int i = 0; i < E; i++) {
    int a, b, e;
    scanf("%d%d%d", &a, &b, &e);
    es[i] = backup[i] = (edge){a, b, e, i};
  }
  kruskal();
  init(V);
  for (int i = 0; i < E; i++) {
    if (!use[i]) ans[i] = get_ans(backup[i].u, backup[i].v);
  }
  hld_init();
  for (int i = E - 1; i >= 0; i--) {
    if (!uu[i]) hld_update(es[i].u, es[i].v, es[i].cost);
  }
  for (int i = 2; i <= V; i++) ans[eid[i]] = hld_query(i, i);
  for (int i = 0; i < E; i++) printf("%d\n", ans[i]);
  return 0;
}
