#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
const long long maxn = 2e5 + 5, N = 1e6 + 5, SQ = 500, base = 800287,
                mod = 998244353, INF = 1e18 + 5, lg = 20;
const long double eps = 1e-4;
struct edge {
  long long v, u, c, id;
};
set<pair<long long, long long> > se[maxn];
long long n, m, res[maxn];
vector<edge> vec, rem;
vector<pair<long long, long long> > edges[maxn];
vector<pair<long long, long long> > query[maxn];
long long par[maxn][lg], mx[maxn][lg], h[maxn];
bool cmp(edge e1, edge e2) { return e1.c < e2.c; }
long long root(long long v) {
  return (par[v][0] == v ? v : par[v][0] = root(par[v][0]));
}
void merge(edge e, long long id) {
  long long v = e.v, u = e.u;
  if ((v = root(v)) == (u = root(u))) {
    rem.push_back(e);
    return;
  }
  par[u][0] = v;
  edges[e.v].push_back({e.u, id});
  edges[e.u].push_back({e.v, id});
}
void MST() {
  long long id = 0;
  sort(vec.begin(), vec.end(), cmp);
  for (long long i = 0; i < n; ++i) {
    par[i][0] = i;
  }
  for (auto e : vec) {
    merge(e, id);
    id++;
  }
}
void dfs1(long long v, long long p = -1) {
  for (auto i : edges[v]) {
    long long u = i.first, c = vec[i.second].c;
    if (u != p) {
      par[u][0] = v, mx[u][0] = c;
      h[u] = h[v] + 1;
      for (long long i = 1; i < lg; ++i) {
        par[u][i] = par[par[u][i - 1]][i - 1];
        mx[u][i] = max(mx[u][i - 1], mx[par[u][i - 1]][i - 1]);
      }
      dfs1(u, v);
    }
  }
}
long long get_par(long long v, long long k) {
  for (long long i = lg - 1; i >= 0; --i) {
    if (k & (1ll << i)) {
      v = par[v][i];
    }
  }
  return v;
}
long long get_lca(long long v, long long u) {
  if (h[v] < h[u]) {
    swap(v, u);
  }
  if (h[v] > h[u]) {
    v = get_par(v, h[v] - h[u]);
  }
  if (v == u) {
    return v;
  }
  for (long long i = lg - 1; i >= 0; --i) {
    if (par[v][i] != par[u][i]) {
      v = par[v][i];
      u = par[u][i];
    }
  }
  return par[v][0];
}
long long get_mx(long long v, long long u, long long l) {
  long long res = 0;
  long long k1 = h[v] - h[l], k2 = h[u] - h[l];
  for (long long i = lg - 1; i >= 0; --i) {
    if (k1 & (1ll << i)) {
      res = max(res, mx[v][i]);
      v = par[v][i];
    }
    if (k2 & (1ll << i)) {
      res = max(res, mx[u][i]);
      u = par[u][i];
    }
  }
  return res;
}
void dfs2(long long v, long long p = -1, long long idx = -1) {
  for (auto x : query[v]) {
    se[v].insert(x);
  }
  for (auto i : edges[v]) {
    long long u = i.first, id = vec[i.second].id;
    if (u != p) {
      dfs2(u, v, id);
      if ((long long)se[u].size() > (long long)se[v].size()) {
        se[v].swap(se[u]);
      }
      for (auto x : se[u]) {
        if (x.second < h[v]) {
          se[v].insert(x);
        }
      }
    }
  }
  while ((long long)se[v].size() && (*se[v].begin()).second >= h[v]) {
    se[v].erase(se[v].begin());
  }
  if (idx == -1 || !(long long)se[v].size()) {
    return;
  }
  res[idx] = (*se[v].begin()).first;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < m; ++i) {
    edge e;
    cin >> e.v >> e.u >> e.c;
    e.v--, e.u--, e.id = i;
    vec.push_back(e);
  }
  MST();
  for (long long i = 0; i < n; ++i) {
    par[i][0] = 0;
  }
  dfs1(0);
  fill(res, res + maxn, INF);
  for (auto e : rem) {
    long long v = e.v, u = e.u, c = e.c;
    long long l = get_lca(v, u);
    if (l != v) query[v].push_back({c - 1, h[l]});
    if (l != u) query[u].push_back({c - 1, h[l]});
    res[e.id] = get_mx(v, u, l) - 1;
  }
  dfs2(0);
  for (long long i = 0; i < m; ++i) {
    cout << (res[i] == INF ? -1 : res[i]) << ' ';
  }
  cout << '\n';
}

### Prompt

