#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int, pair<int, int> > > pq;
unordered_set<int> us;
queue<int> q;
vector<pair<int, int> > graf[200005], legit[200005];
int tata[200005], sz[200005], n, m, nivel[1000005], euclid[1000005], cnt,
    fr[200005], rmq[1000005][22], loga[1000005], put[1000005], nex[200005][22],
    str[200005][22], tata_next[200005];
long long costuri[200005];
long long solutie[1000005];
pair<int, pair<int, int> > to_upd[1000005];
long long cost;
pair<int, pair<int, int> > v[2000005];
void unite(int a, int b, int c) {
  int cp = a;
  while (cp != tata[cp]) {
    cp = tata[cp];
  }
  int cp1 = a;
  while (cp1 != cp) {
    int aux = tata[cp1];
    tata[cp1] = cp;
    cp1 = aux;
  }
  cp = b;
  cp1 = b;
  while (cp != tata[cp]) {
    cp = tata[cp];
  }
  while (cp1 != cp) {
    int aux = tata[cp1];
    tata[cp1] = cp;
    cp1 = aux;
  }
  if (tata[a] == tata[b]) {
    return;
  } else if (sz[tata[a]] > sz[tata[b]]) {
    tata[tata[b]] = tata[a];
    sz[tata[a]] += sz[tata[b]];
  } else {
    tata[tata[a]] = tata[b];
    sz[tata[b]] += sz[tata[a]];
  }
  cost += c;
  graf[a].push_back(make_pair(b, c));
  graf[b].push_back(make_pair(a, c));
}
void dfs(int nod, int niv) {
  nivel[++cnt] = niv;
  euclid[cnt] = nod;
  if (fr[nod] == 0) {
    fr[nod] = cnt;
  }
  for (int i = 0; i < legit[nod].size(); i++) {
    dfs(legit[nod][i].first, niv + 1);
    nivel[++cnt] = niv;
    euclid[cnt] = nod;
  }
}
int find_lca(int a, int b) {
  int st = fr[a], dr = fr[b];
  if (st > dr) swap(st, dr);
  int dist = dr - st + 1;
  if (nivel[rmq[st][loga[dist]]] <
      nivel[rmq[dr - put[loga[dist]] + 1][loga[dist]]]) {
    return euclid[rmq[st][loga[dist]]];
  }
  return euclid[rmq[dr - put[loga[dist]] + 1][loga[dist]]];
}
long long mi(long long a, long long b) {
  if (a > b) return b;
  return a;
}
long long ma(long long a, long long b) {
  if (a > b) return a;
  return b;
}
long long find_max(int down, int up) {
  if (nivel[fr[down]] < nivel[fr[up]]) swap(up, down);
  int dist = nivel[fr[down]] - nivel[fr[up]], maxim = 0;
  while (dist != 0) {
    int e = loga[dist];
    maxim = ma(str[down][e], maxim);
    down = nex[down][e];
    dist -= put[e];
  }
  return maxim;
}
bool exista(int ind) {
  int a = v[ind].first, b = v[ind].second.first;
  for (int i = 0; i < legit[a].size(); i++)
    if (legit[a][i].first == b) return true;
  swap(a, b);
  for (int i = 0; i < legit[a].size(); i++)
    if (legit[a][i].first == b) return true;
  return false;
}
bool mysort(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) {
  return a.first < b.first;
}
void update(int down, int up, int val) {
  int cop = down;
  while (cop != up) {
    int aux = tata_next[cop];
    if (aux == tata[cop] and costuri[cop] == 10000000000) {
      costuri[cop] = val;
    }
    cop = aux;
  }
  cop = down;
  while (cop != up) {
    int aux = tata_next[cop];
    tata_next[cop] = up;
    cop = aux;
  }
}
int find_lca_inef(int up, int down) {
  while (up != down) {
    if (nivel[fr[up]] > nivel[fr[down]]) {
      up = tata_next[up];
    } else
      down = tata_next[down];
  }
  return up;
}
int main() {
  int a, b, c, contt = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) costuri[i] = 10000000000;
  for (int i = 1; i <= m; i++) solutie[i] = -1;
  for (int i = 1; i <= m; i++) {
    cin >> v[i].first >> v[i].second.first >> v[i].second.second;
    a = v[i].first;
    b = v[i].second.first;
    c = v[i].second.second;
    pq.push(make_pair(-c, make_pair(a, b)));
  }
  for (int i = 1; i <= n; i++) tata[i] = i;
  for (int i = 1; i <= n; i++) sz[i] = 1;
  while (pq.empty() == false) {
    pair<int, pair<int, int> > x = pq.top();
    pq.pop();
    unite(x.second.first, x.second.second, -x.first);
  }
  q.push(1);
  us.insert(1);
  while (q.empty() == false) {
    int nod = q.front();
    q.pop();
    for (int i = 0; i < graf[nod].size(); i++) {
      if (us.count(graf[nod][i].first) == 0) {
        legit[nod].push_back(graf[nod][i]);
        us.insert(graf[nod][i].first);
        q.push(graf[nod][i].first);
      }
    }
    graf[nod].clear();
  }
  dfs(1, 1);
  for (int i = 1; i <= cnt; i++) {
    rmq[i][0] = i;
  }
  put[0] = 1;
  for (int i = 1; i <= 25; i++) {
    put[i] = put[i - 1] * 2;
    if (put[i] <= cnt) loga[put[i]] = 1;
  }
  for (int i = 1; i <= cnt; i++) {
    loga[i] += loga[i - 1];
  }
  for (int j = 1; j <= 20; j++) {
    for (int i = 1; i <= cnt; i++) {
      if (i + put[j] <= cnt)
        if (nivel[rmq[i][j - 1]] < nivel[rmq[i + put[j - 1]][j - 1]]) {
          rmq[i][j] = rmq[i][j - 1];
        } else
          rmq[i][j] = rmq[i + put[j - 1]][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < legit[i].size(); j++) {
      nex[legit[i][j].first][0] = i;
      str[legit[i][j].first][0] = legit[i][j].second;
    }
  }
  nex[1][0] = 1;
  for (int j = 1; j <= 20; j++) {
    for (int i = 1; i <= n; i++) {
      nex[i][j] = nex[nex[i][j - 1]][j - 1];
      str[i][j] = ma(str[i][j - 1], str[nex[i][j - 1]][j - 1]);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < legit[i].size(); j++) {
      tata_next[legit[i][j].first] = i;
      tata[legit[i][j].first] = i;
    }
  }
  tata[1] = 1;
  tata_next[1] = 1;
  for (int i = 1; i <= m; i++) {
    int a = v[i].first, b = v[i].second.first;
    int lca = find_lca(a, b);
    long long maxim = ma(find_max(a, lca), find_max(b, lca));
    if (exista(i) == false) {
      solutie[i] = maxim;
      to_upd[++contt] = make_pair(v[i].second.second, make_pair(a, b));
    }
  }
  sort(to_upd + 1, to_upd + contt + 1, mysort);
  for (int i = 1; i <= contt; i++) {
    int a = to_upd[i].second.second;
    int b = to_upd[i].second.first;
    int lca = find_lca_inef(a, b);
    update(a, lca, to_upd[i].first);
    update(b, lca, to_upd[i].first);
  }
  for (int i = 1; i <= m; i++) {
    if (solutie[i] == -1) {
      int a = v[i].first;
      int b = v[i].second.first;
      if (nivel[fr[a]] > nivel[fr[b]]) {
        swap(a, b);
      }
      solutie[i] = costuri[b];
    }
  }
  for (int i = 1; i <= m; i++) {
    if (solutie[i] == 10000000000)
      cout << -1 << " ";
    else
      cout << solutie[i] - 1 << " ";
  }
  return 0;
}

