#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
void amax(X& x, const Y& y) {
  if (x < y) x = y;
}
template <class X, class Y>
void amin(X& x, const Y& y) {
  if (x > y) x = y;
}
const int INF = 1e9 + 10;
const long long INFL = 1e18 + 10;
const int MAX = 2e5 + 10;
struct edge {
  int u, v, c, id;
  bool in;
};
int n, m, limit, hn, hc;
edge ed[MAX];
int lab[MAX];
vector<int> es[MAX];
int h[MAX], pr[MAX], child[MAX], lca[20][MAX], mlca[20][MAX];
int res[MAX];
int hchain[MAX], hhead[MAX], hsize[MAX], hid[MAX], hpos[MAX];
int second[MAX * 4];
int getroot(int x) {
  if (lab[x] < 0) return x;
  lab[x] = getroot(lab[x]);
  return lab[x];
}
void dsunion(int r, int s) {
  if (lab[r] < lab[s])
    lab[s] = r;
  else {
    if (lab[r] == lab[s]) lab[s]--;
    lab[r] = s;
  }
}
int getv(int e, int u) {
  if (ed[e].u == u) return ed[e].v;
  return ed[e].u;
}
void visit(int u) {
  h[u] = h[pr[u]] + 1;
  amax(limit, h[u]);
  child[u] = 1;
  for (auto e : es[u]) {
    int v = getv(e, u);
    if (v == pr[u]) continue;
    pr[v] = u;
    lca[0][v] = u;
    mlca[0][v] = ed[e].c;
    visit(v);
    child[u] += child[v];
  }
}
int getlca(int u, int v) {
  if (h[u] > h[v]) swap(u, v);
  for (int i = limit; i >= 0; i--)
    if (h[lca[i][v]] >= h[u]) v = lca[i][v];
  if (u == v) return u;
  for (int i = limit; i >= 0; i--)
    if (lca[i][u] != lca[i][v]) {
      u = lca[i][u];
      v = lca[i][v];
    }
  return pr[u];
}
int getmlca(int u, int v) {
  int res = 0;
  if (h[u] > h[v]) swap(u, v);
  for (int i = limit; i >= 0; i--)
    if (h[lca[i][v]] >= h[u]) {
      amax(res, mlca[i][v]);
      v = lca[i][v];
    }
  if (u == v) return res;
  for (int i = limit; i >= 0; i--)
    if (lca[i][u] != lca[i][v]) {
      amax(res, mlca[i][u]);
      amax(res, mlca[i][v]);
      u = lca[i][u];
      v = lca[i][v];
    }
  amax(res, mlca[0][u]);
  amax(res, mlca[0][v]);
  return res;
}
void hld(int u) {
  hid[u] = hn;
  hsize[hn]++;
  hchain[++hc] = u;
  hpos[u] = hc;
  bool leaf = true;
  for (auto e : es[u]) {
    int v = getv(e, u);
    if (v == pr[u]) continue;
    hld(v);
    leaf = false;
  }
  if (leaf) hhead[++hn] = hc + 1;
}
void update(int id, int l, int r, int u, int v, int pval) {
  if (v < l || u > r) return;
  if (u <= l && r <= v) {
    amin(second[id], pval);
    return;
  }
  int mid = (l + r) / 2;
  update(id * 2, l, mid, u, v, pval);
  update(id * 2 + 1, mid + 1, r, u, v, pval);
}
int query(int id, int l, int r, int p) {
  if (p < l || p > r) return INF;
  if (l == r) return second[id];
  int mid = (l + r) / 2;
  int res = second[id];
  if (p <= mid)
    amin(res, query(id * 2, l, mid, p));
  else
    amin(res, query(id * 2 + 1, mid + 1, r, p));
  return res;
}
void hupdate(int u, int la, int c) {
  while (true) {
    if (u == la) return;
    if (hid[u] == hid[la]) {
      update(1, 1, n, hpos[la] + 1, hpos[u], c);
      break;
    }
    update(1, 1, n, hhead[hid[u]], hpos[u], c);
    u = pr[hchain[hhead[hid[u]]]];
  }
}
void build_se(int id, int l, int r) {
  if (l == r) {
    second[id] = INF;
    return;
  }
  int mid = (l + r) / 2;
  build_se(id * 2, l, mid);
  build_se(id * 2 + 1, mid + 1, r);
  second[id] = INF;
}
void process() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> ed[i].u >> ed[i].v >> ed[i].c;
    ed[i].id = i;
  }
  sort(ed + 1, ed + m + 1,
       [](const edge& lhs, const edge& rhs) { return lhs.c < rhs.c; });
  memset(lab, 255, sizeof(lab));
  for (int i = 1; i <= m; i++) {
    int r = getroot(ed[i].u);
    int s = getroot(ed[i].v);
    if (r != s) {
      dsunion(r, s);
      es[ed[i].u].push_back(i);
      es[ed[i].v].push_back(i);
      ed[i].in = true;
    }
  }
  visit(1);
  limit = (int)log2l(limit);
  for (int i = 1; i <= limit; i++)
    for (int j = 1; j <= n; j++) {
      lca[i][j] = lca[i - 1][lca[i - 1][j]];
      mlca[i][j] = max(mlca[i - 1][j], mlca[i - 1][lca[i - 1][j]]);
    }
  for (int i = 1; i <= m; i++)
    if (!ed[i].in) {
      res[ed[i].id] = getmlca(ed[i].u, ed[i].v);
    }
  hn = 1;
  hhead[hn] = 1;
  for (int i = 1; i <= n; i++) {
    sort(es[i].begin(), es[i].end(), [i](int lhs, int rhs) {
      return child[getv(lhs, i)] > child[getv(rhs, i)];
    });
  }
  hld(1);
  build_se(1, 1, n);
  for (int i = 1; i <= m; i++)
    if (!ed[i].in) {
      int u = ed[i].u;
      int v = ed[i].v;
      int c = ed[i].c;
      int x = getlca(u, v);
      hupdate(u, x, c);
      hupdate(v, x, c);
    }
  for (int i = 1; i <= m; i++)
    if (ed[i].in) {
      int u = ed[i].u;
      int v = ed[i].v;
      if (pr[v] != u) swap(u, v);
      res[ed[i].id] = query(1, 1, n, hpos[v]);
    }
  for (int i = 1; i <= m; i++) {
    if (res[i] == INF)
      cout << -1 << " ";
    else
      cout << res[i] - 1 << " ";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  process();
}

