#include <bits/stdc++.h>
using namespace std;
class UnionFind {
 private:
  vector<int> p, rank;

 public:
  UnionFind(int N) {
    rank.assign(N + 1, 0);
    p.assign(N + 1, 0);
    for (int i = 0; i <= N; i++) p[i] = i;
  }
  int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
  bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
  void unionSet(int i, int j) {
    if (!isSameSet(i, j)) {
      int x = findSet(i), y = findSet(j);
      if (rank[x] > rank[y])
        p[y] = x;
      else {
        p[x] = y;
        if (rank[x] == rank[y]) rank[y]++;
      }
    }
  }
};
vector<pair<pair<int, int>, int> > adj[200010], mst[200010];
int ans[200010], wt, mx_wt, curr_wt, curr_idx;
pair<pair<int, int>, pair<int, int> > edges[200010];
bool compr(pair<pair<int, int>, pair<int, int> > a,
           pair<pair<int, int>, pair<int, int> > b) {
  return (a.first.first < b.first.first);
}
int numm[200010], num_low[200010], tmm = 0, cnt[200010] = {0};
bool vis[200010] = {0};
void dfs(int v, int p) {
  vis[v] = true;
  num_low[v] = numm[v] = ++tmm;
  for (int i = 0; i < adj[v].size(); ++i) {
    int t = adj[v][i].first.first;
    if (t == p) continue;
    if (!vis[t]) dfs(t, v);
    if (num_low[t] <= numm[v]) ans[adj[v][i].second] = adj[v][i].first.second;
    num_low[v] = min(num_low[v], num_low[t]);
  }
}
int base[200010], pos[200010] = {0}, chainIdx[200010], head[200010],
                  chainNo = 1, num = 1, d[200010];
int max_st[200010 * 4] = {0}, min_st[200010 * 4], min_ch[200010 * 4],
                    par[200010][20];
