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>
using namespace std;
const int N = 1e6 + 5;
struct node {
  int to, next;
} e[N * 2];
int head[N], cnt, tim, fa[N][21], son[N], dep[N], top[N], en[N], siz[N], pos[N];
int f_mx[N * 4], mx[N * 4], mi[N * 4], f_mi[N * 4];
int n, m, rt;
void add(int x, int y) {
  e[cnt].to = y;
  e[cnt].next = head[x];
  head[x] = cnt++;
}
void dfs1(int u) {
  siz[u] = 1;
  for (int i = head[u]; ~i; i = e[i].next) {
    int v = e[i].to;
    if (v != fa[u][0]) {
      fa[v][0] = u;
      dep[v] = dep[u] + 1;
      dfs1(v);
      siz[u] += siz[v];
      if (son[u] == 0 || siz[v] > siz[son[u]]) son[u] = v;
    }
  }
}
void dfs2(int u, int f) {
  tim++;
  top[u] = f;
  pos[u] = tim;
  if (son[u]) dfs2(son[u], f);
  for (int i = head[u]; ~i; i = e[i].next) {
    int v = e[i].to;
    if (v != fa[u][0] && v != son[u]) {
      dfs2(v, v);
    }
  }
  en[u] = tim;
}
void pushdown(int root) {
  mx[root << 1] = max(mx[root << 1], f_mx[root]);
  mx[root << 1 | 1] = max(mx[root << 1 | 1], f_mx[root]);
  f_mx[root << 1] = max(f_mx[root << 1], f_mx[root]);
  f_mx[root << 1 | 1] = max(f_mx[root << 1 | 1], f_mx[root]);
  f_mx[root] = 0;
  mi[root << 1] = min(mi[root << 1], f_mi[root]);
  mi[root << 1 | 1] = min(mi[root << 1 | 1], f_mi[root]);
  f_mi[root << 1] = min(f_mi[root << 1], f_mi[root]);
  f_mi[root << 1 | 1] = min(f_mi[root << 1 | 1], f_mi[root]);
  f_mi[root] = 1e9;
}
void add(int l, int r, int root, int ql, int qr, int val, int op) {
  if (l >= ql && r <= qr) {
    if (op) {
      mx[root] = max(mx[root], val);
      f_mx[root] = max(f_mx[root], val);
    } else {
      mi[root] = min(mi[root], val);
      f_mi[root] = min(f_mi[root], val);
    }
    return;
  }
  pushdown(root);
  int mid = l + r >> 1;
  if (mid >= ql) add(l, mid, root << 1, ql, qr, val, op);
  if (mid < qr) add(mid + 1, r, root << 1 | 1, ql, qr, val, op);
  mx[root] = max(mx[root << 1], mx[root << 1 | 1]);
  mi[root] = min(mi[root << 1], mi[root << 1 | 1]);
}
int q_mx(int l, int r, int root, int ql, int qr) {
  if (l >= ql && r <= qr) return mx[root];
  int mid = l + r >> 1;
  pushdown(root);
  int ans = 0;
  if (mid >= ql) ans = q_mx(l, mid, root << 1, ql, qr);
  if (mid < qr) ans = max(ans, q_mx(mid + 1, r, root << 1 | 1, ql, qr));
  return ans;
}
int q_mi(int l, int r, int root, int p) {
  if (l == r) return mi[root];
  int mid = l + r >> 1;
  pushdown(root);
  if (mid >= p)
    return q_mi(l, mid, root << 1, p);
  else
    return q_mi(mid + 1, r, root << 1 | 1, p);
}
void update(int x, int y, int val, int op) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    add(1, n, 1, pos[top[x]], pos[x], val, op);
    x = fa[top[x]][0];
  }
  if (pos[x] > pos[y]) swap(x, y);
  add(1, n, 1, pos[x], pos[y], val, op);
}
int query(int x, int y) {
  int ans = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    ans = max(ans, q_mx(1, n, 1, pos[top[x]], pos[x]));
    x = fa[top[x]][0];
  }
  if (pos[x] > pos[y]) swap(x, y);
  ans = max(ans, q_mx(1, n, 1, pos[x], pos[y]));
  return ans;
}
struct edge {
  int x, y, v, id;
  bool operator<(const edge& a) const { return v < a.v; }
} p[N];
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 20; i >= 0; i--)
    if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
  if (x == y) return x;
  for (int i = 20; i >= 0; i--)
    if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}
int f[N], in[N];
int finds(int x) { return x == f[x] ? f[x] : f[x] = finds(f[x]); }
void deal() {
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 1; i <= n; i++) fa[i][j] = fa[fa[i][j - 1]][j - 1];
}
int ans[N];
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = 1; i <= m; i++)
    scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].v), p[i].id = i;
  dep[1] = 1;
  sort(p + 1, p + 1 + m);
  for (int i = 1; i <= m; i++) {
    int fax = finds(p[i].x);
    int fay = finds(p[i].y);
    if (fax != fay) {
      in[p[i].id] = 1;
      f[fay] = fax;
      add(p[i].x, p[i].y);
      add(p[i].y, p[i].x);
    }
  }
  dfs1(1);
  dfs2(1, 1);
  deal();
  for (int i = 1; i <= n * 4; i++) mi[i] = f_mi[i] = 1e9;
  for (int i = 1; i <= m; i++) {
    int x = p[i].x, y = p[i].y, firstson, val = p[i].v;
    int l = lca(x, y);
    if (y != l) {
      firstson = y;
      for (int i = 20; i >= 0; i--)
        if (dep[fa[firstson][i]] > dep[l]) firstson = fa[firstson][i];
      update(firstson, y, val, in[p[i].id] ? 1 : 0);
    }
    if (x != l) {
      firstson = x;
      for (int i = 20; i >= 0; i--)
        if (dep[fa[firstson][i]] > dep[l]) firstson = fa[firstson][i];
      update(firstson, x, val, in[p[i].id] ? 1 : 0);
    }
  }
  for (int i = 1; i <= m; i++) {
    if (in[p[i].id])
      ans[p[i].id] =
          q_mi(1, n, 1, dep[p[i].x] > dep[p[i].y] ? pos[p[i].x] : pos[p[i].y]);
    else {
      int x = p[i].x, y = p[i].y, firstson, val = p[i].v;
      int l = lca(x, y);
      int aa = 0;
      if (y != l) {
        firstson = y;
        for (int i = 20; i >= 0; i--)
          if (dep[fa[firstson][i]] > dep[l]) firstson = fa[firstson][i];
        aa = query(firstson, y);
      }
      if (x != l) {
        firstson = x;
        for (int i = 20; i >= 0; i--)
          if (dep[fa[firstson][i]] > dep[l]) firstson = fa[firstson][i];
        aa = max(aa, query(firstson, x));
      }
      ans[p[i].id] = aa;
    }
  }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
