#include <bits/stdc++.h>
using namespace std;
int read() {
  char ch = getchar();
  int x = 0, pd = 0;
  while (ch < '0' || ch > '9') pd ^= ch == '-', ch = getchar();
  while ('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
  return pd ? -x : x;
}
const int maxn = 200005;
const int inf = 0x3f3f3f3f;
int n, m;
struct Edge {
  int a, b, w, id, chs;
} E[maxn];
int ans[maxn];
bool cmp1(Edge x, Edge y) { return x.w < y.w; }
bool cmp2(Edge x, Edge y) { return x.id < y.id; }
int e, bg[maxn], to[maxn * 2], nx[maxn * 2], c[maxn * 2];
void link(int u, int v, int _c) {
  to[++e] = v, c[e] = _c, nx[e] = bg[u], bg[u] = e;
}
struct Union {
  int fa[maxn];
  int findfa(int x) { return fa[x] == x ? x : fa[x] = findfa(fa[x]); }
  void init() {
    for (int i = 1; i <= n; i++) fa[i] = i;
  }
  bool check(int x, int y) { return findfa(x) == findfa(y); }
  void connect(int x, int y) { fa[findfa(x)] = y; }
} U;
void kruskal() {
  U.init();
  sort(E + 1, E + m + 1, cmp1);
  for (int i = 1; i <= m; i++) {
    int a = E[i].a, b = E[i].b;
    if (U.check(a, b)) continue;
    U.connect(a, b);
    link(a, b, E[i].w), link(b, a, E[i].w);
    E[i].chs = 1;
  }
}
int fa[maxn], dep[maxn], size[maxn], son[maxn];
int tot, id[maxn], rk[maxn], top[maxn];
int val[maxn];
void dfs1(int u) {
  dep[u] = dep[fa[u]] + 1, size[u] = 1;
  for (int i = bg[u]; i; i = nx[i]) {
    int v = to[i];
    if (v == fa[u]) continue;
    val[v] = c[i], fa[v] = u, dfs1(v);
    size[u] += size[v];
    if (size[v] > size[son[u]]) son[u] = v;
  }
}
void dfs2(int u, int t) {
  top[u] = t, rk[id[u] = ++tot] = u;
  if (son[u])
    dfs2(son[u], t);
  else
    return;
  for (int i = bg[u]; i; i = nx[i])
    if (to[i] != son[u] && to[i] != fa[u]) dfs2(to[i], to[i]);
}
int f[maxn][20];
void buildST() {
  for (int i = 2; i <= n; i++) f[i][0] = val[rk[i]];
  for (int j = 1, L = 1; j < 18; j++, L <<= 1)
    for (int i = 1; i + L * 2 - 1 <= n; i++)
      f[i][j] = max(f[i][j - 1], f[i + L][j - 1]);
}
int queryST(int x, int y) {
  int len = y - x + 1, lg = log2(len);
  return max(f[x][lg], f[y - (1 << lg) + 1][lg]);
}
int qrange(int x, int y) {
  int res = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] > dep[top[y]]) swap(x, y);
    res = max(res, queryST(id[top[y]], id[y]));
    y = fa[top[y]];
  }
  if (x == y) return res;
  if (dep[x] > dep[y]) swap(x, y);
  return max(res, queryST(id[x] + 1, id[y]));
}
int mn[maxn << 2], tg[maxn << 2];
void build(int rt, int l, int r) {
  mn[rt] = tg[rt] = inf;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build((rt << 1), l, mid), build((rt << 1 | 1), mid + 1, r);
}
void pushdown(int rt) {
  int t = tg[rt];
  mn[(rt << 1)] = min(mn[(rt << 1)], t), tg[(rt << 1)] = min(tg[(rt << 1)], t);
  mn[(rt << 1 | 1)] = min(mn[(rt << 1 | 1)], t),
                tg[(rt << 1 | 1)] = min(tg[(rt << 1 | 1)], t);
  tg[rt] = inf;
}
void update(int rt, int l, int r, int L, int R, int x) {
  if (L <= l && r <= R) {
    mn[rt] = min(mn[rt], x);
    tg[rt] = min(tg[rt], x);
    return;
  }
  if (tg[rt] < inf) pushdown(rt);
  int mid = (l + r) >> 1;
  if (L <= mid) update((rt << 1), l, mid, L, R, x);
  if (R > mid) update((rt << 1 | 1), mid + 1, r, L, R, x);
}
int query(int rt, int l, int r, int x) {
  if (l == r) return mn[rt];
  if (tg[rt] < inf) pushdown(rt);
  int mid = (l + r) >> 1;
  if (x <= mid) return query((rt << 1), l, mid, x);
  return query((rt << 1 | 1), mid + 1, r, x);
}
void urange(int x, int y, int z) {
  while (top[x] != top[y]) {
    if (dep[top[x]] > dep[top[y]]) swap(x, y);
    update(1, 1, n, id[top[y]], id[y], z);
    y = fa[top[y]];
  }
  if (x == y) return;
  if (dep[x] > dep[y]) swap(x, y);
  update(1, 1, n, id[x] + 1, id[y], z);
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    E[i].a = read(), E[i].b = read(), E[i].w = read();
    E[i].id = i;
  }
  kruskal();
  dfs1(1), dfs2(1, 1);
  build(1, 1, n);
  buildST();
  sort(E + 1, E + m + 1, cmp2);
  for (int i = 1; i <= m; i++)
    if (!E[i].chs) {
      ans[i] = qrange(E[i].a, E[i].b) - 1;
      urange(E[i].a, E[i].b, E[i].w);
    }
  for (int i = 1; i <= m; i++)
    if (E[i].chs) {
      int x = query(1, 1, n, max(id[E[i].a], id[E[i].b]));
      printf("%d ", x == inf ? -1 : x - 1);
    } else
      printf("%d ", ans[i]);
  return 0;
}