void dfs_mst(int v, int p) {
  cnt[v] = 1;
  d[v] = d[p] + 1;
  par[v][0] = p;
  int pw = 2;
  for (int i = 1; i < 20; ++i) {
    if (pw <= d[v])
      par[v][i] = par[par[v][i - 1]][i - 1];
    else
      par[v][i] = 0;
    pw = (pw << 1);
  }
  for (int i = 0; i < mst[v].size(); ++i) {
    int t = mst[v][i].first.first;
    if (t == p) continue;
    dfs_mst(t, v);
    cnt[v] += cnt[t];
  }
}
int LCA(int u, int v) {
  if (d[v] > d[u]) swap(u, v);
  int diff = (d[u] - d[v]), pw = 1;
  for (int i = 0; pw <= diff; ++i) {
    if (diff & pw) u = par[u][i];
    pw = (pw << 1);
  }
  if (u == v) return u;
  for (int i = 20 - 1; i >= 0; --i) {
    if (par[u][i] != par[v][i]) {
      u = par[u][i];
      v = par[v][i];
    }
  }
  return par[u][0];
}
void hld(int id, int p) {
  int v = mst[p][id].first.first, w = mst[p][id].first.second;
  if (head[chainNo] == -1) head[chainNo] = v;
  if (v != 1) {
    pos[v] = num;
    base[num++] = w;
  }
  chainIdx[v] = chainNo;
  int spl_id = -1, max_no = -1;
  for (int i = 0; i < mst[v].size(); ++i) {
    int t = mst[v][i].first.first;
    if (t == p) continue;
    if (cnt[t] > max_no) {
      max_no = cnt[t];
      spl_id = i;
    }
  }
  if (max_no > 0) hld(spl_id, v);
  for (int i = 0; i < mst[v].size(); ++i) {
    if (mst[v][i].first.first == p || i == spl_id) continue;
    ++chainNo;
    hld(i, v);
  }
}
int N;
void construct(int L, int R, int curr) {
  if (L == R) {
    max_st[curr] = base[L];
    min_st[curr] = min_ch[curr] = 1000000007ll;
    return;
  }
  int m = ((L + R) >> 1);
  construct(L, m, (curr << 1));
  construct(m + 1, R, ((curr << 1) | 1));
  max_st[curr] = max(max_st[(curr << 1)], max_st[((curr << 1) | 1)]);
  min_st[curr] = min_ch[curr] = 1000000007ll;
}
int query_max(int i, int j, int L, int R, int curr) {
  if (i == L && j == R) return max_st[curr];
  int m = ((L + R) >> 1);
  if (j <= m) return query_max(i, j, L, m, (curr << 1));
  if (i > m) return query_max(i, j, m + 1, R, ((curr << 1) | 1));
  return max(query_max(i, m, L, m, (curr << 1)),
             query_max(m + 1, j, m + 1, R, ((curr << 1) | 1)));
}
int max_wt(int u, int v) {
  int ret = 0, lca = LCA(u, v);
  while (chainIdx[u] != chainIdx[lca]) {
    int hd = head[chainIdx[u]];
    ret = max(ret, query_max(pos[hd], pos[u], 1, N, 1));
    u = par[hd][0];
  }
  if (u != lca) ret = max(ret, query_max(pos[lca] + 1, pos[u], 1, N, 1));
  while (chainIdx[v] != chainIdx[lca]) {
    int hd = head[chainIdx[v]];
    ret = max(ret, query_max(pos[hd], pos[v], 1, N, 1));
    v = par[hd][0];
  }
  if (v != lca) ret = max(ret, query_max(pos[lca] + 1, pos[v], 1, N, 1));
  return ret;
}
void pass_it_on(int curr) {
  int wt = min_ch[curr];
  min_ch[(curr << 1)] = min(min_ch[(curr << 1)], wt);
  min_ch[((curr << 1) | 1)] = min(min_ch[((curr << 1) | 1)], wt);
  min_st[(curr << 1)] = min(min_st[(curr << 1)], wt);
  min_st[((curr << 1) | 1)] = min(min_st[((curr << 1) | 1)], wt);
  min_ch[curr] = 1000000007ll;
}
void update_min(int i, int j, int L, int R, int curr, int wt) {
  if (i == L && j == R) {
    min_ch[curr] = min(min_ch[curr], wt);
    min_st[curr] = min(min_st[curr], wt);
    return;
  }
  int m = ((L + R) >> 1);
  if (min_ch[curr] != 1000000007ll) pass_it_on(curr);
  if (j <= m)
    update_min(i, j, L, m, (curr << 1), wt);
  else if (i > m)
    update_min(i, j, m + 1, R, ((curr << 1) | 1), wt);
  else {
    update_min(i, m, L, m, (curr << 1), wt);
    update_min(m + 1, j, m + 1, R, ((curr << 1) | 1), wt);
  }
  min_st[curr] = min(min_st[curr], wt);
}
void update(int u, int v, int wt) {
  int lca = LCA(u, v);
  while (chainIdx[u] != chainIdx[lca]) {
    int hd = head[chainIdx[u]];
    update_min(pos[hd], pos[u], 1, N, 1, wt);
    u = par[hd][0];
  }
  if (u != lca) update_min(pos[lca] + 1, pos[u], 1, N, 1, wt);
  while (chainIdx[v] != chainIdx[lca]) {
    int hd = head[chainIdx[v]];
    update_min(pos[hd], pos[v], 1, N, 1, wt);
    v = par[hd][0];
  }
  if (v != lca) update_min(pos[lca] + 1, pos[v], 1, N, 1, wt);
}
int query_min(int v) {
  int p = pos[v], L = 1, R = N, curr = 1;
  while (L < R) {
    int m = ((L + R) >> 1);
    if (min_ch[curr] != 1000000007ll) pass_it_on(curr);
    if (p <= m) {
      R = m;
      curr = (curr << 1);
    } else {
      L = m + 1;
      curr = ((curr << 1) | 1);
    }
  }
  return min_st[curr];
}
bool present[200010] = {0};
int main() {
  for (int i = 0; i < 200010; ++i) head[i] = -1;
  int n, m;
  scanf("%d %d", &n, &m);
  N = n - 1;
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    adj[u].push_back(make_pair(make_pair(v, w), i));
    adj[v].push_back(make_pair(make_pair(u, w), i));
    edges[i] = make_pair(make_pair(w, i), make_pair(u, v));
    ans[i] = -1;
  }
  dfs(1, 1);
  mst[0].push_back(make_pair(make_pair(1, 0), 0));
  sort(edges + 1, edges + m + 1, compr);
  int sz = 1;
  UnionFind a(n);
  for (int i = 1; i <= m; ++i) {
    int u = edges[i].second.first, v = edges[i].second.second,
        w = edges[i].first.first, id = edges[i].first.second;
    if (!a.isSameSet(u, v)) {
      mst[u].push_back(make_pair(make_pair(v, w), id));
      mst[v].push_back(make_pair(make_pair(u, w), id));
      present[id] = true;
      a.unionSet(u, v);
      if (++sz == n) break;
    }
  }
  d[0] = -1;
  dfs_mst(1, 0);
  hld(0, 0);
  construct(1, N, 1);
  for (int i = 1; i <= m; ++i) {
    int u = edges[i].second.first, v = edges[i].second.second,
        w = edges[i].first.first, id = edges[i].first.second;
    if (!present[id]) update(u, v, w);
  }
  for (int i = 1; i <= m; ++i) {
    int u = edges[i].second.first, v = edges[i].second.second,
        w = edges[i].first.first, id = edges[i].first.second, vertex = u;
    if (d[u] < d[v]) vertex = v;
    if (ans[id] == -1)
      continue;
    else if (present[id])
      ans[id] = query_min(vertex) - 1;
    else
      ans[id] = max_wt(u, v) - 1;
  }
  for (int i = 1; i <= m; ++i) {
    printf("%d ", ans[i]);
  }
  printf("\n");
  return 0;
}

