#include <bits/stdc++.h>
using namespace std;
int n, m, q;
struct ss {
  int u, v, w, bh;
  bool ju = 0;
} ee[200010];
bool cmp(ss a, ss b) { return a.w < b.w; }
int ff[200010], d[200010];
int find(int a) {
  if (ff[a] == a) {
    return a;
  }
  return ff[a] = find(ff[a]);
}
int head[200010], cnt = 0;
struct sd {
  int to, next, dis;
} e[400010];
inline void add(int a, int b, int c) {
  cnt++;
  e[cnt].to = b;
  e[cnt].dis = c;
  e[cnt].next = head[a];
  head[a] = cnt;
}
int dep[200010], f[200010][22], ma[200010][22];
void dfs(int u, int fa) {
  f[u][0] = fa;
  dep[u] = dep[fa] + 1;
  for (int i = 1; i <= 20; i++) {
    f[u][i] = f[f[u][i - 1]][i - 1];
    ma[u][i] = max(ma[u][i - 1], ma[f[u][i - 1]][i - 1]);
  }
  for (int i = head[u]; i != 0; i = e[i].next) {
    if (e[i].to == fa) {
      continue;
    }
    ma[e[i].to][0] = e[i].dis;
    dfs(e[i].to, u);
  }
  return;
}
int llca(int a, int b) {
  int ans = 0;
  if (dep[a] < dep[b]) {
    swap(a, b);
  }
  for (int i = 20; i >= 0; i--) {
    if (dep[a] - (1 << i) >= dep[b]) {
      ans = max(ans, ma[a][i]);
      a = f[a][i];
    }
  }
  if (a == b) {
    return ans;
  }
  for (int i = 20; i >= 0; i--) {
    if (f[a][i] != f[b][i]) {
      ans = max(ans, max(ma[a][i], ma[b][i]));
      a = f[a][i];
      b = f[b][i];
    }
  }
  ans = max(ans, max(ma[a][0], ma[b][0]));
  return ans;
}
int k[200010];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &ee[i].u, &ee[i].v, &ee[i].w);
    ee[i].bh = i;
  }
  sort(ee + 1, ee + m + 1, cmp);
  for (int i = 1; i <= n; i++) ff[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = find(ee[i].u), v = find(ee[i].v);
    if (u != v) {
      ff[u] = v;
      ee[i].ju = 1;
      add(ee[i].u, ee[i].v, ee[i].w);
      add(ee[i].v, ee[i].u, ee[i].w);
    }
  }
  dfs(1, 0);
  memset(d, -1, sizeof(d));
  memset(k, 0x3f3f3f3f, sizeof(k));
  for (int i = 1; i <= m; i++) {
    if (!ee[i].ju) {
      int yy = llca(ee[i].u, ee[i].v);
      d[ee[i].bh] = yy - 1;
    }
  }
  for (int i = 1; i <= n; i++) ff[i] = i;
  for (int i = 1; i <= m; i++) {
    if (!ee[i].ju) {
      int u = find(ee[i].u), v = find(ee[i].v);
      while (u != v) {
        if (dep[u] < dep[v]) swap(u, v);
        k[u] = ee[i].w - 1;
        ff[u] = f[u][0];
        u = find(u);
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    if (ee[i].ju) {
      if (dep[ee[i].u] < dep[ee[i].v]) swap(ee[i].u, ee[i].v);
      if (f[ee[i].u][0] == ee[i].v && k[ee[i].u] != 0x3f3f3f3f) {
        d[ee[i].bh] = k[ee[i].u];
      }
    }
  }
  for (int i = 1; i <= m; i++) printf("%d ", d[i]);
  return 0;
}

### Prompt

Please provide a cpp coded solution to the problem described below:

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 n, m, q;
struct ss {
  int u, v, w, bh;
  bool ju = 0;
} ee[200010];
bool cmp(ss a, ss b) { return a.w < b.w; }
int ff[200010], d[200010];
int find(int a) {
  if (ff[a] == a) {
    return a;
  }
  return ff[a] = find(ff[a]);
}
int head[200010], cnt = 0;
struct sd {
  int to, next, dis;
} e[400010];
inline void add(int a, int b, int c) {
  cnt++;
  e[cnt].to = b;
  e[cnt].dis = c;
  e[cnt].next = head[a];
  head[a] = cnt;
}
int dep[200010], f[200010][22], ma[200010][22];
void dfs(int u, int fa) {
  f[u][0] = fa;
  dep[u] = dep[fa] + 1;
  for (int i = 1; i <= 20; i++) {
    f[u][i] = f[f[u][i - 1]][i - 1];
    ma[u][i] = max(ma[u][i - 1], ma[f[u][i - 1]][i - 1]);
  }
  for (int i = head[u]; i != 0; i = e[i].next) {
    if (e[i].to == fa) {
      continue;
    }
    ma[e[i].to][0] = e[i].dis;
    dfs(e[i].to, u);
  }
  return;
}
int llca(int a, int b) {
  int ans = 0;
  if (dep[a] < dep[b]) {
    swap(a, b);
  }
  for (int i = 20; i >= 0; i--) {
    if (dep[a] - (1 << i) >= dep[b]) {
      ans = max(ans, ma[a][i]);
      a = f[a][i];
    }
  }
  if (a == b) {
    return ans;
  }
  for (int i = 20; i >= 0; i--) {
    if (f[a][i] != f[b][i]) {
      ans = max(ans, max(ma[a][i], ma[b][i]));
      a = f[a][i];
      b = f[b][i];
    }
  }
  ans = max(ans, max(ma[a][0], ma[b][0]));
  return ans;
}
int k[200010];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &ee[i].u, &ee[i].v, &ee[i].w);
    ee[i].bh = i;
  }
  sort(ee + 1, ee + m + 1, cmp);
  for (int i = 1; i <= n; i++) ff[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = find(ee[i].u), v = find(ee[i].v);
    if (u != v) {
      ff[u] = v;
      ee[i].ju = 1;
      add(ee[i].u, ee[i].v, ee[i].w);
      add(ee[i].v, ee[i].u, ee[i].w);
    }
  }
  dfs(1, 0);
  memset(d, -1, sizeof(d));
  memset(k, 0x3f3f3f3f, sizeof(k));
  for (int i = 1; i <= m; i++) {
    if (!ee[i].ju) {
      int yy = llca(ee[i].u, ee[i].v);
      d[ee[i].bh] = yy - 1;
    }
  }
  for (int i = 1; i <= n; i++) ff[i] = i;
  for (int i = 1; i <= m; i++) {
    if (!ee[i].ju) {
      int u = find(ee[i].u), v = find(ee[i].v);
      while (u != v) {
        if (dep[u] < dep[v]) swap(u, v);
        k[u] = ee[i].w - 1;
        ff[u] = f[u][0];
        u = find(u);
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    if (ee[i].ju) {
      if (dep[ee[i].u] < dep[ee[i].v]) swap(ee[i].u, ee[i].v);
      if (f[ee[i].u][0] == ee[i].v && k[ee[i].u] != 0x3f3f3f3f) {
        d[ee[i].bh] = k[ee[i].u];
      }
    }
  }
  for (int i = 1; i <= m; i++) printf("%d ", d[i]);
  return 0;
}

```