#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 200100;
const int maxm = 200100;
const int max0 = 18;
int n, m;
struct data {
  int first, second, w, id;
  data() {}
  data(int _x, int _y, int _w, int _id)
      : first(_x), second(_y), w(_w), id(_id) {}
  friend bool operator<(const data &first, const data &second) {
    return first.w < second.w;
  }
};
data e[maxm + 5];
vector<pair<int, int> > adj[maxn + 5];
bool on[maxn + 5];
int pid[maxn + 5];
int ans[maxm + 5];
int pa[maxn + 5];
int get_pa(int first) {
  return pa[first] == first ? first : pa[first] = get_pa(pa[first]);
}
int fa[max0][maxn + 5];
int Max[max0][maxn + 5];
int dfn[maxn + 5], End[maxn + 5];
int dfs_tot;
void dfs(int first, int f = -1) {
  dfn[first] = dfs_tot++;
  fa[0][first] = f;
  for (auto second : adj[first])
    if (second.first != f) {
      Max[0][second.first] = e[second.second].w;
      pid[second.first] = second.second;
      dfs(second.first, first);
    }
  End[first] = dfs_tot;
}
inline void find_tree() {
  for (int i = (0), i_end_ = (n); i < i_end_; ++i) pa[i] = i;
  sort(e, e + m);
  for (int i = (0), i_end_ = (m); i < i_end_; ++i) {
    int first = e[i].first, second = e[i].second;
    if (get_pa(first) != get_pa(second)) {
      on[i] = 1;
      pa[get_pa(first)] = get_pa(second);
      adj[first].push_back(make_pair(second, i)),
          adj[second].push_back(make_pair(first, i));
    } else
      on[i] = 0;
  }
  dfs(0);
  for (int i = (1), i_end_ = (max0); i < i_end_; ++i) {
    for (int j = (0), j_end_ = (n); j < j_end_; ++j)
      if (~fa[i - 1][j])
        fa[i][j] = fa[i - 1][fa[i - 1][j]],
        Max[i][j] = max(Max[i - 1][j], Max[i - 1][fa[i - 1][j]]);
      else
        fa[i][j] = fa[i - 1][j], Max[i][j] = Max[i - 1][j];
  }
}
int bel[maxn + 5];
int get_bel(int first) {
  return bel[first] == first ? first : bel[first] = get_bel(bel[first]);
}
inline bool is_anc(int first, int second) {
  return dfn[first] <= dfn[second] && dfn[second] < End[first];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (0), i_end_ = (m); i < i_end_; ++i) {
    scanf("%d%d%d", &e[i].first, &e[i].second, &e[i].w), --e[i].first,
        --e[i].second;
    e[i].id = i;
  }
  find_tree();
  memset(ans, oo, sizeof ans);
  for (int i = (0), i_end_ = (n); i < i_end_; ++i) bel[i] = i;
  for (int i = (0), i_end_ = (m); i < i_end_; ++i)
    if (!on[i]) {
      {
        ans[e[i].id] = 0;
        int u = e[i].first, v = e[i].second;
        for (int k = (0), k_end_ = (2); k < k_end_; ++k) {
          for (int j = max0 - 1; j >= 0; --j)
            if (~fa[j][u] && !is_anc(fa[j][u], v))
              chkmax(ans[e[i].id], Max[j][u] - 1), u = fa[j][u];
          if (!is_anc(u, v)) chkmax(ans[e[i].id], Max[0][u] - 1), u = fa[0][u];
          swap(u, v);
        }
      }
      {
        int u = e[i].first, v = e[i].second;
        for (int k = (0), k_end_ = (2); k < k_end_; ++k) {
          u = get_bel(u);
          while (!is_anc(u, v)) {
            chkmin(ans[e[pid[u]].id], e[i].w - 1);
            u = bel[u] = get_bel(fa[0][u]);
          }
          swap(u, v);
        }
      }
    }
  for (int i = (0), i_end_ = (m); i < i_end_; ++i)
    if (ans[i] < oo)
      printf("%d ", ans[i]);
    else
      printf("-1 ");
  return 0;
}

### Prompt

