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 inf = 1e9, maxN = 1e6 + 5;
int N, M, T, bian, bing[maxN];
int seg[maxN], tree[maxN], lazy[maxN];
int fond(int u) { return bing[u] == u ? u : bing[u] = fond(bing[u]); }
void up(int rt) { tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]); }
void down(int rt) {
  if (lazy[rt] != inf) {
    tree[rt << 1] = min(lazy[rt], tree[rt << 1]);
    tree[rt << 1 | 1] = min(tree[rt << 1 | 1], lazy[rt]);
    lazy[rt << 1] = min(lazy[rt << 1], lazy[rt]);
    lazy[rt << 1 | 1] = min(lazy[rt << 1 | 1], lazy[rt]);
    lazy[rt] = inf;
  }
}
void push_up(int rt) { seg[rt] = max(seg[rt << 1], seg[rt << 1 | 1]); }
void build(int l, int r, int rt) {
  seg[rt] = 0;
  lazy[rt] = tree[rt] = inf;
  if (l == r) return;
  int m = (l + r) >> 1;
  build(l, m, rt << 1);
  build(m + 1, r, rt << 1 | 1);
  up(rt);
}
int ask(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) return tree[rt];
  down(rt);
  int mid = (l + r) / 2, res = inf;
  if (L <= mid) res = min(res, ask(L, R, l, mid, rt * 2));
  if (R > mid) res = min(res, ask(L, R, mid + 1, r, rt * 2 + 1));
  return res;
}
void upd(int L, int R, int d, int l, int r, int rt) {
  if (L <= l && r <= R) {
    tree[rt] = min(tree[rt], d);
    lazy[rt] = min(lazy[rt], d);
    return;
  }
  down(rt);
  int mid = (l + r) / 2;
  if (L <= mid) upd(L, R, d, l, mid, rt * 2);
  if (R > mid) upd(L, R, d, mid + 1, r, rt * 2 + 1);
  up(rt);
}
int query(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) return seg[rt];
  int m = (l + r) >> 1;
  int ret = 0;
  if (L <= m) ret = max(ret, query(L, R, l, m, rt << 1));
  if (R > m) ret = max(ret, query(L, R, m + 1, r, rt << 1 | 1));
  return ret;
}
void update(int p, int x, int l, int r, int rt) {
  if (l == r) {
    seg[rt] = x;
    return;
  }
  int m = (r + l) >> 1;
  if (p <= m)
    update(p, x, l, m, rt << 1);
  else
    update(p, x, m + 1, r, rt << 1 | 1);
  push_up(rt);
}
struct Edge {
  int to, next;
} edge[maxN];
int head[maxN], tot;
int top[maxN];
int fa[maxN];
int deep[maxN];
int num[maxN];
int p[maxN];
int fp[maxN];
int son[maxN];
int pos;
void init() {
  tot = 0;
  pos = 0;
  memset(head, -1, sizeof(head));
  memset(son, -1, sizeof(son));
}
void addEdge(int u, int v) {
  edge[tot].to = v;
  edge[tot].next = head[u];
  head[u] = tot++;
}
void dfs1(int u, int pre, int d) {
  deep[u] = d;
  fa[u] = pre;
  num[u] = 1;
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].to;
    if (v != pre) {
      dfs1(v, u, d + 1);
      num[u] += num[v];
      if (son[u] == -1 || num[v] > num[son[u]]) son[u] = v;
    }
  }
}
void getPos(int u, int sp) {
  top[u] = sp;
  p[u] = pos++;
  fp[p[u]] = u;
  if (son[u] == -1) return;
  getPos(son[u], sp);
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].to;
    if (v != son[u] && v != fa[u]) getPos(v, v);
  }
}
int findMax(int u, int v) {
  int f1 = top[u], f2 = top[v];
  int tmp = 0;
  while (f1 != f2) {
    if (deep[f1] < deep[f2]) {
      swap(f1, f2);
      swap(u, v);
    }
    tmp = max(tmp, query(p[f1], p[u], 0, pos - 1, 1));
    u = fa[f1];
    f1 = top[u];
  }
  if (u == v) return tmp;
  if (deep[u] > deep[v]) swap(u, v);
  return max(tmp, query(p[son[u]], p[v], 0, pos - 1, 1));
}
void solve(int u, int v, int w) {
  int f1 = top[u], f2 = top[v];
  while (f1 != f2) {
    if (deep[f1] < deep[f2]) {
      swap(f1, f2);
      swap(u, v);
    }
    upd(p[f1], p[u], w, 0, pos - 1, 1);
    u = fa[f1];
    f1 = top[u];
  }
  if (u == v) return;
  if (deep[u] > deep[v]) swap(u, v);
  upd(p[son[u]], p[v], w, 0, pos - 1, 1);
}
int findMin(int u, int v) {
  int f1 = top[u], f2 = top[v];
  int tmp = inf;
  while (f1 != f2) {
    if (deep[f1] < deep[f2]) {
      swap(f1, f2);
      swap(u, v);
    }
    tmp = min(tmp, ask(p[f1], p[u], 0, pos - 1, 1));
    u = fa[f1];
    f1 = top[u];
  }
  if (u == v) return tmp;
  if (deep[u] > deep[v]) swap(u, v);
  return min(tmp, ask(p[son[u]], p[v], 0, pos - 1, 1));
}
struct node {
  int u, v, w, id;
  bool operator<(const node &b) const { return w < b.w; }
} e[maxN];
bool book[maxN];
int sumq = 0;
bool cmp(node aa, node bb) { return aa.id < bb.id; }
int main() {
  init();
  scanf("%d%d", &N, &bian);
  for (int i = 1; i <= N; i++) bing[i] = i;
  for (int i = 1; i <= bian; i++) {
    scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
    e[i].id = i;
  }
  sort(e + 1, e + 1 + bian);
  for (int i = 1; i <= bian; i++) {
    int uu = fond(e[i].u);
    int vv = fond(e[i].v);
    if (uu == vv) continue;
    book[e[i].id] = 1;
    bing[uu] = vv;
    addEdge(e[i].u, e[i].v);
    addEdge(e[i].v, e[i].u);
    sumq++;
    if (sumq == N - 1) break;
  }
  sort(e + 1, e + 1 + bian, cmp);
  dfs1(1, 0, 0);
  getPos(1, 1);
  build(0, pos - 1, 1);
  for (int i = 1; i <= bian; i++) {
    if (book[i] == 0) continue;
    if (deep[e[i].u] > deep[e[i].v]) swap(e[i].u, e[i].v);
    update(p[e[i].v], e[i].w, 0, pos - 1, 1);
  }
  for (int i = 1; i <= bian; i++)
    if (book[i] == 0) solve(e[i].u, e[i].v, e[i].w);
  for (int i = 1; i <= bian; i++) {
    if (book[i] == 0)
      printf("%d\n", findMax(e[i].u, e[i].v));
    else
      printf("%d\n", findMin(e[i].u, e[i].v));
  }
  return 0;
}
