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 long long N = 1e5 + 7, M = 1e6 + 7, mod = 1e9 + 7;
struct edge {
  int id;
  int a;
  int b;
  int w;
  edge(int c, int d, int e, int f) {
    a = c;
    b = d;
    w = e;
    id = f;
  }
};
struct dsu {
  vector<int> rep;
  dsu(int n) {
    rep.resize(n + 1);
    for (int i = 1; i <= n; i++) rep[i] = i;
  }
  int re(int a) {
    if (rep[a] != a) rep[a] = re(rep[a]);
    return rep[a];
  }
  bool uni(int a, int b) {
    a = re(a);
    b = re(b);
    if (a == b) return false;
    rep[a] = b;
    return true;
  }
};
bool cmp(const edge &a, const edge &b) {
  if (a.w < b.w) return true;
  return false;
}
vector<pair<int, int> > G[N];
int ans[M];
int gl[N], oj[N][25], ma[N][25];
void dfs1(int v) {
  for (auto u : G[v]) {
    if (oj[v][0] == u.first) continue;
    oj[u.first][0] = v;
    ma[u.first][0] = u.second;
    gl[u.first] = gl[v] + 1;
    dfs1(u.first);
  }
}
int lca(int a, int b) {
  if (gl[a] < gl[b]) swap(a, b);
  int ile = gl[a] - gl[b];
  for (int i = 0; i < 22; i++) {
    if (ile & (1 << i)) {
      a = oj[a][i];
    }
  }
  if (a == b) return a;
  for (int i = 22; i >= 0; i--) {
    if (oj[a][i] != oj[b][i]) {
      a = oj[a][i];
      b = oj[b][i];
    }
  }
  return oj[a][0];
}
int queryPozaHel(int a, int lc) {
  int g = gl[a] - gl[lc];
  int re = 0;
  for (int i = 0; i < 22; i++) {
    if (g & (1 << i)) {
      re = max(re, ma[a][i]);
      a = oj[a][i];
    }
  }
  return re;
}
int queryPoza(int a, int b) {
  int c = lca(a, b);
  return max(queryPozaHel(a, c), queryPozaHel(b, c));
}
void queryTreeHel(int a, int lc, int w) {
  int g = gl[a] - gl[lc];
  for (int i = 0; i < 22; i++) {
    if (g & (1 << i)) {
      ma[a][i] = min(ma[a][i], w);
      a = oj[a][i];
    }
  }
}
void queryTree(int a, int b, int w) {
  int lc = lca(a, b);
  queryTreeHel(a, lc, w);
  queryTreeHel(b, lc, w);
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<edge> edges;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    edges.push_back(edge(a, b, c, i));
  }
  sort(edges.begin(), edges.end(), cmp);
  dsu fu = dsu(n);
  vector<edge> tree, poza;
  for (auto u : edges) {
    if (fu.uni(u.a, u.b)) {
      G[u.a].push_back(make_pair(u.b, u.w));
      G[u.b].push_back(make_pair(u.a, u.w));
      tree.push_back(u);
    } else {
      poza.push_back(u);
    }
  }
  dfs1(1);
  oj[1][0] = 1;
  for (int j = 1; j < 23; j++) {
    for (int i = 1; i <= n; i++) {
      int pop = oj[i][j - 1];
      oj[i][j] = oj[pop][j - 1];
      ma[i][j] = max(ma[pop][j - 1], ma[i][j - 1]);
    }
  }
  fill_n(ans, m, -1);
  for (auto it : poza) {
    ans[it.id] = queryPoza(it.a, it.b);
  }
  for (int j = 0; j < 25; j++) {
    for (int i = 1; i <= n; i++) ma[i][j] = 1e9;
  }
  for (auto it : poza) {
    queryTree(it.a, it.b, it.w);
  }
  for (int j = 22; j > 0; j--) {
    for (int i = 1; i <= n; i++) {
      int pop = oj[i][j - 1];
      ma[i][j - 1] = min(ma[i][j - 1], ma[i][j]);
      ma[pop][j - 1] = min(ma[pop][j - 1], ma[i][j]);
    }
  }
  for (auto it : tree) {
    if (gl[it.a] < gl[it.b]) swap(it.a, it.b);
    ans[it.id] = ma[it.a][0];
  }
  for (int i = 0; i < m; i++) {
    if (ans[i] == -1) continue;
    cout << ans[i] << "\n";
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int qwe = 1;
  for (int i = 1; i <= qwe; i++) {
    solve();
  }
}