### Prompt

Please formulate 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;
int read() {
  char ch = getchar();
  int x = 0, pd = 0;
  while (ch < '0' || ch > '9') pd ^= ch == '-', ch = getchar();
  while ('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
  return pd ? -x : x;
}
const int maxn = 200005;
const int inf = 0x3f3f3f3f;
int n, m;
struct Edge {
  int a, b, w, id, chs;
} E[maxn];
int ans[maxn];
bool cmp1(Edge x, Edge y) { return x.w < y.w; }
bool cmp2(Edge x, Edge y) { return x.id < y.id; }
int e, bg[maxn], to[maxn * 2], nx[maxn * 2], c[maxn * 2];
void link(int u, int v, int _c) {
  to[++e] = v, c[e] = _c, nx[e] = bg[u], bg[u] = e;
}
struct Union {
  int fa[maxn];
  int findfa(int x) { return fa[x] == x ? x : fa[x] = findfa(fa[x]); }
  void init() {
    for (int i = 1; i <= n; i++) fa[i] = i;
  }
  bool check(int x, int y) { return findfa(x) == findfa(y); }
  void connect(int x, int y) { fa[findfa(x)] = y; }
} U;
void kruskal() {
  U.init();
  sort(E + 1, E + m + 1, cmp1);
  for (int i = 1; i <= m; i++) {
    int a = E[i].a, b = E[i].b;
    if (U.check(a, b)) continue;
    U.connect(a, b);
    link(a, b, E[i].w), link(b, a, E[i].w);
    E[i].chs = 1;
  }
}
int fa[maxn], dep[maxn], size[maxn], son[maxn];
int tot, id[maxn], rk[maxn], top[maxn];
int val[maxn];
void dfs1(int u) {
  dep[u] = dep[fa[u]] + 1, size[u] = 1;
  for (int i = bg[u]; i; i = nx[i]) {
    int v = to[i];
    if (v == fa[u]) continue;
    val[v] = c[i], fa[v] = u, dfs1(v);
    size[u] += size[v];
    if (size[v] > size[son[u]]) son[u] = v;
  }
}
void dfs2(int u, int t) {
  top[u] = t, rk[id[u] = ++tot] = u;
  if (son[u])
    dfs2(son[u], t);
  else
    return;
  for (int i = bg[u]; i; i = nx[i])
    if (to[i] != son[u] && to[i] != fa[u]) dfs2(to[i], to[i]);
}
int f[maxn][20];
void buildST() {
  for (int i = 2; i <= n; i++) f[i][0] = val[rk[i]];
  for (int j = 1, L = 1; j < 18; j++, L <<= 1)
    for (int i = 1; i + L * 2 - 1 <= n; i++)
      f[i][j] = max(f[i][j - 1], f[i + L][j - 1]);
}
int queryST(int x, int y) {
  int len = y - x + 1, lg = log2(len);
  return max(f[x][lg], f[y - (1 << lg) + 1][lg]);
}
int qrange(int x, int y) {
  int res = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] > dep[top[y]]) swap(x, y);
    res = max(res, queryST(id[top[y]], id[y]));
    y = fa[top[y]];
  }
  if (x == y) return res;
  if (dep[x] > dep[y]) swap(x, y);
  return max(res, queryST(id[x] + 1, id[y]));
}
int mn[maxn << 2], tg[maxn << 2];
void build(int rt, int l, int r) {
  mn[rt] = tg[rt] = inf;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build((rt << 1), l, mid), build((rt << 1 | 1), mid + 1, r);
}
void pushdown(int rt) {
  int t = tg[rt];
  mn[(rt << 1)] = min(mn[(rt << 1)], t), tg[(rt << 1)] = min(tg[(rt << 1)], t);
  mn[(rt << 1 | 1)] = min(mn[(rt << 1 | 1)], t),
                tg[(rt << 1 | 1)] = min(tg[(rt << 1 | 1)], t);
  tg[rt] = inf;
}
void update(int rt, int l, int r, int L, int R, int x) {
  if (L <= l && r <= R) {
    mn[rt] = min(mn[rt], x);
    tg[rt] = min(tg[rt], x);
    return;
  }
  if (tg[rt] < inf) pushdown(rt);
  int mid = (l + r) >> 1;
  if (L <= mid) update((rt << 1), l, mid, L, R, x);
  if (R > mid) update((rt << 1 | 1), mid + 1, r, L, R, x);
}
int query(int rt, int l, int r, int x) {
  if (l == r) return mn[rt];
  if (tg[rt] < inf) pushdown(rt);
  int mid = (l + r) >> 1;
  if (x <= mid) return query((rt << 1), l, mid, x);
  return query((rt << 1 | 1), mid + 1, r, x);
}
void urange(int x, int y, int z) {
  while (top[x] != top[y]) {
    if (dep[top[x]] > dep[top[y]]) swap(x, y);
    update(1, 1, n, id[top[y]], id[y], z);
    y = fa[top[y]];
  }
  if (x == y) return;
  if (dep[x] > dep[y]) swap(x, y);
  update(1, 1, n, id[x] + 1, id[y], z);
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    E[i].a = read(), E[i].b = read(), E[i].w = read();
    E[i].id = i;
  }
  kruskal();
  dfs1(1), dfs2(1, 1);
  build(1, 1, n);
  buildST();
  sort(E + 1, E + m + 1, cmp2);
  for (int i = 1; i <= m; i++)
    if (!E[i].chs) {
      ans[i] = qrange(E[i].a, E[i].b) - 1;
      urange(E[i].a, E[i].b, E[i].w);
    }
  for (int i = 1; i <= m; i++)
    if (E[i].chs) {
      int x = query(1, 1, n, max(id[E[i].a], id[E[i].b]));
      printf("%d ", x == inf ? -1 : x - 1);
    } else
      printf("%d ", ans[i]);
  return 0;
}

```