Please formulate 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>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
const long long maxn = 2e5 + 5, N = 1e6 + 5, SQ = 500, base = 800287,
                mod = 998244353, INF = 1e18 + 5, lg = 20;
const long double eps = 1e-4;
struct edge {
  long long v, u, c, id;
};
set<pair<long long, long long> > se[maxn];
long long n, m, res[maxn];
vector<edge> vec, rem;
vector<pair<long long, long long> > edges[maxn];
vector<pair<long long, long long> > query[maxn];
long long par[maxn][lg], mx[maxn][lg], h[maxn];
bool cmp(edge e1, edge e2) { return e1.c < e2.c; }
long long root(long long v) {
  return (par[v][0] == v ? v : par[v][0] = root(par[v][0]));
}
void merge(edge e, long long id) {
  long long v = e.v, u = e.u;
  if ((v = root(v)) == (u = root(u))) {
    rem.push_back(e);
    return;
  }
  par[u][0] = v;
  edges[e.v].push_back({e.u, id});
  edges[e.u].push_back({e.v, id});
}
void MST() {
  long long id = 0;
  sort(vec.begin(), vec.end(), cmp);
  for (long long i = 0; i < n; ++i) {
    par[i][0] = i;
  }
  for (auto e : vec) {
    merge(e, id);
    id++;
  }
}
void dfs1(long long v, long long p = -1) {
  for (auto i : edges[v]) {
    long long u = i.first, c = vec[i.second].c;
    if (u != p) {
      par[u][0] = v, mx[u][0] = c;
      h[u] = h[v] + 1;
      for (long long i = 1; i < lg; ++i) {
        par[u][i] = par[par[u][i - 1]][i - 1];
        mx[u][i] = max(mx[u][i - 1], mx[par[u][i - 1]][i - 1]);
      }
      dfs1(u, v);
    }
  }
}
long long get_par(long long v, long long k) {
  for (long long i = lg - 1; i >= 0; --i) {
    if (k & (1ll << i)) {
      v = par[v][i];
    }
  }
  return v;
}
long long get_lca(long long v, long long u) {
  if (h[v] < h[u]) {
    swap(v, u);
  }
  if (h[v] > h[u]) {
    v = get_par(v, h[v] - h[u]);
  }
  if (v == u) {
    return v;
  }
  for (long long i = lg - 1; i >= 0; --i) {
    if (par[v][i] != par[u][i]) {
      v = par[v][i];
      u = par[u][i];
    }
  }
  return par[v][0];
}
long long get_mx(long long v, long long u, long long l) {
  long long res = 0;
  long long k1 = h[v] - h[l], k2 = h[u] - h[l];
  for (long long i = lg - 1; i >= 0; --i) {
    if (k1 & (1ll << i)) {
      res = max(res, mx[v][i]);
      v = par[v][i];
    }
    if (k2 & (1ll << i)) {
      res = max(res, mx[u][i]);
      u = par[u][i];
    }
  }
  return res;
}
void dfs2(long long v, long long p = -1, long long idx = -1) {
  for (auto x : query[v]) {
    se[v].insert(x);
  }
  for (auto i : edges[v]) {
    long long u = i.first, id = vec[i.second].id;
    if (u != p) {
      dfs2(u, v, id);
      if ((long long)se[u].size() > (long long)se[v].size()) {
        se[v].swap(se[u]);
      }
      for (auto x : se[u]) {
        if (x.second < h[v]) {
          se[v].insert(x);
        }
      }
    }
  }
  while ((long long)se[v].size() && (*se[v].begin()).second >= h[v]) {
    se[v].erase(se[v].begin());
  }
  if (idx == -1 || !(long long)se[v].size()) {
    return;
  }
  res[idx] = (*se[v].begin()).first;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < m; ++i) {
    edge e;
    cin >> e.v >> e.u >> e.c;
    e.v--, e.u--, e.id = i;
    vec.push_back(e);
  }
  MST();
  for (long long i = 0; i < n; ++i) {
    par[i][0] = 0;
  }
  dfs1(0);
  fill(res, res + maxn, INF);
  for (auto e : rem) {
    long long v = e.v, u = e.u, c = e.c;
    long long l = get_lca(v, u);
    if (l != v) query[v].push_back({c - 1, h[l]});
    if (l != u) query[u].push_back({c - 1, h[l]});
    res[e.id] = get_mx(v, u, l) - 1;
  }
  dfs2(0);
  for (long long i = 0; i < m; ++i) {
    cout << (res[i] == INF ? -1 : res[i]) << ' ';
  }
  cout << '\n';
}

```