#include <bits/stdc++.h>
using namespace std;
int n, m, a, w[500050], fath[500050], son[500050], top[500050], size[500050],
    fw[500050], val[500050], ans[1000050], dis[500050];
int root, ls[500050 << 2], rs[500050 << 2], mn[500050 << 2], mx[500050 << 2],
    tot = 0, father[500050], times = 0, ret, nume = 1, g[500050];
struct edge {
  int v, w, nxt;
} e[1000050];
struct pnt {
  int u, v, w, id, flag;
} p[1000050];
bool cmp1(const pnt &x, const pnt &y) { return x.w < y.w; }
bool cmp2(const pnt &x, const pnt &y) { return x.flag < y.flag; }
void addedge(int u, int v, int w) {
  e[++nume].v = v;
  e[nume].w = w;
  e[nume].nxt = g[u];
  g[u] = nume;
  e[++nume].v = u;
  e[nume].w = w;
  e[nume].nxt = g[v];
  g[v] = nume;
}
int getfather(int x) {
  if (x != father[x]) father[x] = getfather(father[x]);
  return father[x];
}
void kruskal() {
  sort(p + 1, p + m + 1, cmp1);
  for (int i = 1; i <= n; i++) father[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = p[i].u, v = p[i].v, f1 = getfather(u), f2 = getfather(v);
    if (f1 == f2)
      p[i].flag = 0;
    else {
      father[f1] = f2;
      p[i].flag = 1;
      addedge(p[i].u, p[i].v, p[i].w);
    }
  }
}
void dfs1(int x) {
  size[x] = 1;
  son[x] = 0;
  for (int i = g[x]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (v == fath[x]) continue;
    fath[v] = x;
    val[v] = e[i].w;
    dis[v] = dis[x] + 1;
    dfs1(v);
    size[x] += size[v];
    if (size[v] > size[son[x]]) son[x] = v;
  }
}
void dfs2(int x, int father) {
  w[x] = ++times;
  fw[times] = x;
  top[x] = father;
  if (son[x]) dfs2(son[x], father);
  for (int i = g[x]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (v == fath[x] || v == son[x]) continue;
    dfs2(v, v);
  }
}
void build(int &now, int left, int right) {
  now = ++tot;
  mn[now] = 2000000000;
  if (left == right) {
    mx[now] = val[fw[left]];
    return;
  }
  int mid = (left + right) >> 1;
  build(ls[now], left, mid);
  build(rs[now], mid + 1, right);
  mx[now] = max(mx[ls[now]], mx[rs[now]]);
}
void modify(int now, int left, int right, int l, int r, int val) {
  if (left == l && right == r) {
    mn[now] = min(mn[now], val);
    return;
  }
  int mid = (left + right) >> 1;
  if (r <= mid)
    modify(ls[now], left, mid, l, r, val);
  else if (l >= mid + 1)
    modify(rs[now], mid + 1, right, l, r, val);
  else {
    modify(ls[now], left, mid, l, mid, val);
    modify(rs[now], mid + 1, right, mid + 1, r, val);
  }
}
void ask_mn(int now, int left, int right, int pos) {
  ret = min(ret, mn[now]);
  if (left == right) return;
  int mid = (left + right) >> 1;
  if (pos <= mid)
    ask_mn(ls[now], left, mid, pos);
  else
    ask_mn(rs[now], mid + 1, right, pos);
}
int ask_mx(int now, int left, int right, int l, int r) {
  if (left == l && right == r) return mx[now];
  int mid = (left + right) >> 1;
  if (r <= mid)
    return ask_mx(ls[now], left, mid, l, r);
  else if (l >= mid + 1)
    return ask_mx(rs[now], mid + 1, right, l, r);
  else
    return max(ask_mx(ls[now], left, mid, l, mid),
               ask_mx(rs[now], mid + 1, right, mid + 1, r));
}
int get_mx(int u, int v) {
  int f1 = top[u], f2 = top[v], ret = 0;
  while (f1 != f2) {
    if (dis[f1] < dis[f2]) {
      swap(f1, f2);
      swap(u, v);
    }
    ret = max(ret, ask_mx(root, 1, n, w[f1], w[u]));
    u = fath[f1];
    f1 = top[u];
  }
  if (u == v) return ret;
  if (dis[u] > dis[v]) swap(u, v);
  ret = max(ret, ask_mx(root, 1, n, w[son[u]], w[v]));
  return ret;
}
void modi_mn(int u, int v, int ww) {
  int f1 = top[u], f2 = top[v], ret = 0;
  while (f1 != f2) {
    if (dis[f1] < dis[f2]) {
      swap(f1, f2);
      swap(u, v);
    }
    modify(root, 1, n, w[f1], w[u], ww);
    u = fath[f1];
    f1 = top[u];
  }
  if (u == v) return;
  if (dis[u] > dis[v]) swap(u, v);
  modify(root, 1, n, w[son[u]], w[v], ww);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &p[i].u, &p[i].v, &p[i].w);
    p[i].id = i;
    p[i].flag = 0;
  }
  kruskal();
  dfs1(1);
  dfs2(1, 1);
  build(root, 1, n);
  sort(p + 1, p + m + 1, cmp2);
  int i = 1;
  for (;;) {
    if (p[i].flag) break;
    if (i > m) break;
    ans[p[i].id] = get_mx(p[i].u, p[i].v) - 1;
    modi_mn(p[i].u, p[i].v, p[i].w);
    i++;
  }
  for (;;) {
    if (i > m) break;
    int u = p[i].u, v = p[i].v;
    if (fath[v] == u) swap(u, v);
    ret = 2000000000;
    ask_mn(root, 1, n, w[u]);
    ans[p[i].id] = ret - 1;
    i++;
  }
  for (int i = 1; i <= m; i++) {
    if (ans[i] == 2000000000 - 1)
      printf("-1 ");
    else
      printf("%d ", ans[i]);
  }
  return 0;
}

