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 gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
const int MAXLG = 16;
const int MAXM = 1000000;
struct E {
  int a, b, c, id;
};
struct TE {
  int to, cost, id;
  TE() {}
  TE(int to, int cost, int id) : to(to), cost(cost), id(id) {}
};
bool operator<(const E &p, const E &q) { return p.c < q.c; }
int n, m;
E e[MAXM];
vector<TE> adj[MAXN];
bool used[MAXM];
int lim[MAXM];
int ufpar[MAXN], ufsz[MAXN];
int uffind(int a) {
  if (ufpar[a] == a) return a;
  return ufpar[a] = uffind(ufpar[a]);
}
bool ufunite(int a, int b) {
  a = uffind(a), b = uffind(b);
  if (a == b) return false;
  if (ufsz[a] < ufsz[b]) swap(a, b);
  ufpar[b] = a, ufsz[a] += ufsz[b];
  return true;
}
int par[MAXN];
int parcost[MAXN];
int parid[MAXN];
int dep[MAXN];
int up[MAXN][MAXLG + 1];
int upcost[MAXN][MAXLG + 1];
int mncover[MAXN][MAXLG + 1];
void dfsinit(int at) {
  dep[at] = par[at] == -1 ? 0 : 1 + dep[par[at]];
  up[at][0] = par[at] == -1 ? at : par[at], upcost[at][0] = parcost[at];
  for (int k = (0); k < (MAXLG); ++k)
    up[at][k + 1] = up[up[at][k]][k],
               upcost[at][k + 1] = max(upcost[at][k], upcost[up[at][k]][k]);
  for (int i = (0); i < (((int)(adj[at]).size())); ++i) {
    int to = adj[at][i].to, tocost = adj[at][i].cost, id = adj[at][i].id;
    if (to == par[at]) continue;
    par[to] = at;
    parcost[to] = tocost;
    parid[to] = id;
    dfsinit(to);
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  for (int k = MAXLG; k >= 0; --k)
    if (dep[a] - (1 << k) >= dep[b]) a = up[a][k];
  for (int k = MAXLG; k >= 0; --k)
    if (up[a][k] != up[b][k]) a = up[a][k], b = up[b][k];
  return a == b ? a : up[a][0];
}
void upd(int &a, int b) { a = min(a, b); }
int calcup(int a, int nsteps, int val) {
  int ret = 0;
  for (int k = MAXLG; k >= 0; --k)
    if (nsteps & (1 << k))
      upd(mncover[a][k], val), ret = max(ret, upcost[a][k]), a = up[a][k];
  return ret;
}
void solve() {
  sort(e, e + m);
  for (int i = (0); i < (n); ++i) ufpar[i] = i, ufsz[i] = 1;
  for (int i = (0); i < (m); ++i) used[i] = false;
  for (int i = (0); i < (m); ++i) {
    int a = e[i].a, b = e[i].b, c = e[i].c;
    if (!ufunite(a, b)) continue;
    used[e[i].id] = true;
    adj[a].push_back(TE(b, c, e[i].id));
    adj[b].push_back(TE(a, c, e[i].id));
  }
  int nused = 0;
  for (int i = (0); i < (m); ++i)
    if (used[i]) ++nused;
  assert(nused == n - 1);
  par[0] = -1;
  parcost[0] = 0;
  parid[0] = -1;
  dfsinit(0);
  for (int i = (0); i < (n); ++i)
    for (int k = (0); k <= (MAXLG); ++k) mncover[i][k] = INT_MAX;
  for (int i = (0); i < (m); ++i)
    if (!used[e[i].id]) {
      int a = e[i].a, b = e[i].b, c = lca(a, b);
      lim[e[i].id] = max(calcup(a, dep[a] - dep[c], e[i].c),
                         calcup(b, dep[b] - dep[c], e[i].c));
    }
  for (int k = MAXLG; k >= 1; --k)
    for (int i = (0); i < (n); ++i)
      if (mncover[i][k] != INT_MAX)
        upd(mncover[i][k - 1], mncover[i][k]),
            upd(mncover[up[i][k - 1]][k - 1], mncover[i][k]);
  for (int i = (0); i < (n); ++i)
    if (parid[i] != -1)
      lim[parid[i]] = mncover[i][0] == INT_MAX ? 1000000000 : mncover[i][0];
}
void run() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (m); ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    --a, --b;
    e[i].a = a, e[i].b = b, e[i].c = c, e[i].id = i;
  }
  solve();
  for (int i = (0); i < (m); ++i) printf("%d\n", lim[i]);
}
int main() {
  run();
  return 0;
}
