You are given a connected weighted graph with n vertices and m edges. The graph doesn't contain loops nor multiple edges. Consider some edge with id i. Let's determine for this edge the maximum integer weight we can give to it so that it is contained in all minimum spanning trees of the graph if we don't change the other weights.

You are to determine this maximum weight described above for each edge. You should calculate the answer for each edge independently, it means there can't be two edges with changed weights at the same time.

Input

The first line contains two integers n and m (2 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105), where n and m are the number of vertices and the number of edges in the graph, respectively.

Each of the next m lines contains three integers u, v and c (1 ≤ v, u ≤ n, v ≠ u, 1 ≤ c ≤ 109) meaning that there is an edge between vertices u and v with weight c. 

Output

Print the answer for each edge in the order the edges are given in the input. If an edge is contained in every minimum spanning tree with any weight, print -1 as the answer.

Examples

Input

4 4
1 2 2
2 3 2
3 4 2
4 1 3


Output

2 2 2 1 

Input

4 3
1 2 2
2 3 2
3 4 2


Output

-1 -1 -1 
#include <bits/stdc++.h>
using namespace std;
const long long MXN = 1e3 + 10;
const long long MXM = (1LL << 18) + 10;
const long long MX5 = 5e5 + 10;
const long long MX6 = 1e6 + 10;
const long long LOG = 20;
const long long INF = 8e18;
const double eps = 1e-9;
const long long MOD = 1e9 + 7;
long long power(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * power(a * a % md, b / 2, md) % md
                      : power(a * a % md, b / 2, md) % md));
}
long long bmm(long long a, long long b) {
  return (a % b == 0 ? b : bmm(b, a % b));
}
string base2(long long n) {
  string a = "";
  while (n >= 2) {
    a += (char)(n % 2 + '0');
    n /= 2;
  }
  a += (char)(n + '0');
  reverse((a).begin(), (a).end());
  return a;
}
long long n, m, t;
long long U[MX5], V[MX5], W[MX5], Ans[MX5];
long long Max[MX5][LOG], Jad[MX5][LOG], dis[MX5], SZ[MX5], par[MX5];
vector<pair<long long, long long> > T[MX5], E;
vector<long long> out, R[MX5], O[MX5];
set<pair<long long, long long> > st1[MX5];
bool vis[MX5], mark[MX5];
void init() {
  for (int i = 1; i <= n; i++) SZ[i] = 1, par[i] = i;
  return;
}
long long Find(long long u) {
  return (par[u] == u ? u : par[u] = Find(par[u]));
}
bool Union(long long u, long long v) {
  long long p1 = Find(u), p2 = Find(v);
  if (p1 == p2) return 0;
  if (SZ[p2] > SZ[p1]) swap(p1, p2);
  par[p2] = p1, SZ[p1] += SZ[p2];
  return 1;
}
void dfs(long long u, long long pr, long long wpr) {
  mark[u] = 1, Jad[u][0] = pr, Max[u][0] = wpr;
  for (int i = 1; i < LOG; i++) {
    Jad[u][i] = Jad[Jad[u][i - 1]][i - 1],
    Max[u][i] = max(Max[u][i - 1], Max[Jad[u][i - 1]][i - 1]);
  }
  for (auto Pr : T[u]) {
    long long v = Pr.first, id = Pr.second;
    if (v != pr) {
      dis[v] = dis[u] + 1, dfs(v, u, W[id]);
    }
  }
  return;
}
pair<long long, long long> k_Jad(long long u, long long k) {
  string s = base2(k);
  long long p = 0, maxi = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == '1') {
      maxi = max(maxi, Max[u][p]);
      u = Jad[u][p];
    }
    p++;
  }
  return {u, maxi};
}
long long LCA(long long u, long long v) {
  if (dis[u] < dis[v]) swap(u, v);
  u = k_Jad(u, dis[u] - dis[v]).first;
  if (v == u) return v;
  for (int i = LOG - 1; i >= 0; i--) {
    if (Jad[u][i] != Jad[v][i]) {
      u = Jad[u][i], v = Jad[v][i];
    }
  }
  return Jad[v][0];
}
void DFS(long long u, long long p) {
  vis[u] = 1;
  for (auto Pr : T[u]) {
    long long v = Pr.first, id = Pr.second;
    if (v == p) continue;
    DFS(v, u);
    if (st1[v].size()) {
      Ans[id] = st1[v].begin()->first;
      if (st1[v].size() > st1[u].size()) swap(st1[u], st1[v]);
      for (auto X : st1[v]) st1[u].insert(X);
    }
  }
  for (auto id : O[u]) {
    st1[u].insert({W[id], id});
  }
  for (auto id : R[u]) {
    if (st1[u].find({W[id], id}) != st1[u].end()) {
      st1[u].erase(st1[u].find({W[id], id}));
    }
  }
  return;
}
int main() {
  cin >> n >> m;
  init();
  for (int i = 0; i < m; i++) {
    cin >> U[i] >> V[i] >> W[i];
    E.push_back({W[i], i});
  }
  sort(((E)).begin(), ((E)).end());
  for (int i = 0; i < m; i++) {
    long long id = E[i].second;
    bool f = Union(U[id], V[id]);
    if (f) {
      T[U[id]].push_back({V[id], id}), T[V[id]].push_back({U[id], id});
    } else {
      O[U[id]].push_back(id), O[V[id]].push_back(id), out.push_back(id);
    }
  }
  dfs(1, 0, 0);
  for (auto id : out) {
    long long lca = LCA(U[id], V[id]);
    long long d1 = dis[U[id]] - dis[lca], d2 = dis[V[id]] - dis[lca];
    long long maxi1 = k_Jad(U[id], d1).second, maxi2 = k_Jad(V[id], d2).second;
    Ans[id] = max(maxi1, maxi2), R[LCA(U[id], V[id])].push_back(id);
  }
  DFS(1, 0);
  for (int i = 0; i < m; i++) cout << Ans[i] - 1 << ' ';
  return 0;
}
