#include <bits/stdc++.h>
struct edge {
  int node, len, id;
  edge() : node(-1), len(-1), id(-1) {}
  edge(int n, int len, int id) : node(n), len(len), id(id) {}
};
std::vector<std::vector<edge>> adjlist;
std::vector<edge> parent;
std::vector<int> height;
struct lca_elem {
  int node, max_len, min_s;
  lca_elem(int n)
      : node(parent[n].node), max_len(parent[n].len), min_s(INT_MAX) {}
  void join(lca_elem x) {
    max_len = std::max(max_len, x.max_len);
    node = x.node;
  }
  void merge(lca_elem x) { min_s = std::min(min_s, x.min_s); }
};
std::array<std::vector<lca_elem>, 19> lca;
void build_lca() {
  lca[0].clear();
  for (size_t node = 0; node < parent.size(); ++node) lca[0].emplace_back(node);
  for (size_t h = 1; h < lca.size(); ++h) {
    lca[h].clear();
    for (size_t node = 0; node < parent.size(); ++node) {
      lca[h].push_back(lca[h - 1][node]);
      if (lca[h][node].node != -1)
        lca[h][node].join(lca[h - 1][lca[h][node].node]);
    }
  }
}
void collapse_lca() {
  for (size_t h = lca.size(); h-- > 1;) {
    for (size_t node = 0; node < parent.size(); ++node) {
      lca[h - 1][node].merge(lca[h][node]);
      int a = lca[h - 1][node].node;
      if (a != -1) lca[h - 1][a].merge(lca[h][node]);
    }
  }
}
struct DSU {
  std::vector<int> parent;
  void join(int u, int v) { parent[root(u)] = parent[root(v)]; }
  int root(int i) {
    if (i == parent[i]) return i;
    return parent[i] = root(parent[i]);
  }
  DSU(int n_vertices) {
    parent.resize(n_vertices);
    for (int i = 0; i < n_vertices; i++) {
      parent[i] = i;
    }
  }
};
void root_tree(int node) {
  for (edge e : adjlist[node]) {
    int child = e.node;
    parent[child].node = node;
    parent[child].len = e.len;
    parent[child].id = e.id;
    adjlist[child].erase(
        std::remove_if(
            adjlist[child].begin(), adjlist[child].end(),
            [node](edge const& e) -> bool { return e.node == node; }),
        adjlist[child].end());
    height[child] = height[node] + 1;
    root_tree(child);
  }
}
struct free_edge {
  int u, v, len, id;
  free_edge(int u, int v, int len, int id) : u(u), v(v), len(len), id(id) {}
  bool operator<(free_edge const& e) const { return len < e.len; }
};
template <typename Function>
void eval_lca(int u, int v, Function fn) {
  if (height[u] > height[v]) std::swap(u, v);
  size_t height_delta = height[v] - height[u];
  for (size_t h = lca.size(); h-- > 0;) {
    if (height_delta < (1u << h)) continue;
    height_delta -= 1 << h;
    fn(lca[h][v]);
    v = lca[h][v].node;
  }
  assert(height[u] == height[v]);
  assert(u != -1 && v != -1);
  if (u == v) return;
  for (size_t h = lca.size(); h-- > 0;) {
    if (lca[h][u].node == lca[h][v].node) continue;
    fn(lca[h][u]);
    fn(lca[h][v]);
    u = lca[h][u].node;
    v = lca[h][v].node;
    assert(u != -1 && v != -1);
  }
  fn(lca[0][u]);
  fn(lca[0][v]);
  assert(lca[0][u].node == lca[0][v].node);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int nNode, nEdge;
  std::cin >> nNode >> nEdge;
  adjlist.resize(nNode);
  std::vector<free_edge> edges, not_in_spanning_tree;
  for (int i = 0; i < nEdge; ++i) {
    int u, v, len;
    std::cin >> u >> v >> len;
    --u;
    --v;
    edges.emplace_back(u, v, len, i);
  }
  std::sort(edges.begin(), edges.end());
  DSU dsu(nNode);
  for (free_edge e : edges) {
    if (dsu.root(e.u) == dsu.root(e.v)) {
      not_in_spanning_tree.push_back(e);
      continue;
    }
    dsu.join(e.u, e.v);
    adjlist[e.u].emplace_back(e.v, e.len, e.id);
    adjlist[e.v].emplace_back(e.u, e.len, e.id);
  }
  edges.clear();
  parent.assign(nNode, {});
  height.assign(nNode, 0);
  root_tree(0);
  build_lca();
  std::vector<int> ans_1(nEdge, INT_MAX);
  for (auto e : not_in_spanning_tree) {
    int ans_e = -1;
    eval_lca(e.u, e.v, [&](lca_elem& x) -> void {
      ans_e = std::max(ans_e, x.max_len);
      x.min_s = std::min(e.len, x.min_s);
    });
    ans_1[e.id] = ans_e;
  }
  collapse_lca();
  for (size_t i = parent.size(); i-- > 1;) {
    assert(parent[i].id != -1);
    assert((size_t)parent[i].id < ans_1.size());
    ans_1[parent[i].id] = lca[0][i].min_s;
  }
  for (int x : ans_1) {
    std::cout << (x == INT_MAX ? -1 : x - 1) << ' ';
  }
  std::cout << '\n';
}

### Prompt

Generate a cpp solution to the following problem:

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 