### Prompt

Construct a Cpp code solution to the problem outlined:

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;
class UnionFind {
 private:
  vector<int> p, rank;

 public:
  UnionFind(int N) {
    rank.assign(N + 1, 0);
    p.assign(N + 1, 0);
    for (int i = 0; i <= N; i++) p[i] = i;
  }
  int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
  bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
  void unionSet(int i, int j) {
    if (!isSameSet(i, j)) {
      int x = findSet(i), y = findSet(j);
      if (rank[x] > rank[y])
        p[y] = x;
      else {
        p[x] = y;
        if (rank[x] == rank[y]) rank[y]++;
      }
    }
  }
};
vector<pair<pair<int, int>, int> > adj[200010], mst[200010];
int ans[200010], wt, mx_wt, curr_wt, curr_idx;
pair<pair<int, int>, pair<int, int> > edges[200010];
bool compr(pair<pair<int, int>, pair<int, int> > a,
           pair<pair<int, int>, pair<int, int> > b) {
  return (a.first.first < b.first.first);
}
int numm[200010], num_low[200010], tmm = 0, cnt[200010] = {0};
bool vis[200010] = {0};
void dfs(int v, int p) {
  vis[v] = true;
  num_low[v] = numm[v] = ++tmm;
  for (int i = 0; i < adj[v].size(); ++i) {
    int t = adj[v][i].first.first;
    if (t == p) continue;
    if (!vis[t]) dfs(t, v);
    if (num_low[t] <= numm[v]) ans[adj[v][i].second] = adj[v][i].first.second;
    num_low[v] = min(num_low[v], num_low[t]);
  }
}
int base[200010], pos[200010] = {0}, chainIdx[200010], head[200010],
                  chainNo = 1, num = 1, d[200010];
int max_st[200010 * 4] = {0}, min_st[200010 * 4], min_ch[200010 * 4],
                    par[200010][20];
