#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 5, mod = 1e9 + 7, inf = 1e18;
long long n, m, a[maxn], ans[maxn], tme, st[maxn], ft[maxn], h[maxn],
    par[20][maxn], mx[20][maxn], pw2[20], p[maxn], sz[maxn], ip[maxn], mn[maxn];
vector<pair<long long, long long>> v1[maxn], v2[maxn];
vector<pair<long long, pair<long long, long long>>> adj[maxn], g[maxn];
vector<pair<pair<long long, long long>, pair<long long, long long>>> vec;
set<pair<long long, long long>> s[maxn];
bool mrk[maxn];
long long get_par(long long u) {
  if (p[u] == 0) return u;
  p[u] = get_par(p[u]);
  return p[u];
}
bool merge(long long u, long long v) {
  u = get_par(u);
  v = get_par(v);
  if (u == v) return 0;
  if (sz[u] < sz[v]) swap(u, v);
  p[v] = u;
  sz[u] += sz[v];
  return 1;
}
void dfs12(long long u) {
  st[u] = tme;
  tme++;
  for (long long i = 1; i < 20; i++) {
    long long v = par[i - 1][u];
    if ((v == 0) || (par[i - 1][v] == 0)) break;
    par[i][u] = par[i - 1][v];
    mx[i][u] = max(mx[i - 1][u], mx[i - 1][v]);
  }
  for (auto j : g[u]) {
    long long v = j.first, w = j.second.first;
    if (j.first == par[0][u]) continue;
    ip[v] = j.second.second;
    mx[0][v] = w;
    par[0][v] = u;
    h[v] = h[u] + 1ll;
    dfs12(v);
  }
  ft[u] = tme;
}
void build_mst() {
  sort(vec.begin(), vec.end());
  for (auto i : vec) {
    long long u = i.second.first, v = i.second.second, w = i.first.first;
    if (merge(u, v)) {
      g[u].push_back({v, {w, i.first.second}});
      g[v].push_back({u, {w, i.first.second}});
      mrk[i.first.second] = 1;
    }
  }
  dfs12(1);
}
long long get_pr(long long v, long long d) {
  for (long long i = 0; i < 20; i++) {
    if ((pw2[i] & d) == 0) continue;
    v = par[i][v];
  }
  return v;
}
long long get_mx(long long v, long long d) {
  long long MX = 0;
  for (long long i = 0; i < 20; i++) {
    if ((pw2[i] & d) == 0) continue;
    MX = max(MX, mx[i][v]);
    v = par[i][v];
  }
  return MX;
}
long long get_lca(long long u, long long v) {
  if (h[u] > h[v]) swap(u, v);
  if (h[u] != h[v]) v = get_pr(v, h[v] - h[u]);
  if (u == v) return u;
  for (long long i = 19; i >= 0; i--) {
    if (par[i][v] == par[i][u]) continue;
    u = par[i][u];
    v = par[i][v];
  }
  return par[0][u];
}
bool mrk2[maxn];
vector<long long> ver;
void dfs(long long u, long long prr) {
  sz[u] = 1;
  ver.push_back(u);
  st[u] = tme;
  tme++;
  mn[u] = inf;
  for (auto j : g[u]) {
    if ((j.first == prr) || (mrk2[j.first])) continue;
    long long v = j.first;
    dfs(v, u);
    sz[u] += sz[v];
  }
  ft[u] = tme;
}
long long find_cent(long long u, long long prr, long long root) {
  for (auto j : g[u]) {
    if ((j.first == prr) || (mrk2[j.first])) continue;
    long long v = j.first;
    if (sz[v] <= (sz[root] / 2ll)) continue;
    return find_cent(v, u, root);
  }
  return u;
}
void dfs2(long long u, long long prr, long long rt, long long ind) {
  for (auto j : adj[u]) {
    if (ft[j.first] == 0) continue;
    if ((st[rt] <= st[j.first]) && (ft[j.first] <= ft[rt])) continue;
    if ((rt == u) && (j.first == prr)) continue;
    mn[u] = min(mn[u], j.second.first - 1ll);
  }
  for (auto j : g[u]) {
    if ((j.first == prr) || (mrk2[j.first])) continue;
    dfs2(j.first, u, rt, j.second.second);
    mn[u] = min(mn[u], mn[j.first]);
  }
  ans[ind] = min(ans[ind], mn[u]);
}
void build_cent(long long u) {
  dfs(u, 0);
  ver.clear();
  long long v = find_cent(u, u, u);
  tme = 0;
  dfs(v, 0);
  for (auto j : g[v]) {
    if (mrk2[j.first]) continue;
    dfs2(j.first, v, j.first, j.second.second);
  }
  for (long long i : ver) {
    mn[i] = inf;
    st[i] = 0;
    ft[i] = 0;
  }
  ver.clear();
  mrk2[v] = 1;
  for (auto j : g[v]) {
    if (mrk2[j.first]) continue;
    build_cent(j.first);
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  pw2[0] = 1;
  for (long long i = 1; i < 20; i++) pw2[i] = (pw2[i - 1] * 2ll);
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    adj[u].push_back({v, {w, i}});
    adj[v].push_back({u, {w, i}});
    vec.push_back({{w, i}, {u, v}});
  }
  build_mst();
  for (auto j : vec) {
    long long u = j.second.first, v = j.second.second, w = j.first.first,
              i = j.first.second;
    if (h[u] > h[v]) swap(u, v);
    if (par[0][v] == u) {
      ans[i] = inf;
      continue;
    }
    long long lca = get_lca(u, v);
    v1[u].push_back({w, v});
    v1[v].push_back({w, u});
    v2[lca].push_back({w, v});
    v2[lca].push_back({w, u});
    ans[i] = max(get_mx(u, h[u] - h[lca]), get_mx(v, h[v] - h[lca])) - 1;
  }
  build_cent(1);
  for (long long i = 0; i < m; i++) {
    if (ans[i] > mod) ans[i] = -1;
    cout << ans[i] << ' ';
  }
}