### 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>
using namespace std;
template <class X, class Y>
void amax(X& x, const Y& y) {
  if (x < y) x = y;
}
template <class X, class Y>
void amin(X& x, const Y& y) {
  if (x > y) x = y;
}
const int INF = 1e9 + 10;
const long long INFL = 1e18 + 10;
const int MAX = 2e5 + 10;
struct edge {
  int u, v, c, id;
  bool in;
};
int n, m, limit, hn, hc;
edge ed[MAX];
int lab[MAX];
vector<int> es[MAX];
int h[MAX], pr[MAX], child[MAX], lca[20][MAX], mlca[20][MAX];
int res[MAX];
int hchain[MAX], hhead[MAX], hsize[MAX], hid[MAX], hpos[MAX];
int second[MAX * 4];
int getroot(int x) {
  if (lab[x] < 0) return x;
  lab[x] = getroot(lab[x]);
  return lab[x];
}
void dsunion(int r, int s) {
  if (lab[r] < lab[s])
    lab[s] = r;
  else {
    if (lab[r] == lab[s]) lab[s]--;
    lab[r] = s;
  }
}
int getv(int e, int u) {
  if (ed[e].u == u) return ed[e].v;
  return ed[e].u;
}
void visit(int u) {
  h[u] = h[pr[u]] + 1;
  amax(limit, h[u]);
  child[u] = 1;
  for (auto e : es[u]) {
    int v = getv(e, u);
    if (v == pr[u]) continue;
    pr[v] = u;
    lca[0][v] = u;
    mlca[0][v] = ed[e].c;
    visit(v);
    child[u] += child[v];
  }
}
int getlca(int u, int v) {
  if (h[u] > h[v]) swap(u, v);
  for (int i = limit; i >= 0; i--)
    if (h[lca[i][v]] >= h[u]) v = lca[i][v];
  if (u == v) return u;
  for (int i = limit; i >= 0; i--)
    if (lca[i][u] != lca[i][v]) {
      u = lca[i][u];
      v = lca[i][v];
    }
  return pr[u];
}
int getmlca(int u, int v) {
  int res = 0;
  if (h[u] > h[v]) swap(u, v);
  for (int i = limit; i >= 0; i--)
    if (h[lca[i][v]] >= h[u]) {
      amax(res, mlca[i][v]);
      v = lca[i][v];
    }
  if (u == v) return res;
  for (int i = limit; i >= 0; i--)
    if (lca[i][u] != lca[i][v]) {
      amax(res, mlca[i][u]);
      amax(res, mlca[i][v]);
      u = lca[i][u];
      v = lca[i][v];
    }
  amax(res, mlca[0][u]);
  amax(res, mlca[0][v]);
  return res;
}
void hld(int u) {
  hid[u] = hn;
  hsize[hn]++;
  hchain[++hc] = u;
  hpos[u] = hc;
  bool leaf = true;
  for (auto e : es[u]) {
    int v = getv(e, u);
    if (v == pr[u]) continue;
    hld(v);
    leaf = false;
  }
  if (leaf) hhead[++hn] = hc + 1;
}
void update(int id, int l, int r, int u, int v, int pval) {
  if (v < l || u > r) return;
  if (u <= l && r <= v) {
    amin(second[id], pval);
    return;
  }
  int mid = (l + r) / 2;
  update(id * 2, l, mid, u, v, pval);
  update(id * 2 + 1, mid + 1, r, u, v, pval);
}
int query(int id, int l, int r, int p) {
  if (p < l || p > r) return INF;
  if (l == r) return second[id];
  int mid = (l + r) / 2;
  int res = second[id];
  if (p <= mid)
    amin(res, query(id * 2, l, mid, p));
  else
    amin(res, query(id * 2 + 1, mid + 1, r, p));
  return res;
}
void hupdate(int u, int la, int c) {
  while (true) {
    if (u == la) return;
    if (hid[u] == hid[la]) {
      update(1, 1, n, hpos[la] + 1, hpos[u], c);
      break;
    }
    update(1, 1, n, hhead[hid[u]], hpos[u], c);
    u = pr[hchain[hhead[hid[u]]]];
  }
}
void build_se(int id, int l, int r) {
  if (l == r) {
    second[id] = INF;
    return;
  }
  int mid = (l + r) / 2;
  build_se(id * 2, l, mid);
  build_se(id * 2 + 1, mid + 1, r);
  second[id] = INF;
}
void process() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> ed[i].u >> ed[i].v >> ed[i].c;
    ed[i].id = i;
  }
  sort(ed + 1, ed + m + 1,
       [](const edge& lhs, const edge& rhs) { return lhs.c < rhs.c; });
  memset(lab, 255, sizeof(lab));
  for (int i = 1; i <= m; i++) {
    int r = getroot(ed[i].u);
    int s = getroot(ed[i].v);
    if (r != s) {
      dsunion(r, s);
      es[ed[i].u].push_back(i);
      es[ed[i].v].push_back(i);
      ed[i].in = true;
    }
  }
  visit(1);
  limit = (int)log2l(limit);
  for (int i = 1; i <= limit; i++)
    for (int j = 1; j <= n; j++) {
      lca[i][j] = lca[i - 1][lca[i - 1][j]];
      mlca[i][j] = max(mlca[i - 1][j], mlca[i - 1][lca[i - 1][j]]);
    }
  for (int i = 1; i <= m; i++)
    if (!ed[i].in) {
      res[ed[i].id] = getmlca(ed[i].u, ed[i].v);
    }
  hn = 1;
  hhead[hn] = 1;
  for (int i = 1; i <= n; i++) {
    sort(es[i].begin(), es[i].end(), [i](int lhs, int rhs) {
      return child[getv(lhs, i)] > child[getv(rhs, i)];
    });
  }
  hld(1);
  build_se(1, 1, n);
  for (int i = 1; i <= m; i++)
    if (!ed[i].in) {
      int u = ed[i].u;
      int v = ed[i].v;
      int c = ed[i].c;
      int x = getlca(u, v);
      hupdate(u, x, c);
      hupdate(v, x, c);
    }
  for (int i = 1; i <= m; i++)
    if (ed[i].in) {
      int u = ed[i].u;
      int v = ed[i].v;
      if (pr[v] != u) swap(u, v);
      res[ed[i].id] = query(1, 1, n, hpos[v]);
    }
  for (int i = 1; i <= m; i++) {
    if (res[i] == INF)
      cout << -1 << " ";
    else
      cout << res[i] - 1 << " ";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  process();
}

```