#include <bits/stdc++.h>
const int N = 2e5 + 10, INF = 2e9;
int cnt, la[N];
struct edg {
  int v, w, la;
  inline void set(int _v, int _w, int _la) {
    v = _v;
    w = _w;
    la = _la;
  }
} g[N << 1];
inline void add(int u, int v, int w) {
  cnt++;
  g[cnt].set(v, w, la[u]);
  la[u] = cnt;
}
int f[N];
int find(int x) { return f[x] > 0 ? f[x] = find(f[x]) : x; }
bool un(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return false;
  if (f[x] > f[y])
    f[y] += f[x], f[x] = y;
  else
    f[x] += f[y], f[y] = x;
  return true;
}
struct edge {
  int u, v, w;
  bool flag;
  int id;
  inline void read(int i) {
    scanf("%d%d%d", &u, &v, &w);
    id = i;
  }
  bool operator<(const edge &e) const { return w < e.w; }
  inline void add() {
    ::add(u, v, w);
    ::add(v, u, w);
  }
  inline void un() {
    if (flag = ::un(u, v)) add();
  }
} e[N];
int n, mx[N << 2], tag[N << 2], val[N];
inline int max(int a, int b) { return a > b ? a : b; }
inline void fmin(int &a, int b) {
  if (a > b) a = b;
}
void build(int rt, int l, int r) {
  if (l == r) {
    mx[rt] = val[l];
    return;
  }
  int m = (l + r) >> 1;
  build(rt << 1, l, m);
  build(rt << 1 | 1, m + 1, r);
  mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
}
int update(int rt, int l, int r, int ql, int qr, int x) {
  if (ql <= l && r <= qr) {
    if (!tag[rt]) tag[rt] = x;
    return mx[rt];
  }
  int m = (l + r) >> 1, a = 0, b = 0;
  if (ql <= m) a = update(rt << 1, l, m, ql, qr, x);
  if (qr > m) b = update(rt << 1 | 1, m + 1, r, ql, qr, x);
  return max(a, b);
}
void query(int rt, int l, int r, int x, int &y) {
  if (tag[rt]) fmin(y, tag[rt]);
  if (l == r) {
    if (y == INF) y = 0;
    return;
  }
  int m = (l + r) >> 1;
  if (x <= m)
    query(rt << 1, l, m, x, y);
  else
    query(rt << 1 | 1, m + 1, r, x, y);
}
int son[N], siz[N], top[N], dep[N], id[N];
void dfs1(int now, int las) {
  int i;
  siz[now] = 1;
  f[now] = las;
  dep[now] = dep[las] + 1;
  for (i = la[now]; i; i = g[i].la)
    if (g[i].v != las) {
      dfs1(g[i].v, now);
      siz[now] += siz[g[i].v];
      if (siz[g[i].v] > siz[son[now]]) son[now] = g[i].v;
    }
}
void dfs2(int now, int tp) {
  top[now] = tp;
  id[now] = ++cnt;
  if (son[now]) dfs2(son[now], tp);
  int i;
  for (i = la[now]; i; i = g[i].la)
    if (g[i].v == f[now])
      val[id[now]] = g[i].w;
    else if (g[i].v != son[now])
      dfs2(g[i].v, g[i].v);
}
int LCA(int x, int y, int val) {
  int mx = 0, tmp;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) tmp = x, x = y, y = tmp;
    tmp = update(1, 1, n, id[top[x]], id[x], val);
    if (mx < tmp) mx = tmp;
    x = f[top[x]];
  }
  if (x != y) {
    if (dep[x] > dep[y]) tmp = x, x = y, y = tmp;
    tmp = update(1, 1, n, id[x] + 1, id[y], val);
    if (mx < tmp) mx = tmp;
  }
  return mx;
}
int main() {
  int i, m;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) e[i].read(i);
  std::sort(e + 1, e + 1 + m);
  memset(f, -1, sizeof f);
  for (i = 1; i <= m; i++) e[i].un();
  cnt = 0;
  dfs1(1, 0);
  dfs2(1, 1);
  build(1, 1, n);
  for (i = 1; i <= m; i++)
    if (!e[i].flag)
      val[e[i].id] = LCA(e[i].u, e[i].v, e[i].w);
    else
      val[e[i].id] = INF;
  for (i = 1; i <= m; i++)
    if (e[i].flag)
      query(1, 1, n, dep[e[i].v] > dep[e[i].u] ? id[e[i].v] : id[e[i].u],
            val[e[i].id]);
  for (i = 1; i <= m; i++) printf("%d ", val[i] - 1);
  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>
