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;
constexpr int kN = int(1E5 + 10), kM = int(1E6 + 10);
struct seg_tree {
  int val[kN << 2];
  void set(int n, int l, int r, int pos, int x) {
    if (l == r)
      val[n] = x;
    else {
      int mid = (l + r) >> 1;
      if (pos <= mid)
        set(n << 1, l, mid, pos, x);
      else
        set(n << 1 | 1, mid + 1, r, pos, x);
      val[n] = max(val[n << 1], val[n << 1 | 1]);
    }
    return;
  }
  int ask(int n, int l, int r, int L, int R) {
    if (L <= l && r <= R)
      return val[n];
    else if (l > R || L > r)
      return 0;
    else {
      int mid = (l + r) >> 1;
      return max(ask(n << 1, l, mid, L, R), ask(n << 1 | 1, mid + 1, r, L, R));
    }
  }
};
struct seg_tree_min {
  int val[kN << 2], v2[kN << 2], flag[kN << 2];
  void addtag(int n, int x) {
    v2[n] = val[n] = flag[n] = x;
    return;
  }
  void push(int n) {
    if (flag[n] > -1) {
      addtag(n << 1, flag[n]), addtag(n << 1 | 1, flag[n]);
      flag[n] = -1;
    }
    return;
  }
  void pull(int n) {
    val[n] = min(val[n << 1], val[n << 1 | 1]);
    v2[n] = max(v2[n << 1], v2[n << 1 | 1]);
    return;
  }
  void init(int n, int l, int r) {
    val[n] = v2[n] = 1E9;
    flag[n] = -1;
    if (l < r) {
      int mid = (l + r) >> 1;
      init(n << 1, l, mid), init(n << 1 | 1, mid + 1, r);
    }
    return;
  }
  void fix(int n, int l, int r, int L, int R, int x) {
    if ((L <= l && r <= R && val[n] > x))
      addtag(n, x);
    else if (!(l > R || L > r || v2[n] <= x)) {
      int mid = (l + r) >> 1;
      push(n);
      fix(n << 1, l, mid, L, R, x), fix(n << 1 | 1, mid + 1, r, L, R, x);
      pull(n);
    }
    return;
  }
  int ask(int n, int l, int r, int pos) {
    if (l == r)
      return val[n];
    else {
      int mid = (l + r) >> 1;
      push(n);
      if (pos <= mid)
        return ask(n << 1, l, mid, pos);
      else
        return ask(n << 1 | 1, mid + 1, r, pos);
    }
  }
};
tuple<int, int, int, int> edge[kM];
int p[kN], pe[kN], dep[kN], ans[kM], in[kN], top[kN], t, sz[kN], oew[kM];
bool used[kM];
vector<pair<int, int>> graph[kN];
seg_tree sg;
seg_tree_min sgm;
int Find(int n) { return n == p[n] ? n : p[n] = Find(p[n]); }
void Merge(tuple<int, int, int, int> tp) {
  int l = Find(get<1>(tp)), r = Find(get<2>(tp));
  if (l != r) {
    p[r] = l;
    used[get<3>(tp)] = true;
    graph[get<1>(tp)].push_back({get<2>(tp), get<3>(tp)});
    graph[get<2>(tp)].push_back({get<1>(tp), get<3>(tp)});
  }
  return;
}
void dfs(int pos, int from) {
  dep[pos] = dep[from] + 1;
  sz[pos] = 1;
  p[pos] = from;
  for (pair<int, int> i : graph[pos])
    if (i.first != from) {
      pe[i.first] = i.second;
      dfs(i.first, pos);
      sz[pos] += sz[i.first];
    }
  return;
}
void init(int pos, int from, int tp) {
  in[pos] = ++t;
  top[pos] = tp;
  int idx = 0;
  for (pair<int, int> i : graph[pos])
    if (i.first != from && sz[idx] < sz[i.first]) idx = i.first;
  if (idx) init(idx, pos, tp);
  for (pair<int, int> i : graph[pos])
    if (i.first != from && i.first != idx) init(i.first, pos, i.first);
  return;
}
int LCA(int l, int r, int x) {
  int ans = 0;
  while (top[l] != top[r]) {
    if (dep[top[l]] < dep[top[r]]) swap(l, r);
    sgm.fix(1, 1, t, in[top[l]], in[l], x);
    ans = max(ans, sg.ask(1, 1, t, in[top[l]], in[l]));
    l = p[top[l]];
  }
  if (dep[l] < dep[r]) swap(l, r);
  if (l != r) {
    sgm.fix(1, 1, t, in[r] + 1, in[l], x);
    ans = max(ans, sg.ask(1, 1, t, in[r] + 1, in[l]));
  }
  return ans;
}
int main() {
  int n, m, l, r, c;
  priority_queue<int, vector<int>, greater<int>> pq, npq;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &l, &r, &c);
    edge[i] = {c, l, r, i};
    oew[i] = c;
  }
  sort(edge + 1, edge + m + 1);
  for (int i = 1; i <= n; i++) p[i] = i;
  for (int i = 1; i <= m; i++) Merge(edge[i]);
  dfs(1, 1);
  init(1, 1, 1);
  for (int i = 1; i <= n; i++) {
    graph[i].clear();
    graph[i].shrink_to_fit();
  }
  for (int i = 2; i <= n; i++) sg.set(1, 1, t, in[i], oew[pe[i]]);
  sgm.init(1, 1, t);
  for (int i = 1; i <= m; i++)
    if (!used[get<3>(edge[i])])
      ans[get<3>(edge[i])] =
          LCA(get<1>(edge[i]), get<2>(edge[i]), get<0>(edge[i]));
  for (int i = 2; i <= n; i++) ans[pe[i]] = sgm.ask(1, 1, t, in[i]);
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
