#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 13;
int const M = 18;
int const INF = 1e9 + 13;
int n, m;
int f[N], s[N], c[N];
vector<int> eid[N];
int p[N], par[N];
vector<int> perm;
bool take[N];
int by[N];
int lca[N][M], dp[N][M], cnt, tin[N], tout[N];
int ans[N];
int le[N], ri[N];
vector<pair<int, int>> edges;
int tsz;
int getTo(int u, int id) { return (f[id] == u ? s[id] : f[id]); }
int find(int u) {
  if (p[u] == u) return u;
  return p[u] = find(p[u]);
}
bool uni(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b) return false;
  p[b] = a;
  return true;
}
bool cmp(int i, int j) { return c[i] < c[j]; }
void dfs(int u, int p) {
  ++cnt;
  tin[u] = cnt;
  par[u] = p;
  lca[u][0] = p;
  if (p != -1) {
    dp[u][0] = c[by[u]];
    for (int i = 1; i < M; ++i) {
      if (lca[u][i - 1] == -1) break;
      lca[u][i] = lca[lca[u][i - 1]][i - 1];
      dp[u][i] = max(dp[u][i - 1], dp[lca[u][i - 1]][i - 1]);
    }
  }
  for (int i = 0; i < eid[u].size(); ++i) {
    if (!take[eid[u][i]]) continue;
    int to = getTo(u, eid[u][i]);
    if (to == p) continue;
    by[to] = eid[u][i];
    dfs(to, u);
  }
  tout[u] = cnt;
}
bool isA(int a, int b) { return (tin[a] <= tin[b] && tout[a] >= tout[b]); }
int getLca(int a, int b) {
  if (isA(a, b)) return a;
  if (isA(b, a)) return b;
  for (int i = M - 1; i >= 0; --i) {
    if (lca[a][i] == -1 || isA(lca[a][i], b)) continue;
    a = lca[a][i];
  }
  return lca[a][0];
}
int getMax(int b, int t) {
  int ret = 0;
  for (int i = M - 1; i >= 0; --i) {
    if (lca[b][i] == -1 || isA(lca[b][i], t)) continue;
    ret = max(ret, dp[b][i]);
    b = lca[b][i];
  }
  if (b != t) ret = max(ret, dp[b][0]);
  return ret;
}
int getMaxPath(int a, int b) {
  int l = getLca(a, b);
  int mx1 = getMax(a, l);
  int mx2 = getMax(b, l);
  return max(mx1, mx2);
}
vector<pair<int, int>> t[N * 4];
vector<int> pref[N * 4], suf[N * 4];
void merge(vector<pair<int, int>> &v, vector<pair<int, int>> &l,
           vector<pair<int, int>> &r) {
  int lp = 0;
  int rp = 0;
  int ln = l.size();
  int rn = r.size();
  while (lp < ln || rp < rn) {
    if (rp == rn || (lp < ln && l[lp].first < r[rp].first)) {
      v.push_back(l[lp]);
      ++lp;
    } else {
      v.push_back(r[rp]);
      ++rp;
    }
  }
}
void build(int v, int tl, int tr) {
  if (tl > tr) return;
  if (tl == tr) {
    t[v].push_back(edges[tl]);
    pref[v].push_back(edges[tl].second);
    suf[v].push_back(edges[tl].second);
  } else {
    int tm = (tl + tr) / 2;
    build(v * 2 + 1, tl, tm);
    build(v * 2 + 2, tm + 1, tr);
    merge(t[v], t[v * 2 + 1], t[v * 2 + 2]);
    int n = t[v].size();
    pref[v].resize(n);
    suf[v].resize(n);
    for (int i = 0; i < n; ++i) {
      pref[v][i] = t[v][i].second;
      if (i > 0) pref[v][i] = min(pref[v][i], pref[v][i - 1]);
      suf[v][n - 1 - i] = t[v][n - 1 - i].second;
      if (i > 0)
        suf[v][n - 1 - i] = min(suf[v][n - 1 - i], suf[v][n - 1 - i + 1]);
    }
  }
}
int upper(vector<pair<int, int>> &v, int val) {
  int l = -1;
  int r = v.size();
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (v[m].first > val)
      r = m;
    else
      l = m;
  }
  return r;
}
int getMin(int v, int tl, int tr, int l, int r, int p, int s) {
  if (tl > r || l > tr) return INF;
  if (l <= tl && tr <= r) {
    int ret = INF;
    int ppos = upper(t[v], p - 1);
    --ppos;
    if (ppos >= 0) ret = min(ret, pref[v][ppos]);
    int spos = upper(t[v], s - 1);
    if (spos < suf[v].size()) ret = min(ret, suf[v][spos]);
    return ret;
  }
  int tm = (tl + tr) / 2;
  return min(getMin(v * 2 + 1, tl, tm, l, r, p, s),
             getMin(v * 2 + 2, tm + 1, tr, l, r, p, s));
}
int calcEdge(int id) {
  int a = f[id];
  int b = s[id];
  if (lca[b][0] == a) swap(a, b);
  int p = tin[a];
  int s = tout[a] + 1;
  return getMin(0, 0, tsz - 1, le[a], ri[a], p, s);
}
void dfs1(int u, int p) {
  le[u] = edges.size();
  for (int i = 0; i < eid[u].size(); ++i) {
    if (!take[eid[u][i]]) continue;
    int to = getTo(u, eid[u][i]);
    if (to == p) continue;
    dfs1(to, u);
  }
  for (int i = 0; i < eid[u].size(); ++i) {
    if (take[eid[u][i]]) continue;
    int to = getTo(u, eid[u][i]);
    if (to == p) continue;
    edges.push_back(make_pair(tin[to], c[eid[u][i]]));
  }
  ri[u] = (int)edges.size() - 1;
}
void solve() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d %d %d", &f[i], &s[i], &c[i]);
    --f[i];
    --s[i];
    eid[f[i]].push_back(i);
    eid[s[i]].push_back(i);
    perm.push_back(i);
  }
  for (int i = 0; i < N; ++i) p[i] = i;
  sort(perm.begin(), perm.end(), cmp);
  for (int i = 0; i < m; ++i) {
    if (uni(f[perm[i]], s[perm[i]])) {
      take[perm[i]] = 1;
    }
  }
  dfs(0, -1);
  dfs1(0, -1);
  tsz = edges.size();
  build(0, 0, tsz - 1);
  for (int i = 0; i < m; ++i) {
    if (take[i])
      ans[i] = calcEdge(i);
    else
      ans[i] = getMaxPath(f[i], s[i]);
    if (ans[i] == INF) ans[i] = 0;
  }
  for (int i = 0; i < m; ++i) printf("%d ", ans[i] - 1);
}
int main() {
  solve();
  return 0;
}