Please provide a cpp coded solution to the problem described below:

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>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 200100;
const int maxm = 200100;
const int max0 = 18;
int n, m;
struct data {
  int first, second, w, id;
  data() {}
  data(int _x, int _y, int _w, int _id)
      : first(_x), second(_y), w(_w), id(_id) {}
  friend bool operator<(const data &first, const data &second) {
    return first.w < second.w;
  }
};
data e[maxm + 5];
vector<pair<int, int> > adj[maxn + 5];
bool on[maxn + 5];
int pid[maxn + 5];
int ans[maxm + 5];
int pa[maxn + 5];
int get_pa(int first) {
  return pa[first] == first ? first : pa[first] = get_pa(pa[first]);
}
int fa[max0][maxn + 5];
int Max[max0][maxn + 5];
int dfn[maxn + 5], End[maxn + 5];
int dfs_tot;
void dfs(int first, int f = -1) {
  dfn[first] = dfs_tot++;
  fa[0][first] = f;
  for (auto second : adj[first])
    if (second.first != f) {
      Max[0][second.first] = e[second.second].w;
      pid[second.first] = second.second;
      dfs(second.first, first);
    }
  End[first] = dfs_tot;
}
inline void find_tree() {
  for (int i = (0), i_end_ = (n); i < i_end_; ++i) pa[i] = i;
  sort(e, e + m);
  for (int i = (0), i_end_ = (m); i < i_end_; ++i) {
    int first = e[i].first, second = e[i].second;
    if (get_pa(first) != get_pa(second)) {
      on[i] = 1;
      pa[get_pa(first)] = get_pa(second);
      adj[first].push_back(make_pair(second, i)),
          adj[second].push_back(make_pair(first, i));
    } else
      on[i] = 0;
  }
  dfs(0);
  for (int i = (1), i_end_ = (max0); i < i_end_; ++i) {
    for (int j = (0), j_end_ = (n); j < j_end_; ++j)
      if (~fa[i - 1][j])
        fa[i][j] = fa[i - 1][fa[i - 1][j]],
        Max[i][j] = max(Max[i - 1][j], Max[i - 1][fa[i - 1][j]]);
      else
        fa[i][j] = fa[i - 1][j], Max[i][j] = Max[i - 1][j];
  }
}
int bel[maxn + 5];
int get_bel(int first) {
  return bel[first] == first ? first : bel[first] = get_bel(bel[first]);
}
inline bool is_anc(int first, int second) {
  return dfn[first] <= dfn[second] && dfn[second] < End[first];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (0), i_end_ = (m); i < i_end_; ++i) {
    scanf("%d%d%d", &e[i].first, &e[i].second, &e[i].w), --e[i].first,
        --e[i].second;
    e[i].id = i;
  }
  find_tree();
  memset(ans, oo, sizeof ans);
  for (int i = (0), i_end_ = (n); i < i_end_; ++i) bel[i] = i;
  for (int i = (0), i_end_ = (m); i < i_end_; ++i)
    if (!on[i]) {
      {
        ans[e[i].id] = 0;
        int u = e[i].first, v = e[i].second;
        for (int k = (0), k_end_ = (2); k < k_end_; ++k) {
          for (int j = max0 - 1; j >= 0; --j)
            if (~fa[j][u] && !is_anc(fa[j][u], v))
              chkmax(ans[e[i].id], Max[j][u] - 1), u = fa[j][u];
          if (!is_anc(u, v)) chkmax(ans[e[i].id], Max[0][u] - 1), u = fa[0][u];
          swap(u, v);
        }
      }
      {
        int u = e[i].first, v = e[i].second;
        for (int k = (0), k_end_ = (2); k < k_end_; ++k) {
          u = get_bel(u);
          while (!is_anc(u, v)) {
            chkmin(ans[e[pid[u]].id], e[i].w - 1);
            u = bel[u] = get_bel(fa[0][u]);
          }
          swap(u, v);
        }
      }
    }
  for (int i = (0), i_end_ = (m); i < i_end_; ++i)
    if (ans[i] < oo)
      printf("%d ", ans[i]);
    else
      printf("-1 ");
  return 0;
}

```