### Response

```cpp
#include <bits/stdc++.h>
struct edge {
  int node, len, id;
  edge() : node(-1), len(-1), id(-1) {}
  edge(int n, int len, int id) : node(n), len(len), id(id) {}
};
std::vector<std::vector<edge>> adjlist;
std::vector<edge> parent;
std::vector<int> height;
struct lca_elem {
  int node, max_len, min_s;
  lca_elem(int n)
      : node(parent[n].node), max_len(parent[n].len), min_s(INT_MAX) {}
  void join(lca_elem x) {
    max_len = std::max(max_len, x.max_len);
    node = x.node;
  }
  void merge(lca_elem x) { min_s = std::min(min_s, x.min_s); }
};
std::array<std::vector<lca_elem>, 19> lca;
void build_lca() {
  lca[0].clear();
  for (size_t node = 0; node < parent.size(); ++node) lca[0].emplace_back(node);
  for (size_t h = 1; h < lca.size(); ++h) {
    lca[h].clear();
    for (size_t node = 0; node < parent.size(); ++node) {
      lca[h].push_back(lca[h - 1][node]);
      if (lca[h][node].node != -1)
        lca[h][node].join(lca[h - 1][lca[h][node].node]);
    }
  }
}
void collapse_lca() {
  for (size_t h = lca.size(); h-- > 1;) {
    for (size_t node = 0; node < parent.size(); ++node) {
      lca[h - 1][node].merge(lca[h][node]);
      int a = lca[h - 1][node].node;
      if (a != -1) lca[h - 1][a].merge(lca[h][node]);
    }
  }
}
struct DSU {
  std::vector<int> parent;
  void join(int u, int v) { parent[root(u)] = parent[root(v)]; }
  int root(int i) {
    if (i == parent[i]) return i;
    return parent[i] = root(parent[i]);
  }
  DSU(int n_vertices) {
    parent.resize(n_vertices);
    for (int i = 0; i < n_vertices; i++) {
      parent[i] = i;
    }
  }
};
void root_tree(int node) {
  for (edge e : adjlist[node]) {
    int child = e.node;
    parent[child].node = node;
    parent[child].len = e.len;
    parent[child].id = e.id;
    adjlist[child].erase(
        std::remove_if(
            adjlist[child].begin(), adjlist[child].end(),
            [node](edge const& e) -> bool { return e.node == node; }),
        adjlist[child].end());
    height[child] = height[node] + 1;
    root_tree(child);
  }
}
struct free_edge {
  int u, v, len, id;
  free_edge(int u, int v, int len, int id) : u(u), v(v), len(len), id(id) {}
  bool operator<(free_edge const& e) const { return len < e.len; }
};
template <typename Function>
void eval_lca(int u, int v, Function fn) {
  if (height[u] > height[v]) std::swap(u, v);
  size_t height_delta = height[v] - height[u];
  for (size_t h = lca.size(); h-- > 0;) {
    if (height_delta < (1u << h)) continue;
    height_delta -= 1 << h;
    fn(lca[h][v]);
    v = lca[h][v].node;
  }
  assert(height[u] == height[v]);
  assert(u != -1 && v != -1);
  if (u == v) return;
  for (size_t h = lca.size(); h-- > 0;) {
    if (lca[h][u].node == lca[h][v].node) continue;
    fn(lca[h][u]);
    fn(lca[h][v]);
    u = lca[h][u].node;
    v = lca[h][v].node;
    assert(u != -1 && v != -1);
  }
  fn(lca[0][u]);
  fn(lca[0][v]);
  assert(lca[0][u].node == lca[0][v].node);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int nNode, nEdge;
  std::cin >> nNode >> nEdge;
  adjlist.resize(nNode);
  std::vector<free_edge> edges, not_in_spanning_tree;
  for (int i = 0; i < nEdge; ++i) {
    int u, v, len;
    std::cin >> u >> v >> len;
    --u;
    --v;
    edges.emplace_back(u, v, len, i);
  }
  std::sort(edges.begin(), edges.end());
  DSU dsu(nNode);
  for (free_edge e : edges) {
    if (dsu.root(e.u) == dsu.root(e.v)) {
      not_in_spanning_tree.push_back(e);
      continue;
    }
    dsu.join(e.u, e.v);
    adjlist[e.u].emplace_back(e.v, e.len, e.id);
    adjlist[e.v].emplace_back(e.u, e.len, e.id);
  }
  edges.clear();
  parent.assign(nNode, {});
  height.assign(nNode, 0);
  root_tree(0);
  build_lca();
  std::vector<int> ans_1(nEdge, INT_MAX);
  for (auto e : not_in_spanning_tree) {
    int ans_e = -1;
    eval_lca(e.u, e.v, [&](lca_elem& x) -> void {
      ans_e = std::max(ans_e, x.max_len);
      x.min_s = std::min(e.len, x.min_s);
    });
    ans_1[e.id] = ans_e;
  }
  collapse_lca();
  for (size_t i = parent.size(); i-- > 1;) {
    assert(parent[i].id != -1);
    assert((size_t)parent[i].id < ans_1.size());
    ans_1[parent[i].id] = lca[0][i].min_s;
  }
  for (int x : ans_1) {
    std::cout << (x == INT_MAX ? -1 : x - 1) << ' ';
  }
  std::cout << '\n';
}

```