### Prompt

Your challenge is to write 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;
int const N = 2e5 + 13;
int const M = 18;
int const INF = 1e9 + 13;
int n, m;
int f[N], s[N], c[N];
vector<int> eid[N];
int p[N], par[N];
vector<int> perm;
bool take[N];
int by[N];
int lca[N][M], dp[N][M], cnt, tin[N], tout[N];
int ans[N];
int le[N], ri[N];
vector<pair<int, int>> edges;
int tsz;
int getTo(int u, int id) { return (f[id] == u ? s[id] : f[id]); }
int find(int u) {
  if (p[u] == u) return u;
  return p[u] = find(p[u]);
}
bool uni(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b) return false;
  p[b] = a;
  return true;
}
bool cmp(int i, int j) { return c[i] < c[j]; }
void dfs(int u, int p) {
  ++cnt;
  tin[u] = cnt;
  par[u] = p;
  lca[u][0] = p;
  if (p != -1) {
    dp[u][0] = c[by[u]];
    for (int i = 1; i < M; ++i) {
      if (lca[u][i - 1] == -1) break;
      lca[u][i] = lca[lca[u][i - 1]][i - 1];
      dp[u][i] = max(dp[u][i - 1], dp[lca[u][i - 1]][i - 1]);
    }
  }
  for (int i = 0; i < eid[u].size(); ++i) {
    if (!take[eid[u][i]]) continue;
    int to = getTo(u, eid[u][i]);
    if (to == p) continue;
    by[to] = eid[u][i];
    dfs(to, u);
  }
  tout[u] = cnt;
}
bool isA(int a, int b) { return (tin[a] <= tin[b] && tout[a] >= tout[b]); }
int getLca(int a, int b) {
  if (isA(a, b)) return a;
  if (isA(b, a)) return b;
  for (int i = M - 1; i >= 0; --i) {
    if (lca[a][i] == -1 || isA(lca[a][i], b)) continue;
    a = lca[a][i];
  }
  return lca[a][0];
}
int getMax(int b, int t) {
  int ret = 0;
  for (int i = M - 1; i >= 0; --i) {
    if (lca[b][i] == -1 || isA(lca[b][i], t)) continue;
    ret = max(ret, dp[b][i]);
    b = lca[b][i];
  }
  if (b != t) ret = max(ret, dp[b][0]);
  return ret;
}
int getMaxPath(int a, int b) {
  int l = getLca(a, b);
  int mx1 = getMax(a, l);
  int mx2 = getMax(b, l);
  return max(mx1, mx2);
}
vector<pair<int, int>> t[N * 4];
vector<int> pref[N * 4], suf[N * 4];
void merge(vector<pair<int, int>> &v, vector<pair<int, int>> &l,
           vector<pair<int, int>> &r) {
  int lp = 0;
  int rp = 0;
  int ln = l.size();
  int rn = r.size();
  while (lp < ln || rp < rn) {
    if (rp == rn || (lp < ln && l[lp].first < r[rp].first)) {
      v.push_back(l[lp]);
      ++lp;
    } else {
      v.push_back(r[rp]);
      ++rp;
    }
  }
}
void build(int v, int tl, int tr) {
  if (tl > tr) return;
  if (tl == tr) {
    t[v].push_back(edges[tl]);
    pref[v].push_back(edges[tl].second);
    suf[v].push_back(edges[tl].second);
  } else {
    int tm = (tl + tr) / 2;
    build(v * 2 + 1, tl, tm);
    build(v * 2 + 2, tm + 1, tr);
    merge(t[v], t[v * 2 + 1], t[v * 2 + 2]);
    int n = t[v].size();
    pref[v].resize(n);
    suf[v].resize(n);
    for (int i = 0; i < n; ++i) {
      pref[v][i] = t[v][i].second;
      if (i > 0) pref[v][i] = min(pref[v][i], pref[v][i - 1]);
      suf[v][n - 1 - i] = t[v][n - 1 - i].second;
      if (i > 0)
        suf[v][n - 1 - i] = min(suf[v][n - 1 - i], suf[v][n - 1 - i + 1]);
    }
  }
}
int upper(vector<pair<int, int>> &v, int val) {
  int l = -1;
  int r = v.size();
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (v[m].first > val)
      r = m;
    else
      l = m;
  }
  return r;
}
int getMin(int v, int tl, int tr, int l, int r, int p, int s) {
  if (tl > r || l > tr) return INF;
  if (l <= tl && tr <= r) {
    int ret = INF;
    int ppos = upper(t[v], p - 1);
    --ppos;
    if (ppos >= 0) ret = min(ret, pref[v][ppos]);
    int spos = upper(t[v], s - 1);
    if (spos < suf[v].size()) ret = min(ret, suf[v][spos]);
    return ret;
  }
  int tm = (tl + tr) / 2;
  return min(getMin(v * 2 + 1, tl, tm, l, r, p, s),
             getMin(v * 2 + 2, tm + 1, tr, l, r, p, s));
}
int calcEdge(int id) {
  int a = f[id];
  int b = s[id];
  if (lca[b][0] == a) swap(a, b);
  int p = tin[a];
  int s = tout[a] + 1;
  return getMin(0, 0, tsz - 1, le[a], ri[a], p, s);
}
void dfs1(int u, int p) {
  le[u] = edges.size();
  for (int i = 0; i < eid[u].size(); ++i) {
    if (!take[eid[u][i]]) continue;
    int to = getTo(u, eid[u][i]);
    if (to == p) continue;
    dfs1(to, u);
  }
  for (int i = 0; i < eid[u].size(); ++i) {
    if (take[eid[u][i]]) continue;
    int to = getTo(u, eid[u][i]);
    if (to == p) continue;
    edges.push_back(make_pair(tin[to], c[eid[u][i]]));
  }
  ri[u] = (int)edges.size() - 1;
}
void solve() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d %d %d", &f[i], &s[i], &c[i]);
    --f[i];
    --s[i];
    eid[f[i]].push_back(i);
    eid[s[i]].push_back(i);
    perm.push_back(i);
  }
  for (int i = 0; i < N; ++i) p[i] = i;
  sort(perm.begin(), perm.end(), cmp);
  for (int i = 0; i < m; ++i) {
    if (uni(f[perm[i]], s[perm[i]])) {
      take[perm[i]] = 1;
    }
  }
  dfs(0, -1);
  dfs1(0, -1);
  tsz = edges.size();
  build(0, 0, tsz - 1);
  for (int i = 0; i < m; ++i) {
    if (take[i])
      ans[i] = calcEdge(i);
    else
      ans[i] = getMaxPath(f[i], s[i]);
    if (ans[i] == INF) ans[i] = 0;
  }
  for (int i = 0; i < m; ++i) printf("%d ", ans[i] - 1);
}
int main() {
  solve();
  return 0;
}

```