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 mod = 1e9 + 7;
const int N = 1e6;
const int T = 20;
vector<pair<int, pair<int, int> > > g[N];
int n, m;
int weight[N];
int used[N];
int up[T][N];
int maxup[T][N];
int ans[N];
int tin[N];
int tout[N];
int timer = 0;
int dsu[N];
int rang[N];
int h[N];
int ind[N];
int ind1[N];
int p_up[N], p_up_ind_ver[N];
vector<vector<int> > vertexes;
vector<vector<int> > edges;
vector<vector<int> > Trees, Suf;
int _sz[N];
int pred(int a) {
  if (a == dsu[a]) return a;
  return dsu[a] = pred(dsu[a]);
}
void unite(int a, int b) {
  a = pred(a);
  b = pred(b);
  if (a != b) {
    if (rang[a] < rang[b]) {
      swap(a, b);
    }
    dsu[b] = a;
    if (rang[a] == rang[b]) {
      rang[a]++;
    }
  }
}
void dfs(int v, int p = -1) {
  _sz[v] = 1;
  tin[v] = timer++;
  for (auto to : g[v]) {
    if (to.first != p) {
      h[to.first] = h[v] + 1;
      p_up[to.first] = v;
      p_up_ind_ver[to.first] = to.second.second;
      up[0][to.first] = v;
      maxup[0][to.first] = to.second.first;
      dfs(to.first, v);
      _sz[v] += _sz[to.first];
    }
  }
  tout[v] = timer++;
}
bool pred(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; }
int lca(int a, int b) {
  if (pred(a, b)) {
    return a;
  }
  for (int j = T - 1; j >= 0; j--) {
    if (!pred(up[j][a], b)) {
      a = up[j][a];
    }
  }
  return up[0][a];
}
int max_val(int a, int b) {
  int ans = 0;
  for (int j = T - 1; j >= 0; j--) {
    if (!pred(up[j][a], b)) {
      ans = max(ans, maxup[j][a]);
      a = up[j][a];
    }
  }
  for (int j = T - 1; j >= 0; j--) {
    if (!pred(up[j][b], a)) {
      ans = max(ans, maxup[j][b]);
      b = up[j][b];
    }
  }
  if (!pred(a, b)) {
    ans = max(ans, maxup[0][a]);
  }
  if (!pred(b, a)) {
    ans = max(ans, maxup[0][b]);
  }
  return ans;
}
vector<int> ver, ed;
void dfs2(int v, int p = -1) {
  ver.push_back(v);
  for (auto to : g[v]) {
    if (to.first == p) {
      continue;
    }
    if (_sz[to.first] * 2 >= _sz[v]) {
      ed.push_back(to.second.second);
      dfs2(to.first, v);
    }
  }
  if ((int)ed.size() > 0) {
    reverse(ver.begin(), ver.end());
    reverse(ed.begin(), ed.end());
    Trees.push_back({});
    Trees[(int)vertexes.size()].resize(4 * (int)ver.size(), 1e9);
    Suf.push_back({});
    ver.pop_back();
    Suf[(int)vertexes.size()].resize((int)ver.size(), 1e9);
    for (int u = 0; u < (int)ver.size(); u++) {
      ind[ver[u]] = (int)vertexes.size();
      ind1[ver[u]] = u;
    }
    edges.push_back(ed);
    vertexes.push_back(ver);
  }
  ed.clear();
  ver.clear();
  for (auto to : g[v]) {
    if (to.first == p) {
      continue;
    }
    if (_sz[to.first] * 2 < _sz[v]) {
      dfs2(to.first, v);
    }
  }
}
void minimize(int &a, int b) { a = min(a, b); }
void update(int ind, int v, int l, int r, int al, int ar, int val) {
  if (al > ar) {
    return;
  }
  if (l >= al && r <= ar) {
    Trees[ind][v] = min(Trees[ind][v], val);
  } else if (l <= ar && r >= al) {
    update(ind, v * 2, l, (r + l) / 2, al, ar, val);
    update(ind, v * 2 + 1, (r + l) / 2 + 1, r, al, ar, val);
  }
}
int ans1(int ind, int v, int l, int r, int x) {
  if (l == r) {
    return Trees[ind][v];
  }
  if (x <= (r + l) / 2) {
    return min(Trees[ind][v], ans1(ind, v * 2, l, (r + l) / 2, x));
  }
  return min(Trees[ind][v], ans1(ind, v * 2 + 1, (r + l) / 2 + 1, r, x));
}
void go_to(int from, int to, int val) {
  int cnt = 0;
  while (from != to) {
    cnt++;
    if (ind[from] == -1) {
      minimize(ans[p_up_ind_ver[from]], val);
      from = p_up[from];
    } else if (ind[from] == ind[to]) {
      int u = ind1[from];
      int u1 = ind1[to];
      update(ind[from], 1, 0, (int)edges[ind[from]].size() - 1, u, u1 - 1, val);
      from = to;
    } else {
      int u = ind1[from];
      minimize(Suf[ind[from]][u], val);
      from = p_up[vertexes[ind[from]].back()];
    }
    assert(cnt <= 20);
  }
}
pair<int, int> wer[N];
int idx[N];
bool cmp(int a, int b) { return weight[a] < weight[b]; }
signed main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  cin >> n >> m;
  fill(ans, ans + m, 1e9);
  fill(ind, ind + n, -1);
  for (int i = 0; i < n; i++) {
    dsu[i] = i;
    rang[i] = 1;
  }
  fill(used, used + n, 0);
  for (int i = 0; i < m; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    weight[i] = w;
    idx[i] = i;
    wer[i] = {a, b};
  }
  sort(idx, idx + m, cmp);
  for (int j = 0; j < m; j++) {
    int i = idx[j];
    int a = wer[i].first;
    int b = wer[i].second;
    if (pred(a) != pred(b)) {
      used[i] = 1;
      unite(a, b);
      g[a].push_back({b, {weight[i], i}});
      g[b].push_back({a, {weight[i], i}});
    }
  }
  dfs(0);
  for (int t = 1; t < T; t++) {
    for (int i = 0; i < n; i++) {
      up[t][i] = up[t - 1][up[t - 1][i]];
      maxup[t][i] = max(maxup[t - 1][i], maxup[t - 1][up[t - 1][i]]);
    }
  }
  dfs2(0);
  for (int i = 0; i < m; i++) {
    if (used[i]) {
      continue;
    }
    int u = max_val(wer[i].first, wer[i].second);
    ans[i] = u;
    int lca_ = lca(wer[i].first, wer[i].second);
    go_to(wer[i].first, lca_, weight[i]);
    go_to(wer[i].second, lca_, weight[i]);
  }
  for (int i = 0; i < (int)edges.size(); i++) {
    int t = 1e9;
    for (int j = 0; j < (int)edges[i].size(); j++) {
      t = min(t, Suf[i][j]);
      minimize(ans[edges[i][j]], ans1(i, 1, 0, (int)edges[i].size() - 1, j));
      minimize(ans[edges[i][j]], t);
    }
  }
  for (int i = 0; i < m; i++) {
    cout << ans[i] << "\n";
  }
  return 0;
}