### Prompt

In cpp, your task is to solve 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;
const long long maxn = 2e5 + 5, mod = 1e9 + 7, inf = 1e18;
long long n, m, a[maxn], ans[maxn], tme, st[maxn], ft[maxn], h[maxn],
    par[20][maxn], mx[20][maxn], pw2[20], p[maxn], sz[maxn], ip[maxn], mn[maxn];
vector<pair<long long, long long>> v1[maxn], v2[maxn];
vector<pair<long long, pair<long long, long long>>> adj[maxn], g[maxn];
vector<pair<pair<long long, long long>, pair<long long, long long>>> vec;
set<pair<long long, long long>> s[maxn];
bool mrk[maxn];
long long get_par(long long u) {
  if (p[u] == 0) return u;
  p[u] = get_par(p[u]);
  return p[u];
}
bool merge(long long u, long long v) {
  u = get_par(u);
  v = get_par(v);
  if (u == v) return 0;
  if (sz[u] < sz[v]) swap(u, v);
  p[v] = u;
  sz[u] += sz[v];
  return 1;
}
void dfs12(long long u) {
  st[u] = tme;
  tme++;
  for (long long i = 1; i < 20; i++) {
    long long v = par[i - 1][u];
    if ((v == 0) || (par[i - 1][v] == 0)) break;
    par[i][u] = par[i - 1][v];
    mx[i][u] = max(mx[i - 1][u], mx[i - 1][v]);
  }
  for (auto j : g[u]) {
    long long v = j.first, w = j.second.first;
    if (j.first == par[0][u]) continue;
    ip[v] = j.second.second;
    mx[0][v] = w;
    par[0][v] = u;
    h[v] = h[u] + 1ll;
    dfs12(v);
  }
  ft[u] = tme;
}
void build_mst() {
  sort(vec.begin(), vec.end());
  for (auto i : vec) {
    long long u = i.second.first, v = i.second.second, w = i.first.first;
    if (merge(u, v)) {
      g[u].push_back({v, {w, i.first.second}});
      g[v].push_back({u, {w, i.first.second}});
      mrk[i.first.second] = 1;
    }
  }
  dfs12(1);
}
long long get_pr(long long v, long long d) {
  for (long long i = 0; i < 20; i++) {
    if ((pw2[i] & d) == 0) continue;
    v = par[i][v];
  }
  return v;
}
long long get_mx(long long v, long long d) {
  long long MX = 0;
  for (long long i = 0; i < 20; i++) {
    if ((pw2[i] & d) == 0) continue;
    MX = max(MX, mx[i][v]);
    v = par[i][v];
  }
  return MX;
}
long long get_lca(long long u, long long v) {
  if (h[u] > h[v]) swap(u, v);
  if (h[u] != h[v]) v = get_pr(v, h[v] - h[u]);
  if (u == v) return u;
  for (long long i = 19; i >= 0; i--) {
    if (par[i][v] == par[i][u]) continue;
    u = par[i][u];
    v = par[i][v];
  }
  return par[0][u];
}
bool mrk2[maxn];
vector<long long> ver;
void dfs(long long u, long long prr) {
  sz[u] = 1;
  ver.push_back(u);
  st[u] = tme;
  tme++;
  mn[u] = inf;
  for (auto j : g[u]) {
    if ((j.first == prr) || (mrk2[j.first])) continue;
    long long v = j.first;
    dfs(v, u);
    sz[u] += sz[v];
  }
  ft[u] = tme;
}
long long find_cent(long long u, long long prr, long long root) {
  for (auto j : g[u]) {
    if ((j.first == prr) || (mrk2[j.first])) continue;
    long long v = j.first;
    if (sz[v] <= (sz[root] / 2ll)) continue;
    return find_cent(v, u, root);
  }
  return u;
}
void dfs2(long long u, long long prr, long long rt, long long ind) {
  for (auto j : adj[u]) {
    if (ft[j.first] == 0) continue;
    if ((st[rt] <= st[j.first]) && (ft[j.first] <= ft[rt])) continue;
    if ((rt == u) && (j.first == prr)) continue;
    mn[u] = min(mn[u], j.second.first - 1ll);
  }
  for (auto j : g[u]) {
    if ((j.first == prr) || (mrk2[j.first])) continue;
    dfs2(j.first, u, rt, j.second.second);
    mn[u] = min(mn[u], mn[j.first]);
  }
  ans[ind] = min(ans[ind], mn[u]);
}
void build_cent(long long u) {
  dfs(u, 0);
  ver.clear();
  long long v = find_cent(u, u, u);
  tme = 0;
  dfs(v, 0);
  for (auto j : g[v]) {
    if (mrk2[j.first]) continue;
    dfs2(j.first, v, j.first, j.second.second);
  }
  for (long long i : ver) {
    mn[i] = inf;
    st[i] = 0;
    ft[i] = 0;
  }
  ver.clear();
  mrk2[v] = 1;
  for (auto j : g[v]) {
    if (mrk2[j.first]) continue;
    build_cent(j.first);
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  pw2[0] = 1;
  for (long long i = 1; i < 20; i++) pw2[i] = (pw2[i - 1] * 2ll);
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    adj[u].push_back({v, {w, i}});
    adj[v].push_back({u, {w, i}});
    vec.push_back({{w, i}, {u, v}});
  }
  build_mst();
  for (auto j : vec) {
    long long u = j.second.first, v = j.second.second, w = j.first.first,
              i = j.first.second;
    if (h[u] > h[v]) swap(u, v);
    if (par[0][v] == u) {
      ans[i] = inf;
      continue;
    }
    long long lca = get_lca(u, v);
    v1[u].push_back({w, v});
    v1[v].push_back({w, u});
    v2[lca].push_back({w, v});
    v2[lca].push_back({w, u});
    ans[i] = max(get_mx(u, h[u] - h[lca]), get_mx(v, h[v] - h[lca])) - 1;
  }
  build_cent(1);
  for (long long i = 0; i < m; i++) {
    if (ans[i] > mod) ans[i] = -1;
    cout << ans[i] << ' ';
  }
}

```