### Prompt

Please create a solution in Cpp 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 n, m, a, w[500050], fath[500050], son[500050], top[500050], size[500050],
    fw[500050], val[500050], ans[1000050], dis[500050];
int root, ls[500050 << 2], rs[500050 << 2], mn[500050 << 2], mx[500050 << 2],
    tot = 0, father[500050], times = 0, ret, nume = 1, g[500050];
struct edge {
  int v, w, nxt;
} e[1000050];
struct pnt {
  int u, v, w, id, flag;
} p[1000050];
bool cmp1(const pnt &x, const pnt &y) { return x.w < y.w; }
bool cmp2(const pnt &x, const pnt &y) { return x.flag < y.flag; }
void addedge(int u, int v, int w) {
  e[++nume].v = v;
  e[nume].w = w;
  e[nume].nxt = g[u];
  g[u] = nume;
  e[++nume].v = u;
  e[nume].w = w;
  e[nume].nxt = g[v];
  g[v] = nume;
}
int getfather(int x) {
  if (x != father[x]) father[x] = getfather(father[x]);
  return father[x];
}
void kruskal() {
  sort(p + 1, p + m + 1, cmp1);
  for (int i = 1; i <= n; i++) father[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = p[i].u, v = p[i].v, f1 = getfather(u), f2 = getfather(v);
    if (f1 == f2)
      p[i].flag = 0;
    else {
      father[f1] = f2;
      p[i].flag = 1;
      addedge(p[i].u, p[i].v, p[i].w);
    }
  }
}
void dfs1(int x) {
  size[x] = 1;
  son[x] = 0;
  for (int i = g[x]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (v == fath[x]) continue;
    fath[v] = x;
    val[v] = e[i].w;
    dis[v] = dis[x] + 1;
    dfs1(v);
    size[x] += size[v];
    if (size[v] > size[son[x]]) son[x] = v;
  }
}
void dfs2(int x, int father) {
  w[x] = ++times;
  fw[times] = x;
  top[x] = father;
  if (son[x]) dfs2(son[x], father);
  for (int i = g[x]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (v == fath[x] || v == son[x]) continue;
    dfs2(v, v);
  }
}
void build(int &now, int left, int right) {
  now = ++tot;
  mn[now] = 2000000000;
  if (left == right) {
    mx[now] = val[fw[left]];
    return;
  }
  int mid = (left + right) >> 1;
  build(ls[now], left, mid);
  build(rs[now], mid + 1, right);
  mx[now] = max(mx[ls[now]], mx[rs[now]]);
}
void modify(int now, int left, int right, int l, int r, int val) {
  if (left == l && right == r) {
    mn[now] = min(mn[now], val);
    return;
  }
  int mid = (left + right) >> 1;
  if (r <= mid)
    modify(ls[now], left, mid, l, r, val);
  else if (l >= mid + 1)
    modify(rs[now], mid + 1, right, l, r, val);
  else {
    modify(ls[now], left, mid, l, mid, val);
    modify(rs[now], mid + 1, right, mid + 1, r, val);
  }
}
void ask_mn(int now, int left, int right, int pos) {
  ret = min(ret, mn[now]);
  if (left == right) return;
  int mid = (left + right) >> 1;
  if (pos <= mid)
    ask_mn(ls[now], left, mid, pos);
  else
    ask_mn(rs[now], mid + 1, right, pos);
}
int ask_mx(int now, int left, int right, int l, int r) {
  if (left == l && right == r) return mx[now];
  int mid = (left + right) >> 1;
  if (r <= mid)
    return ask_mx(ls[now], left, mid, l, r);
  else if (l >= mid + 1)
    return ask_mx(rs[now], mid + 1, right, l, r);
  else
    return max(ask_mx(ls[now], left, mid, l, mid),
               ask_mx(rs[now], mid + 1, right, mid + 1, r));
}
int get_mx(int u, int v) {
  int f1 = top[u], f2 = top[v], ret = 0;
  while (f1 != f2) {
    if (dis[f1] < dis[f2]) {
      swap(f1, f2);
      swap(u, v);
    }
    ret = max(ret, ask_mx(root, 1, n, w[f1], w[u]));
    u = fath[f1];
    f1 = top[u];
  }
  if (u == v) return ret;
  if (dis[u] > dis[v]) swap(u, v);
  ret = max(ret, ask_mx(root, 1, n, w[son[u]], w[v]));
  return ret;
}
void modi_mn(int u, int v, int ww) {
  int f1 = top[u], f2 = top[v], ret = 0;
  while (f1 != f2) {
    if (dis[f1] < dis[f2]) {
      swap(f1, f2);
      swap(u, v);
    }
    modify(root, 1, n, w[f1], w[u], ww);
    u = fath[f1];
    f1 = top[u];
  }
  if (u == v) return;
  if (dis[u] > dis[v]) swap(u, v);
  modify(root, 1, n, w[son[u]], w[v], ww);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &p[i].u, &p[i].v, &p[i].w);
    p[i].id = i;
    p[i].flag = 0;
  }
  kruskal();
  dfs1(1);
  dfs2(1, 1);
  build(root, 1, n);
  sort(p + 1, p + m + 1, cmp2);
  int i = 1;
  for (;;) {
    if (p[i].flag) break;
    if (i > m) break;
    ans[p[i].id] = get_mx(p[i].u, p[i].v) - 1;
    modi_mn(p[i].u, p[i].v, p[i].w);
    i++;
  }
  for (;;) {
    if (i > m) break;
    int u = p[i].u, v = p[i].v;
    if (fath[v] == u) swap(u, v);
    ret = 2000000000;
    ask_mn(root, 1, n, w[u]);
    ans[p[i].id] = ret - 1;
    i++;
  }
  for (int i = 1; i <= m; i++) {
    if (ans[i] == 2000000000 - 1)
      printf("-1 ");
    else
      printf("%d ", ans[i]);
  }
  return 0;
}

```