const int N = 2e5 + 10, INF = 2e9;
int cnt, la[N];
struct edg {
  int v, w, la;
  inline void set(int _v, int _w, int _la) {
    v = _v;
    w = _w;
    la = _la;
  }
} g[N << 1];
inline void add(int u, int v, int w) {
  cnt++;
  g[cnt].set(v, w, la[u]);
  la[u] = cnt;
}
int f[N];
int find(int x) { return f[x] > 0 ? f[x] = find(f[x]) : x; }
bool un(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return false;
  if (f[x] > f[y])
    f[y] += f[x], f[x] = y;
  else
    f[x] += f[y], f[y] = x;
  return true;
}
struct edge {
  int u, v, w;
  bool flag;
  int id;
  inline void read(int i) {
    scanf("%d%d%d", &u, &v, &w);
    id = i;
  }
  bool operator<(const edge &e) const { return w < e.w; }
  inline void add() {
    ::add(u, v, w);
    ::add(v, u, w);
  }
  inline void un() {
    if (flag = ::un(u, v)) add();
  }
} e[N];
int n, mx[N << 2], tag[N << 2], val[N];
inline int max(int a, int b) { return a > b ? a : b; }
inline void fmin(int &a, int b) {
  if (a > b) a = b;
}
void build(int rt, int l, int r) {
  if (l == r) {
    mx[rt] = val[l];
    return;
  }
  int m = (l + r) >> 1;
  build(rt << 1, l, m);
  build(rt << 1 | 1, m + 1, r);
  mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
}
int update(int rt, int l, int r, int ql, int qr, int x) {
  if (ql <= l && r <= qr) {
    if (!tag[rt]) tag[rt] = x;
    return mx[rt];
  }
  int m = (l + r) >> 1, a = 0, b = 0;
  if (ql <= m) a = update(rt << 1, l, m, ql, qr, x);
  if (qr > m) b = update(rt << 1 | 1, m + 1, r, ql, qr, x);
  return max(a, b);
}
void query(int rt, int l, int r, int x, int &y) {
  if (tag[rt]) fmin(y, tag[rt]);
  if (l == r) {
    if (y == INF) y = 0;
    return;
  }
  int m = (l + r) >> 1;
  if (x <= m)
    query(rt << 1, l, m, x, y);
  else
    query(rt << 1 | 1, m + 1, r, x, y);
}
int son[N], siz[N], top[N], dep[N], id[N];
void dfs1(int now, int las) {
  int i;
  siz[now] = 1;
  f[now] = las;
  dep[now] = dep[las] + 1;
  for (i = la[now]; i; i = g[i].la)
    if (g[i].v != las) {
      dfs1(g[i].v, now);
      siz[now] += siz[g[i].v];
      if (siz[g[i].v] > siz[son[now]]) son[now] = g[i].v;
    }
}
void dfs2(int now, int tp) {
  top[now] = tp;
  id[now] = ++cnt;
  if (son[now]) dfs2(son[now], tp);
  int i;
  for (i = la[now]; i; i = g[i].la)
    if (g[i].v == f[now])
      val[id[now]] = g[i].w;
    else if (g[i].v != son[now])
      dfs2(g[i].v, g[i].v);
}
int LCA(int x, int y, int val) {
  int mx = 0, tmp;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) tmp = x, x = y, y = tmp;
    tmp = update(1, 1, n, id[top[x]], id[x], val);
    if (mx < tmp) mx = tmp;
    x = f[top[x]];
  }
  if (x != y) {
    if (dep[x] > dep[y]) tmp = x, x = y, y = tmp;
    tmp = update(1, 1, n, id[x] + 1, id[y], val);
    if (mx < tmp) mx = tmp;
  }
  return mx;
}
int main() {
  int i, m;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) e[i].read(i);
  std::sort(e + 1, e + 1 + m);
  memset(f, -1, sizeof f);
  for (i = 1; i <= m; i++) e[i].un();
  cnt = 0;
  dfs1(1, 0);
  dfs2(1, 1);
  build(1, 1, n);
  for (i = 1; i <= m; i++)
    if (!e[i].flag)
      val[e[i].id] = LCA(e[i].u, e[i].v, e[i].w);
    else
      val[e[i].id] = INF;
  for (i = 1; i <= m; i++)
    if (e[i].flag)
      query(1, 1, n, dep[e[i].v] > dep[e[i].u] ? id[e[i].v] : id[e[i].u],
            val[e[i].id]);
  for (i = 1; i <= m; i++) printf("%d ", val[i] - 1);
  return 0;
}

```