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;
int n, m;
vector<pair<pair<int, int>, pair<int, int> > > edges;
vector<pair<int, int> > adj[100005];
vector<int> cac[100005], bo[100005];
int loll[100005];
multiset<int> cac1;
int f[100005];
pair<int, int> anc[100005][20];
int abcd[100005];
int cnt = 0;
int pos[100005];
bool take[1000005];
int ans[1000005];
int chain[100005];
int it[400005];
int lazy[400005];
int h[100005];
pair<int, int> lca(int u, int v) {
  int yo = 0;
  if (h[u] < h[v]) swap(u, v);
  for (int i = 18; i >= 0; i--) {
    if (h[anc[u][i].first] < h[v]) continue;
    yo = max(yo, anc[u][i].second);
    u = anc[u][i].first;
  }
  if (u == v) return {v, yo};
  for (int i = 18; i >= 0; i--) {
    if (anc[u][i].first == anc[v][i].first) continue;
    yo = max(yo, anc[u][i].second);
    u = anc[u][i].first;
    yo = max(yo, anc[v][i].second);
    v = anc[v][i].first;
  }
  yo = max(yo, max(anc[u][0].second, anc[v][0].second));
  return {anc[u][0].first, yo};
}
void down(int id) {
  it[id * 2] = lazy[id];
  it[id * 2 + 1] = lazy[id];
  lazy[id * 2] = lazy[id];
  lazy[id * 2 + 1] = lazy[id];
  lazy[id] = -1;
}
void update(int id, int l, int r, int u, int v, int val) {
  if (u > r || v < l) return;
  if (l >= u && r <= v) {
    it[id] = val;
    lazy[id] = val;
    return;
  }
  if (lazy[id] != -1) down(id);
  int mid = (l + r) / 2;
  update(id * 2, l, mid, u, v, val);
  update(id * 2 + 1, mid + 1, r, u, v, val);
  it[id] = min(it[id * 2], it[id * 2 + 1]);
}
int get(int id, int l, int r, int u, int v) {
  if (u > r || v < l) return 1000000000;
  if (l >= u && r <= v) {
    return it[id];
  }
  if (lazy[id] != -1) down(id);
  int mid = (l + r) / 2;
  return min(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
void dfs(int node, int fa) {
  h[node] = h[fa] + 1;
  abcd[node]++;
  for (auto i : adj[node]) {
    if (i.first == fa) {
      anc[node][0] = i;
    } else {
      dfs(i.first, node);
      abcd[node] += abcd[i.first];
    }
  }
}
void update1(int deo, int lien, int quan, int l, int r, int val) {
  cac[l].push_back(val);
  bo[r + 1].push_back(val);
}
void updseg(int node, int fa, int val) {
  if (node == fa) return;
  while (chain[node] != chain[fa]) {
    update1(1, 1, n, pos[chain[node]], pos[node], val);
    node = anc[chain[node]][0].first;
  }
  if (node == fa) return;
  update1(1, 1, n, pos[fa] + 1, pos[node], val);
}
void hld(int node, int fat, bool head) {
  cnt++;
  pos[node] = cnt;
  if (head) {
    chain[node] = node;
  } else {
    chain[node] = chain[fat];
  }
  pair<int, int> take = {0, 0};
  for (auto i : adj[node]) {
    if (i.first == fat) continue;
    take = max(take, {abcd[i.first], i.first});
  }
  if (take.second != 0) hld(take.second, node, false);
  for (auto i : adj[node]) {
    if (i.first != fat && i.first != take.second) {
      hld(i.first, node, true);
    }
  }
}
int find(int i) { return f[i] == i ? f[i] : f[i] = find(f[i]); }
void defg(int i, int j) { f[find(i)] = find(j); }
signed main() {
  ios::sync_with_stdio(false);
  cac[1].push_back(1000000000);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 18; j++) {
      anc[i][j] = {0, 1000000000};
    }
  }
  for (int i = 1; i <= 400000; i++) {
    it[i] = 1000000000;
    lazy[i] = -1;
  }
  for (int i = 1; i <= m; i++) {
    int u, v, val;
    cin >> u >> v >> val;
    edges.push_back({{val, i}, {u, v}});
  }
  for (int i = 1; i <= n; i++) {
    f[i] = i;
  }
  sort(edges.begin(), edges.end());
  for (int i = 0; i < m; i++) {
    if (find(edges[i].second.first) != find(edges[i].second.second)) {
      defg(edges[i].second.first, edges[i].second.second);
      take[i] = true;
      adj[edges[i].second.first].emplace_back(edges[i].second.second,
                                              edges[i].first.first);
      adj[edges[i].second.second].emplace_back(edges[i].second.first,
                                               edges[i].first.first);
    }
  }
  dfs(1, 1);
  for (int j = 1; j <= 18; j++) {
    for (int i = 1; i <= n; i++) {
      anc[i][j].first = anc[anc[i][j - 1].first][j - 1].first;
      anc[i][j].second =
          max(anc[i][j - 1].second, anc[anc[i][j - 1].first][j - 1].second);
    }
  }
  hld(1, 1, 1);
  for (int i = m - 1; i >= 0; i--) {
    if (!take[i]) {
      pair<int, int> cac = lca(edges[i].second.first, edges[i].second.second);
      ans[edges[i].first.second] = cac.second;
      updseg(edges[i].second.first, cac.first, edges[i].first.first);
      updseg(edges[i].second.second, cac.first, edges[i].first.first);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (auto j : cac[i]) {
      cac1.insert(j);
    }
    for (auto j : bo[i]) {
      auto k = cac1.lower_bound(j);
      cac1.erase(k);
    }
    loll[i] = *cac1.begin();
  }
  for (int i = 0; i < m; i++) {
    if (take[i]) {
      if (h[edges[i].second.first] > h[edges[i].second.second])
        ans[edges[i].first.second] = loll[pos[edges[i].second.first]];
      else
        ans[edges[i].first.second] = loll[pos[edges[i].second.second]];
    }
  }
  for (int i = 1; i <= m; i++) {
    cout << ans[i] << endl;
  }
}