### 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;
priority_queue<pair<int, pair<int, int> > > pq;
unordered_set<int> us;
queue<int> q;
vector<pair<int, int> > graf[200005], legit[200005];
int tata[200005], sz[200005], n, m, nivel[1000005], euclid[1000005], cnt,
    fr[200005], rmq[1000005][22], loga[1000005], put[1000005], nex[200005][22],
    str[200005][22], tata_next[200005];
long long costuri[200005];
long long solutie[1000005];
pair<int, pair<int, int> > to_upd[1000005];
long long cost;
pair<int, pair<int, int> > v[2000005];
void unite(int a, int b, int c) {
  int cp = a;
  while (cp != tata[cp]) {
    cp = tata[cp];
  }
  int cp1 = a;
  while (cp1 != cp) {
    int aux = tata[cp1];
    tata[cp1] = cp;
    cp1 = aux;
  }
  cp = b;
  cp1 = b;
  while (cp != tata[cp]) {
    cp = tata[cp];
  }
  while (cp1 != cp) {
    int aux = tata[cp1];
    tata[cp1] = cp;
    cp1 = aux;
  }
  if (tata[a] == tata[b]) {
    return;
  } else if (sz[tata[a]] > sz[tata[b]]) {
    tata[tata[b]] = tata[a];
    sz[tata[a]] += sz[tata[b]];
  } else {
    tata[tata[a]] = tata[b];
    sz[tata[b]] += sz[tata[a]];
  }
  cost += c;
  graf[a].push_back(make_pair(b, c));
  graf[b].push_back(make_pair(a, c));
}
void dfs(int nod, int niv) {
  nivel[++cnt] = niv;
  euclid[cnt] = nod;
  if (fr[nod] == 0) {
    fr[nod] = cnt;
  }
  for (int i = 0; i < legit[nod].size(); i++) {
    dfs(legit[nod][i].first, niv + 1);
    nivel[++cnt] = niv;
    euclid[cnt] = nod;
  }
}
int find_lca(int a, int b) {
  int st = fr[a], dr = fr[b];
  if (st > dr) swap(st, dr);
  int dist = dr - st + 1;
  if (nivel[rmq[st][loga[dist]]] <
      nivel[rmq[dr - put[loga[dist]] + 1][loga[dist]]]) {
    return euclid[rmq[st][loga[dist]]];
  }
  return euclid[rmq[dr - put[loga[dist]] + 1][loga[dist]]];
}
long long mi(long long a, long long b) {
  if (a > b) return b;
  return a;
}
long long ma(long long a, long long b) {
  if (a > b) return a;
  return b;
}
long long find_max(int down, int up) {
  if (nivel[fr[down]] < nivel[fr[up]]) swap(up, down);
  int dist = nivel[fr[down]] - nivel[fr[up]], maxim = 0;
  while (dist != 0) {
    int e = loga[dist];
    maxim = ma(str[down][e], maxim);
    down = nex[down][e];
    dist -= put[e];
  }
  return maxim;
}
bool exista(int ind) {
  int a = v[ind].first, b = v[ind].second.first;
  for (int i = 0; i < legit[a].size(); i++)
    if (legit[a][i].first == b) return true;
  swap(a, b);
  for (int i = 0; i < legit[a].size(); i++)
    if (legit[a][i].first == b) return true;
  return false;
}
bool mysort(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) {
  return a.first < b.first;
}
void update(int down, int up, int val) {
  int cop = down;
  while (cop != up) {
    int aux = tata_next[cop];
    if (aux == tata[cop] and costuri[cop] == 10000000000) {
      costuri[cop] = val;
    }
    cop = aux;
  }
  cop = down;
  while (cop != up) {
    int aux = tata_next[cop];
    tata_next[cop] = up;
    cop = aux;
  }
}
int find_lca_inef(int up, int down) {
  while (up != down) {
    if (nivel[fr[up]] > nivel[fr[down]]) {
      up = tata_next[up];
    } else
      down = tata_next[down];
  }
  return up;
}
int main() {
  int a, b, c, contt = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) costuri[i] = 10000000000;
  for (int i = 1; i <= m; i++) solutie[i] = -1;
  for (int i = 1; i <= m; i++) {
    cin >> v[i].first >> v[i].second.first >> v[i].second.second;
    a = v[i].first;
    b = v[i].second.first;
    c = v[i].second.second;
    pq.push(make_pair(-c, make_pair(a, b)));
  }
  for (int i = 1; i <= n; i++) tata[i] = i;
  for (int i = 1; i <= n; i++) sz[i] = 1;
  while (pq.empty() == false) {
    pair<int, pair<int, int> > x = pq.top();
    pq.pop();
    unite(x.second.first, x.second.second, -x.first);
  }
  q.push(1);
  us.insert(1);
  while (q.empty() == false) {
    int nod = q.front();
    q.pop();
    for (int i = 0; i < graf[nod].size(); i++) {
      if (us.count(graf[nod][i].first) == 0) {
        legit[nod].push_back(graf[nod][i]);
        us.insert(graf[nod][i].first);
        q.push(graf[nod][i].first);
      }
    }
    graf[nod].clear();
  }
  dfs(1, 1);
  for (int i = 1; i <= cnt; i++) {
    rmq[i][0] = i;
  }
  put[0] = 1;
  for (int i = 1; i <= 25; i++) {
    put[i] = put[i - 1] * 2;
    if (put[i] <= cnt) loga[put[i]] = 1;
  }
  for (int i = 1; i <= cnt; i++) {
    loga[i] += loga[i - 1];
  }
  for (int j = 1; j <= 20; j++) {
    for (int i = 1; i <= cnt; i++) {
      if (i + put[j] <= cnt)
        if (nivel[rmq[i][j - 1]] < nivel[rmq[i + put[j - 1]][j - 1]]) {
          rmq[i][j] = rmq[i][j - 1];
        } else
          rmq[i][j] = rmq[i + put[j - 1]][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < legit[i].size(); j++) {
      nex[legit[i][j].first][0] = i;
      str[legit[i][j].first][0] = legit[i][j].second;
    }
  }
  nex[1][0] = 1;
  for (int j = 1; j <= 20; j++) {
    for (int i = 1; i <= n; i++) {
      nex[i][j] = nex[nex[i][j - 1]][j - 1];
      str[i][j] = ma(str[i][j - 1], str[nex[i][j - 1]][j - 1]);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < legit[i].size(); j++) {
      tata_next[legit[i][j].first] = i;
      tata[legit[i][j].first] = i;
    }
  }
  tata[1] = 1;
  tata_next[1] = 1;
  for (int i = 1; i <= m; i++) {
    int a = v[i].first, b = v[i].second.first;
    int lca = find_lca(a, b);
    long long maxim = ma(find_max(a, lca), find_max(b, lca));
    if (exista(i) == false) {
      solutie[i] = maxim;
      to_upd[++contt] = make_pair(v[i].second.second, make_pair(a, b));
    }
  }
  sort(to_upd + 1, to_upd + contt + 1, mysort);
  for (int i = 1; i <= contt; i++) {
    int a = to_upd[i].second.second;
    int b = to_upd[i].second.first;
    int lca = find_lca_inef(a, b);
    update(a, lca, to_upd[i].first);
    update(b, lca, to_upd[i].first);
  }
  for (int i = 1; i <= m; i++) {
    if (solutie[i] == -1) {
      int a = v[i].first;
      int b = v[i].second.first;
      if (nivel[fr[a]] > nivel[fr[b]]) {
        swap(a, b);
      }
      solutie[i] = costuri[b];
    }
  }
  for (int i = 1; i <= m; i++) {
    if (solutie[i] == 10000000000)
      cout << -1 << " ";
    else
      cout << solutie[i] - 1 << " ";
  }
  return 0;
}

```