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;
int n, m, tree[4 * 200005], lazy[4 * 200005], h[200005], size[200005], sz, pos,
    par[200005][25], maxi[200005][25], chainno, chainhead[200005],
    chainindex[200005], posinbasearray[200005], dsu_par[200005], in_mst[200005],
    ans[200005];
vector<pair<int, int> > mst_tree[200005];
vector<pair<pair<int, int>, pair<int, int> > > edges;
int find(int x) {
  if (dsu_par[x] == x) return x;
  return dsu_par[x] = find(dsu_par[x]);
}
void build(int node, int a, int b) {
  tree[node] = 2000000000;
  lazy[node] = 2000000000;
  if (a == b) return;
  int mid = (a + b) / 2;
  build(2 * node, a, mid);
  build(2 * node + 1, mid + 1, b);
}
void update(int node, int a, int b, int l, int r, int val) {
  if (lazy[node] != 2000000000) {
    tree[node] = min(tree[node], lazy[node]);
    if (a != b) {
      lazy[2 * node] = min(lazy[2 * node], lazy[node]);
      lazy[2 * node + 1] = min(lazy[2 * node + 1], lazy[node]);
    }
    lazy[node] = 2000000000;
  }
  if (a > b || a > r || b < l) return;
  if (a >= l && b <= r) {
    tree[node] = min(tree[node], val);
    if (a != b) {
      lazy[2 * node] = min(lazy[2 * node], val);
      lazy[2 * node + 1] = min(lazy[2 * node + 1], val);
    }
    return;
  }
  int mid = (a + b) / 2;
  update(2 * node, a, mid, l, r, val);
  update(2 * node + 1, mid + 1, b, l, r, val);
  tree[node] = min(tree[2 * node], tree[2 * node + 1]);
}
int query(int node, int a, int b, int l, int r) {
  if (lazy[node] != 2000000000) {
    tree[node] = min(tree[node], lazy[node]);
    if (a != b) {
      lazy[2 * node] = min(lazy[2 * node], lazy[node]);
      lazy[2 * node + 1] = min(lazy[2 * node + 1], lazy[node]);
    }
    lazy[node] = 2000000000;
  }
  if (a > b || a > r || b < l) return 2000000000;
  if (a >= l && b <= r) return tree[node];
  int mid = (a + b) / 2;
  return min(query(2 * node, a, mid, l, r),
             query(2 * node + 1, mid + 1, b, l, r));
}
void dfs1(int cur, int p, int wt) {
  if (p != -1)
    h[cur] = h[p] + 1;
  else
    h[cur] = h[p];
  par[cur][0] = p;
  maxi[cur][0] = wt;
  size[cur] = 1;
  for (int i = 1; i < 20; i++) {
    if (par[cur][i - 1] != -1) {
      par[cur][i] = par[par[cur][i - 1]][i - 1];
      maxi[cur][i] = max(maxi[cur][i - 1], maxi[par[cur][i - 1]][i - 1]);
    }
  }
  for (int i = 0; i < mst_tree[cur].size(); i++) {
    int x = mst_tree[cur][i].first;
    int y = mst_tree[cur][i].second;
    if (x == p) continue;
    dfs1(x, cur, y);
    size[cur] += size[x];
  }
}
int lca(int v, int u) {
  if (h[v] < h[u]) swap(v, u);
  for (int i = 20 - 1; i >= 0; i--) {
    if (par[v][i] != -1 && h[par[v][i]] >= h[u]) {
      v = par[v][i];
    }
  }
  if (v == u) return u;
  for (int i = 20 - 1; i >= 0; i--) {
    if (par[v][i] - par[u][i]) v = par[v][i], u = par[u][i];
  }
  return par[v][0];
}
int get_max_on_anc_path(int child, int parent) {
  if (child == parent) return 0;
  int ans = 0;
  for (int i = 20 - 1; i >= 0; i--) {
    if (par[child][i] != -1 && h[par[child][i]] >= h[parent]) {
      ans = max(ans, maxi[child][i]);
      child = par[child][i];
    }
  }
  assert(child == parent);
  return ans;
}
int get_max_on_path(int x, int y) {
  int lc = lca(x, y);
  return max(get_max_on_anc_path(x, lc), get_max_on_anc_path(y, lc));
}
void hld(int cur, int p, int wt) {
  if (chainhead[chainno] == -1) chainhead[chainno] = cur;
  posinbasearray[cur] = pos;
  pos++;
  chainindex[cur] = chainno;
  int sc = 0;
  int cost;
  for (int i = 0; i < mst_tree[cur].size(); i++) {
    if (mst_tree[cur][i].first == p) continue;
    if (size[mst_tree[cur][i].first] > size[sc]) {
      sc = mst_tree[cur][i].first;
      cost = mst_tree[cur][i].second;
    }
  }
  if (sc) hld(sc, cur, cost);
  for (int i = 0; i < mst_tree[cur].size(); i++) {
    if (mst_tree[cur][i].first == p || mst_tree[cur][i].first == sc) continue;
    chainno++;
    hld(mst_tree[cur][i].first, cur, mst_tree[cur][i].second);
  }
}
void update_path(int child, int parent, int val) {
  int parent_chain = chainindex[parent];
  while (1) {
    int child_chain = chainindex[child];
    if (child_chain == parent_chain) {
      update(1, 1, sz, posinbasearray[parent] + 1, posinbasearray[child], val);
      break;
    }
    update(1, 1, sz, posinbasearray[chainhead[child_chain]],
           posinbasearray[child], val);
    child = chainhead[child_chain];
    child = par[child][0];
  }
}
int main() {
  ios::sync_with_stdio(false);
  ;
  cin.tie(0);
  ;
  int i, j, x, y, xp, yp, z, id, lc;
  memset(par, -1, sizeof par);
  cin >> n >> m;
  for (i = 0; i <= n; i++) chainhead[i] = -1;
  for (i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    edges.push_back({{z, i}, {x, y}});
  }
  sort(edges.begin(), edges.end());
  for (i = 1; i <= n; i++) dsu_par[i] = i;
  for (auto it : edges) {
    x = it.second.first;
    y = it.second.second;
    z = it.first.first;
    id = it.first.second;
    xp = find(x);
    yp = find(y);
    if (xp == yp)
      in_mst[id] = 0;
    else {
      in_mst[id] = 1;
      dsu_par[xp] = yp;
      mst_tree[x].push_back({y, z});
      mst_tree[y].push_back({x, z});
    }
  }
  dfs1(1, -1, 0);
  for (auto it : edges) {
    x = it.second.first;
    y = it.second.second;
    z = it.first.first;
    id = it.first.second;
    if (in_mst[id]) continue;
    ans[id] = get_max_on_path(x, y);
  }
  pos = 1;
  chainno = 1;
  hld(1, -1, 0);
  sz = pos - 1;
  build(1, 1, sz);
  for (auto it : edges) {
    x = it.second.first;
    y = it.second.second;
    z = it.first.first;
    id = it.first.second;
    if (in_mst[id]) continue;
    lc = lca(x, y);
    update_path(x, lc, z);
    update_path(y, lc, z);
  }
  for (auto it : edges) {
    x = it.second.first;
    y = it.second.second;
    z = it.first.first;
    id = it.first.second;
    if (!in_mst[id]) continue;
    if (h[x] > h[y]) swap(x, y);
    ans[id] = query(1, 1, sz, posinbasearray[y], posinbasearray[y]);
  }
  for (i = 1; i <= m; i++) {
    if (ans[i] == 2000000000) ans[i] = 0;
    cout << ans[i] - 1 << " ";
  }
  return 0;
}
