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 MAXN = 1e5 + 5;
struct edge {
  int u, v;
  int value;
  int index;
};
bool operator<(const edge A, const edge B) {
  if (A.value != B.value) return A.value < B.value;
  return A.index < B.index;
}
int n, m;
edge allEdges[MAXN * 10];
struct dsu {
  vector<int> component[MAXN];
  vector<pair<int, int>> parent[MAXN];
  void Init(int n) {
    for (int i = 1; i <= n; i++) {
      this->component[i] = {i};
      this->parent[i] = {{i, -1}};
    }
  }
  int Find(int x) {
    if (parent[x].back().first == x) return x;
    return Find(parent[x].back().first);
  }
  bool areConnected(int u, int v) {
    u = this->Find(u);
    v = this->Find(v);
    if (u == v) return true;
    return false;
  }
  int state(int x, int value) {
    int output = -1;
    for (int i = 0; i < this->parent[x].size(); i++) {
      if (allEdges[this->parent[x][i].second].value >= value) break;
      output = this->parent[x][i].first;
    }
    return output;
  }
  void Union(int u, int v, int index) {
    u = this->Find(u);
    v = this->Find(v);
    if (u == v) return;
    if (this->component[u].size() < this->component[v].size()) swap(u, v);
    for (int x : this->component[v]) {
      this->parent[x].push_back({u, index});
      this->component[u].push_back(x);
    }
  }
};
dsu *T = new dsu();
int answer[MAXN * 10];
vector<edge> graph[MAXN];
set<pair<int, int>> inMST;
void solve1() {
  T->Init(n);
  memset(answer, -1, sizeof(answer));
  for (int i = 0; i < m; i++) {
    int u = allEdges[i].u;
    int v = allEdges[i].v;
    int value = allEdges[i].value;
    if (T->areConnected(u, v) == true) {
      int l, r, mid;
      l = 0;
      r = 1e9;
      while (l + 1 < r) {
        mid = (l + r) / 2;
        if (T->state(u, mid) != T->state(v, mid))
          l = mid;
        else
          r = mid - 1;
      }
      if (T->state(u, r) != T->state(v, r))
        answer[allEdges[i].index] = r;
      else
        answer[allEdges[i].index] = l;
    } else {
      inMST.insert({u, v});
      inMST.insert({v, u});
      T->Union(u, v, i);
    }
  }
}
int sparseParent[25][MAXN];
int depth[MAXN], parent[MAXN];
void DFSInit(int x, int last, int level) {
  depth[x] = level;
  parent[x] = last;
  for (edge e : graph[x]) {
    if (e.v == last) continue;
    DFSInit(e.v, x, level + 1);
  }
}
int biggest[MAXN];
multiset<int> ms[MAXN];
int findLCA(int u, int v) {
  if (depth[u] > depth[v]) swap(u, v);
  for (int i = 19; i >= 0; i--) {
    if (sparseParent[i][v] != -1 && depth[sparseParent[i][v]] >= depth[u])
      v = sparseParent[i][v];
  }
  if (u == v) return u;
  for (int i = 19; i >= 0; i--) {
    if (sparseParent[i][u] != sparseParent[i][v]) {
      u = sparseParent[i][u];
      v = sparseParent[i][v];
    }
  }
  return parent[u];
}
vector<int> toRemove[MAXN];
void DFS(int x, int last, int lastEdge) {
  biggest[x] = x;
  for (edge e : graph[x]) {
    if (e.v != last && inMST.count({e.u, e.v}) == true) {
      DFS(e.v, x, e.index);
      if (ms[biggest[e.v]].size() > ms[biggest[x]].size())
        biggest[x] = biggest[e.v];
    }
  }
  for (edge e : graph[x]) {
    if (e.v != last && inMST.count({e.u, e.v}) == true &&
        biggest[e.v] != biggest[x]) {
      for (int item : ms[biggest[e.v]]) ms[biggest[x]].insert(item);
      ms[biggest[e.v]].clear();
    }
  }
  for (edge e : graph[x]) {
    if (e.v != last && inMST.count({e.u, e.v}) == false &&
        findLCA(x, e.v) != x) {
      toRemove[findLCA(x, e.v)].push_back(e.value);
      ms[biggest[x]].insert(e.value);
    }
  }
  for (int val : toRemove[x]) ms[biggest[x]].erase(ms[biggest[x]].find(val));
  if (lastEdge != -1) {
    if (ms[biggest[x]].empty() == true)
      answer[lastEdge] = 1e9;
    else
      answer[lastEdge] = *(ms[biggest[x]].begin());
  }
}
void solve2() {
  for (edge e : allEdges) {
    if (inMST.count({e.u, e.v}) == true) {
      graph[e.u].push_back(e);
      swap(e.u, e.v);
      graph[e.u].push_back(e);
    }
  }
  DFSInit(1, -1, 1);
  for (int x = 1; x <= n; x++) {
    sparseParent[0][x] = parent[x];
  }
  for (int i = 1; i < 20; i++) {
    for (int x = 1; x <= n; x++) {
      if (sparseParent[i - 1][x] == -1)
        sparseParent[i][x] = -1;
      else
        sparseParent[i][x] = sparseParent[i - 1][sparseParent[i - 1][x]];
    }
  }
  for (edge e : allEdges) {
    if (inMST.count({e.u, e.v}) == false) {
      graph[e.u].push_back(e);
      swap(e.u, e.v);
      graph[e.u].push_back(e);
    }
  }
  DFS(1, -1, -1);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> allEdges[i].u >> allEdges[i].v >> allEdges[i].value;
    allEdges[i].index = i;
  }
  sort(allEdges, allEdges + m);
  solve1();
  solve2();
  for (int i = 0; i < m; i++) {
    cout << answer[i] << '\n';
  }
}