void dfs_mst(int v, int p) {
  cnt[v] = 1;
  d[v] = d[p] + 1;
  par[v][0] = p;
  int pw = 2;
  for (int i = 1; i < 20; ++i) {
    if (pw <= d[v])
      par[v][i] = par[par[v][i - 1]][i - 1];
    else
      par[v][i] = 0;
    pw = (pw << 1);
  }
  for (int i = 0; i < mst[v].size(); ++i) {
    int t = mst[v][i].first.first;
    if (t == p) continue;
    dfs_mst(t, v);
    cnt[v] += cnt[t];
  }
}
int LCA(int u, int v) {
  if (d[v] > d[u]) swap(u, v);
  int diff = (d[u] - d[v]), pw = 1;
  for (int i = 0; pw <= diff; ++i) {
    if (diff & pw) u = par[u][i];
    pw = (pw << 1);
  }
  if (u == v) return u;
  for (int i = 20 - 1; i >= 0; --i) {
    if (par[u][i] != par[v][i]) {
      u = par[u][i];
      v = par[v][i];
    }
  }
  return par[u][0];
}
void hld(int id, int p) {
  int v = mst[p][id].first.first, w = mst[p][id].first.second;
  if (head[chainNo] == -1) head[chainNo] = v;
  if (v != 1) {
    pos[v] = num;
    base[num++] = w;
  }
  chainIdx[v] = chainNo;
  int spl_id = -1, max_no = -1;
  for (int i = 0; i < mst[v].size(); ++i) {
    int t = mst[v][i].first.first;
    if (t == p) continue;
    if (cnt[t] > max_no) {
      max_no = cnt[t];
      spl_id = i;
    }
  }
  if (max_no > 0) hld(spl_id, v);
  for (int i = 0; i < mst[v].size(); ++i) {
    if (mst[v][i].first.first == p || i == spl_id) continue;
    ++chainNo;
    hld(i, v);
  }
}
int N;
void construct(int L, int R, int curr) {
  if (L == R) {
    max_st[curr] = base[L];
    min_st[curr] = min_ch[curr] = 1000000007ll;
    return;
  }
  int m = ((L + R) >> 1);
  construct(L, m, (curr << 1));
  construct(m + 1, R, ((curr << 1) | 1));
  max_st[curr] = max(max_st[(curr << 1)], max_st[((curr << 1) | 1)]);
  min_st[curr] = min_ch[curr] = 1000000007ll;
}
int query_max(int i, int j, int L, int R, int curr) {
  if (i == L && j == R) return max_st[curr];
  int m = ((L + R) >> 1);
  if (j <= m) return query_max(i, j, L, m, (curr << 1));
  if (i > m) return query_max(i, j, m + 1, R, ((curr << 1) | 1));
  return max(query_max(i, m, L, m, (curr << 1)),
             query_max(m + 1, j, m + 1, R, ((curr << 1) | 1)));
}
int max_wt(int u, int v) {
  int ret = 0, lca = LCA(u, v);
  while (chainIdx[u] != chainIdx[lca]) {
    int hd = head[chainIdx[u]];
    ret = max(ret, query_max(pos[hd], pos[u], 1, N, 1));
    u = par[hd][0];
  }
  if (u != lca) ret = max(ret, query_max(pos[lca] + 1, pos[u], 1, N, 1));
  while (chainIdx[v] != chainIdx[lca]) {
    int hd = head[chainIdx[v]];
    ret = max(ret, query_max(pos[hd], pos[v], 1, N, 1));
    v = par[hd][0];
  }
  if (v != lca) ret = max(ret, query_max(pos[lca] + 1, pos[v], 1, N, 1));
  return ret;
}
void pass_it_on(int curr) {
  int wt = min_ch[curr];
  min_ch[(curr << 1)] = min(min_ch[(curr << 1)], wt);
  min_ch[((curr << 1) | 1)] = min(min_ch[((curr << 1) | 1)], wt);
  min_st[(curr << 1)] = min(min_st[(curr << 1)], wt);
  min_st[((curr << 1) | 1)] = min(min_st[((curr << 1) | 1)], wt);
  min_ch[curr] = 1000000007ll;
}
void update_min(int i, int j, int L, int R, int curr, int wt) {
  if (i == L && j == R) {
    min_ch[curr] = min(min_ch[curr], wt);
    min_st[curr] = min(min_st[curr], wt);
    return;
  }
  int m = ((L + R) >> 1);
  if (min_ch[curr] != 1000000007ll) pass_it_on(curr);
  if (j <= m)
    update_min(i, j, L, m, (curr << 1), wt);
  else if (i > m)
    update_min(i, j, m + 1, R, ((curr << 1) | 1), wt);
  else {
    update_min(i, m, L, m, (curr << 1), wt);
    update_min(m + 1, j, m + 1, R, ((curr << 1) | 1), wt);
  }
  min_st[curr] = min(min_st[curr], wt);
}
void update(int u, int v, int wt) {
  int lca = LCA(u, v);
  while (chainIdx[u] != chainIdx[lca]) {
    int hd = head[chainIdx[u]];
    update_min(pos[hd], pos[u], 1, N, 1, wt);
    u = par[hd][0];
  }
  if (u != lca) update_min(pos[lca] + 1, pos[u], 1, N, 1, wt);
  while (chainIdx[v] != chainIdx[lca]) {
    int hd = head[chainIdx[v]];
    update_min(pos[hd], pos[v], 1, N, 1, wt);
    v = par[hd][0];
  }
  if (v != lca) update_min(pos[lca] + 1, pos[v], 1, N, 1, wt);
}
int query_min(int v) {
  int p = pos[v], L = 1, R = N, curr = 1;
  while (L < R) {
    int m = ((L + R) >> 1);
    if (min_ch[curr] != 1000000007ll) pass_it_on(curr);
    if (p <= m) {
      R = m;
      curr = (curr << 1);
    } else {
      L = m + 1;
      curr = ((curr << 1) | 1);
    }
  }
  return min_st[curr];
}
bool present[200010] = {0};
int main() {
  for (int i = 0; i < 200010; ++i) head[i] = -1;
  int n, m;
  scanf("%d %d", &n, &m);
  N = n - 1;
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    adj[u].push_back(make_pair(make_pair(v, w), i));
    adj[v].push_back(make_pair(make_pair(u, w), i));
    edges[i] = make_pair(make_pair(w, i), make_pair(u, v));
    ans[i] = -1;
  }
  dfs(1, 1);
  mst[0].push_back(make_pair(make_pair(1, 0), 0));
  sort(edges + 1, edges + m + 1, compr);
  int sz = 1;
  UnionFind a(n);
  for (int i = 1; i <= m; ++i) {
    int u = edges[i].second.first, v = edges[i].second.second,
        w = edges[i].first.first, id = edges[i].first.second;
    if (!a.isSameSet(u, v)) {
      mst[u].push_back(make_pair(make_pair(v, w), id));
      mst[v].push_back(make_pair(make_pair(u, w), id));
      present[id] = true;
      a.unionSet(u, v);
      if (++sz == n) break;
    }
  }
  d[0] = -1;
  dfs_mst(1, 0);
  hld(0, 0);
  construct(1, N, 1);
  for (int i = 1; i <= m; ++i) {
    int u = edges[i].second.first, v = edges[i].second.second,
        w = edges[i].first.first, id = edges[i].first.second;
    if (!present[id]) update(u, v, w);
  }
  for (int i = 1; i <= m; ++i) {
    int u = edges[i].second.first, v = edges[i].second.second,
        w = edges[i].first.first, id = edges[i].first.second, vertex = u;
    if (d[u] < d[v]) vertex = v;
    if (ans[id] == -1)
      continue;
    else if (present[id])
      ans[id] = query_min(vertex) - 1;
    else
      ans[id] = max_wt(u, v) - 1;
  }
  for (int i = 1; i <= m; ++i) {
    printf("%d ", ans[i]);
  }
  printf("\n");
  return 0;
}

```