#include <bits/stdc++.h>
namespace Rstd {
using namespace std;
template <typename T>
inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T>
inline void read(T &x) {
  char c = getchar();
  x = 0;
  bool f = 0;
  while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
  while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
  x = f ? -x : x;
}
void file() {}
}  // namespace Rstd
using namespace Rstd;
int head[200005], cnt, f[200005];
int fa[200005], size[200005], id[200005], tot, son[200005], dep[200005],
    top[200005], a[200005], w[200005], pre[200005];
struct T {
  int maxx, minn = 2139062143;
} tree[200005 << 2];
int lzt[200005 << 2], n, m, ans[200005];
struct edge {
  int to, nxt, w, id;
} e[200005 << 1];
struct node {
  int u, v, w, id;
  bool used;
  bool operator<(const node &A) const { return w < A.w; }
} line[200005];
int getfa(int x) { return (x == f[x]) ? x : f[x] = getfa(f[x]); }
void add(int u, int v, int w, int id) {
  e[++cnt] = (edge){v, head[u], w, id};
  head[u] = cnt;
}
void dfs1(int u, int father) {
  size[u] = 1;
  fa[u] = father;
  dep[u] = dep[father] + 1;
  for (int i = head[(u)]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == father) continue;
    pre[v] = e[i].id;
    a[v] = e[i].w;
    dfs1(v, u);
    size[u] += size[v];
    if (size[son[u]] < size[v]) son[u] = v;
  }
}
void dfs2(int u, int Top) {
  top[u] = Top;
  id[u] = ++tot;
  w[tot] = a[u];
  if (!son[u]) return;
  dfs2(son[u], Top);
  for (int i = head[(u)]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == fa[u] || v == son[u]) continue;
    dfs2(v, v);
  }
}
void pushup(int rt) {
  tree[rt].maxx = max(tree[rt << 1].maxx, tree[rt << 1 | 1].maxx);
  tree[rt].minn = min(tree[rt << 1].minn, tree[rt << 1 | 1].minn);
}
void build(int rt, int l, int r) {
  if (l == r) {
    tree[rt].maxx = w[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
  pushup(rt);
}
void pushdown(int rt) {
  if (lzt[rt] == 2139062143) return;
  chkmin(tree[rt << 1].minn, lzt[rt]);
  chkmin(lzt[rt << 1], lzt[rt]);
  chkmin(tree[rt << 1 | 1].minn, lzt[rt]);
  chkmin(lzt[rt << 1 | 1], lzt[rt]);
  lzt[rt] = 2139062143;
}
void update(int rt, int l, int r, int x, int y, int val) {
  if (x <= l && r <= y) {
    chkmin(tree[rt].minn, val);
    chkmin(lzt[rt], val);
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(rt);
  if (x <= mid) update(rt << 1, l, mid, x, y, val);
  if (mid < y) update(rt << 1 | 1, mid + 1, r, x, y, val);
  pushup(rt);
}
int query_min(int rt, int l, int r, int x) {
  if (l == r) {
    return tree[rt].minn;
  }
  int mid = (l + r) >> 1;
  pushdown(rt);
  if (x <= mid)
    return query_min(rt << 1, l, mid, x);
  else
    return query_min(rt << 1 | 1, mid + 1, r, x);
}
int query_max(int rt, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    return tree[rt].maxx;
  }
  int mid = (l + r) >> 1, res = 0;
  pushdown(rt);
  if (x <= mid) chkmax(res, query_max(rt << 1, l, mid, x, y));
  if (mid < y) chkmax(res, query_max(rt << 1 | 1, mid + 1, r, x, y));
  return res;
}
void Upd(int x, int y, int val) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    update(1, 1, n, id[top[x]], id[x], val);
    x = fa[top[x]];
  }
  if (dep[x] > dep[y]) swap(x, y);
  update(1, 1, n, id[x] + 1, id[y], val);
}
int Qmax(int x, int y) {
  int res = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    chkmax(res, query_max(1, 1, n, id[top[x]], id[x]));
    x = fa[top[x]];
  }
  if (dep[x] > dep[y]) swap(x, y);
  return max(res, query_max(1, 1, n, id[x] + 1, id[y]));
}
int main() {
  file();
  memset(lzt, 127, sizeof(lzt));
  read(n);
  read(m);
  for (int i = 1, u, v, w; i <= m; i++) {
    read(u);
    read(v);
    read(w);
    line[i] = (node){u, v, w, i};
  }
  sort(line + 1, line + m + 1);
  for (int i = (1); i <= (n); i++) f[i] = i;
  for (int i = (1); i <= (m); i++) {
    int u = getfa(line[i].u), v = getfa(line[i].v);
    if (u == v) continue;
    f[u] = v;
    line[i].used = 1;
    add(line[i].u, line[i].v, line[i].w, line[i].id);
    add(line[i].v, line[i].u, line[i].w, line[i].id);
  }
  dfs1(1, 0);
  dfs2(1, 1);
  build(1, 1, n);
  for (int i = (1); i <= (m); i++) {
    if (line[i].used) continue;
    ans[line[i].id] = Qmax(line[i].u, line[i].v) - 1;
    Upd(line[i].u, line[i].v, line[i].w);
  }
  for (int i = (2); i <= (n); i++) {
    ans[pre[i]] = query_min(1, 1, n, id[i]) - 1;
  }
  for (int i = (1); i <= (m); i++)
    printf("%d ", ans[i] == 2139062143 - 1 ? -1 : ans[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>
namespace Rstd {
using namespace std;
template <typename T>
inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T>
inline void read(T &x) {
  char c = getchar();
  x = 0;
  bool f = 0;
  while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
  while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
  x = f ? -x : x;
}
void file() {}
}  // namespace Rstd
using namespace Rstd;
int head[200005], cnt, f[200005];
int fa[200005], size[200005], id[200005], tot, son[200005], dep[200005],
    top[200005], a[200005], w[200005], pre[200005];
struct T {
  int maxx, minn = 2139062143;
} tree[200005 << 2];
int lzt[200005 << 2], n, m, ans[200005];
struct edge {
  int to, nxt, w, id;
} e[200005 << 1];
struct node {
  int u, v, w, id;
  bool used;
  bool operator<(const node &A) const { return w < A.w; }
} line[200005];
int getfa(int x) { return (x == f[x]) ? x : f[x] = getfa(f[x]); }
void add(int u, int v, int w, int id) {
  e[++cnt] = (edge){v, head[u], w, id};
  head[u] = cnt;
}
void dfs1(int u, int father) {
  size[u] = 1;
  fa[u] = father;
  dep[u] = dep[father] + 1;
  for (int i = head[(u)]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == father) continue;
    pre[v] = e[i].id;
    a[v] = e[i].w;
    dfs1(v, u);
    size[u] += size[v];
    if (size[son[u]] < size[v]) son[u] = v;
  }
}
void dfs2(int u, int Top) {
  top[u] = Top;
  id[u] = ++tot;
  w[tot] = a[u];
  if (!son[u]) return;
  dfs2(son[u], Top);
  for (int i = head[(u)]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == fa[u] || v == son[u]) continue;
    dfs2(v, v);
  }
}
void pushup(int rt) {
  tree[rt].maxx = max(tree[rt << 1].maxx, tree[rt << 1 | 1].maxx);
  tree[rt].minn = min(tree[rt << 1].minn, tree[rt << 1 | 1].minn);
}
void build(int rt, int l, int r) {
  if (l == r) {
    tree[rt].maxx = w[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
  pushup(rt);
}
void pushdown(int rt) {
  if (lzt[rt] == 2139062143) return;
  chkmin(tree[rt << 1].minn, lzt[rt]);
  chkmin(lzt[rt << 1], lzt[rt]);
  chkmin(tree[rt << 1 | 1].minn, lzt[rt]);
  chkmin(lzt[rt << 1 | 1], lzt[rt]);
  lzt[rt] = 2139062143;
}
void update(int rt, int l, int r, int x, int y, int val) {
  if (x <= l && r <= y) {
    chkmin(tree[rt].minn, val);
    chkmin(lzt[rt], val);
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(rt);
  if (x <= mid) update(rt << 1, l, mid, x, y, val);
  if (mid < y) update(rt << 1 | 1, mid + 1, r, x, y, val);
  pushup(rt);
}
int query_min(int rt, int l, int r, int x) {
  if (l == r) {
    return tree[rt].minn;
  }
  int mid = (l + r) >> 1;
  pushdown(rt);
  if (x <= mid)
    return query_min(rt << 1, l, mid, x);
  else
    return query_min(rt << 1 | 1, mid + 1, r, x);
}
int query_max(int rt, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    return tree[rt].maxx;
  }
  int mid = (l + r) >> 1, res = 0;
  pushdown(rt);
  if (x <= mid) chkmax(res, query_max(rt << 1, l, mid, x, y));
  if (mid < y) chkmax(res, query_max(rt << 1 | 1, mid + 1, r, x, y));
  return res;
}
void Upd(int x, int y, int val) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    update(1, 1, n, id[top[x]], id[x], val);
    x = fa[top[x]];
  }
  if (dep[x] > dep[y]) swap(x, y);
  update(1, 1, n, id[x] + 1, id[y], val);
}
int Qmax(int x, int y) {
  int res = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    chkmax(res, query_max(1, 1, n, id[top[x]], id[x]));
    x = fa[top[x]];
  }
  if (dep[x] > dep[y]) swap(x, y);
  return max(res, query_max(1, 1, n, id[x] + 1, id[y]));
}
int main() {
  file();
  memset(lzt, 127, sizeof(lzt));
  read(n);
  read(m);
  for (int i = 1, u, v, w; i <= m; i++) {
    read(u);
    read(v);
    read(w);
    line[i] = (node){u, v, w, i};
  }
  sort(line + 1, line + m + 1);
  for (int i = (1); i <= (n); i++) f[i] = i;
  for (int i = (1); i <= (m); i++) {
    int u = getfa(line[i].u), v = getfa(line[i].v);
    if (u == v) continue;
    f[u] = v;
    line[i].used = 1;
    add(line[i].u, line[i].v, line[i].w, line[i].id);
    add(line[i].v, line[i].u, line[i].w, line[i].id);
  }
  dfs1(1, 0);
  dfs2(1, 1);
  build(1, 1, n);
  for (int i = (1); i <= (m); i++) {
    if (line[i].used) continue;
    ans[line[i].id] = Qmax(line[i].u, line[i].v) - 1;
    Upd(line[i].u, line[i].v, line[i].w);
  }
  for (int i = (2); i <= (n); i++) {
    ans[pre[i]] = query_min(1, 1, n, id[i]) - 1;
  }
  for (int i = (1); i <= (m); i++)
    printf("%d ", ans[i] == 2139062143 - 1 ? -1 : ans[i]);
  return 0;
}

```