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;
namespace tree {
struct Edge {
  int cost;
  int id;
};
int n;
vector<Edge> edges;
vector<vector<pair<int, int>>> adj;
void makeTree(int _n) {
  n = _n;
  adj = vector<vector<pair<int, int>>>(n + 1);
}
template <typename... T>
int addEdge(int from, int to, const T&... e) {
  int id = edges.size();
  adj[from].emplace_back(to, id);
  adj[to].emplace_back(from, id);
  edges.push_back({e...});
  return id;
}
vector<int> depth, pv, pe, pre, endpre, preseq, siz;
int logn;
vector<vector<int>> anc;
struct Jump {
  int sum;
  int mn = 1000000000;
  void init(int v) {
    Edge& e = edges[pe[v]];
    sum = e.cost;
  }
  void combine(const Jump& x, const Jump& a) { sum = max(x.sum, a.sum); }
};
vector<vector<Jump>> jump;
void dfs(int x, int p = -1) {
  siz[x] = 1;
  pre[x] = preseq.size();
  preseq.push_back(x);
  for (auto& e : adj[x]) {
    int y = e.first, eid = e.second;
    if (y == p) continue;
    depth[y] = depth[x] + 1;
    pv[y] = x;
    pe[y] = eid;
    dfs(y, x);
    siz[x] += siz[y];
  }
  endpre[x] = (int)preseq.size() - 1;
}
void init(int root = 1) {
  siz = vector<int>(n + 1);
  depth = vector<int>(n + 1);
  pv = vector<int>(n + 1, -1);
  pe = vector<int>(n + 1, -1);
  pre = vector<int>(n + 1, -1);
  endpre = vector<int>(n + 1, -1);
  preseq.clear();
  dfs(root);
  logn = 31 - __builtin_clz(n);
  anc = vector<vector<int>>(n + 1, vector<int>(logn + 1));
  for (int i = 1; i <= n; ++i) anc[i][0] = pv[i];
  for (int p = 1; p <= logn; ++p)
    for (int i = 1; i <= n; ++i)
      anc[i][p] = anc[i][p - 1] == -1 ? -1 : anc[anc[i][p - 1]][p - 1];
  jump = vector<vector<Jump>>(n + 1, vector<Jump>(logn + 1));
  for (int i = 1; i <= n; ++i)
    if (anc[i][0] != -1) jump[i][0].init(i);
  for (int p = 1; p <= logn; ++p)
    for (int i = 1; i <= n; ++i)
      if (anc[i][p] != -1)
        jump[i][p].combine(jump[i][p - 1], jump[anc[i][p - 1]][p - 1]);
}
bool isAnc(int a, int b) { return pre[a] <= pre[b] && endpre[a] >= endpre[b]; }
int getChildTowards(int a, int sub) {
  int dh = depth[sub] - depth[a] - 1;
  for (int p = logn; p >= 0; --p)
    if (dh & (1 << p)) sub = anc[sub][p];
  return sub;
}
int getLca(int a, int b) {
  if (isAnc(a, b)) return a;
  if (isAnc(b, a)) return b;
  for (int p = logn; p >= 0; --p)
    if (anc[a][p] != -1 && !isAnc(anc[a][p], b)) a = anc[a][p];
  a = anc[a][0];
  return a;
}
void jumpUp(int a, int ancestor, function<void(Jump&)> f) {
  assert(isAnc(ancestor, a));
  int up = depth[a] - depth[ancestor];
  for (int p = logn; p >= 0; --p)
    if (up & (1 << p)) {
      f(jump[a][p]);
      a = anc[a][p];
    }
}
vector<int> chainRoot, chainBottom;
void dfsSiz(int x, int p = -1) {
  for (auto it = adj[x].begin(); it != adj[x].end(); ++it)
    if (it->first == p) {
      adj[x].erase(it);
      break;
    }
  siz[x] = 1;
  for (auto& e : adj[x]) {
    int y = e.first;
    dfsSiz(y, x);
    siz[x] += siz[y];
    if (siz[y] > siz[adj[x][0].first]) swap(e, adj[x][0]);
  }
}
void dfsHld(int x, int hldroot) {
  chainRoot[x] = hldroot;
  chainBottom[hldroot] = x;
  for (auto& e : adj[x]) {
    int y = e.first;
    dfsHld(y, y == adj[x][0].first ? hldroot : y);
  }
}
struct Chain {
  vector<int> vs;
  void init(int bot, int top) {
    vs.clear();
    for (int x = bot; x != pv[top]; x = pv[x]) vs.push_back(x);
  }
};
vector<Chain> chains;
void moveUp(int x, int y, function<void(Chain&, int, int)> f) {
  for (;;) {
    int z = chainRoot[x];
    bool isLastChain = z == chainRoot[y];
    f(chains[chainRoot[x]], x, isLastChain ? y : z);
    if (isLastChain) break;
    x = pv[z];
  }
}
void initHld(int root = 1) {
  siz = vector<int>(n + 1);
  dfsSiz(root);
  init(root);
  chainRoot = vector<int>(n + 1);
  chainBottom = vector<int>(n + 1);
  dfsHld(root, root);
  chains = vector<Chain>(n + 1);
  for (int i = 1; i <= n; ++i) {
    if (chainRoot[i] != i) continue;
    chains[i].init(chainBottom[i], i);
  }
}
};  // namespace tree
using namespace tree;
int mstPar[100005];
int root(int a) {
  if (a == mstPar[a]) return a;
  return mstPar[a] = root(mstPar[a]);
}
bool inMst[1000005];
int ans[1000005];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  makeTree(n);
  vector<pair<int, int>> edges(m);
  vector<pair<int, pair<int, int>>> origEdges(m);
  for (int i = 0; i < m; ++i) {
    int a, b, w;
    scanf("%d%d%d", &a, &b, &w);
    origEdges[i] = {w, {a, b}};
    edges[i] = {w, i};
  }
  sort(edges.begin(), edges.end());
  iota(mstPar, mstPar + n + 1, 0);
  int mstCnt = 0;
  for (int i = 0; i < m; ++i) {
    int a, b, w, eid;
    tie(w, eid) = edges[i];
    tie(a, b) = origEdges[eid].second;
    int ra = root(a);
    int rb = root(b);
    if (ra == rb) continue;
    mstPar[ra] = rb;
    0;
    addEdge(a, b, w, eid);
    inMst[eid] = true;
    ++mstCnt;
  }
  assert(mstCnt == n - 1);
  init();
  for (int i = 0; i < m; ++i) {
    int a, b, w = origEdges[i].first;
    tie(a, b) = origEdges[i].second;
    if (!inMst[i]) {
      int lca = getLca(a, b);
      int ret = 0;
      auto f = [&](Jump& jump) {
        ret = max(ret, jump.sum);
        jump.mn = min(jump.mn, w);
      };
      jumpUp(a, lca, f);
      jumpUp(b, lca, f);
      ans[i] = ret;
    }
  }
  for (int p = logn; p >= 1; --p)
    for (int i = 1; i <= n; ++i)
      if (anc[i][p] != -1) {
        int a = anc[i][p - 1];
        jump[i][p - 1].mn = min(jump[i][p - 1].mn, jump[i][p].mn);
        jump[a][p - 1].mn = min(jump[a][p - 1].mn, jump[i][p].mn);
      }
  for (int i = 0; i < m; ++i) {
    if (inMst[i]) {
      int a = origEdges[i].second.first, b = origEdges[i].second.second;
      if (depth[a] < depth[b]) swap(a, b);
      ans[i] = jump[a][0].mn;
    }
    printf("%d\n", ans[i]);
  }
}
