#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const int inf = 0x7fffffff;
struct sb {
  int l, r, mi, mx, lazy;
} tree[maxn * 5];
struct node {
  int x, y, val, id;
} edge[maxn * 4];
struct jjj {
  int x, y, val, id;
} edge2[maxn * 4];
struct node1 {
  int nxt, to, val;
} edge1[maxn * 3];
bool vis[maxn];
int n, m;
int fa[maxn];
int head[maxn], cnt;
void add(int x, int y, int v) {
  edge1[++cnt].nxt = head[x];
  edge1[cnt].to = y;
  edge1[cnt].val = v;
  head[x] = cnt;
}
int get(int x) {
  if (x == fa[x])
    return x;
  else
    return fa[x] = get(fa[x]);
}
bool cmp(node a, node b) { return a.val < b.val; }
int ans, tot;
void kruskal() {
  sort(edge + 1, edge + 1 + m, cmp);
  for (int i = 1; i <= m; i++) {
    int f1 = get(edge[i].x);
    int f2 = get(edge[i].y);
    if (f1 != f2) {
      fa[f1] = f2;
      ans += edge[i].val;
      vis[edge[i].id] = true;
      tot++;
      if (tot == n - 1) break;
    }
  }
}
int dep[maxn], faa[maxn], w[maxn], size[maxn], son[maxn];
void dfs1(int x, int f) {
  size[x] = 1;
  faa[x] = f;
  dep[x] = dep[f] + 1;
  int maxson = -1;
  for (int i = head[x]; i; i = edge1[i].nxt) {
    int v = edge1[i].to;
    if (v == faa[x]) continue;
    w[v] = edge1[i].val;
    dfs1(v, x);
    size[x] += size[v];
    if (size[v] > maxson) {
      maxson = size[v];
      son[x] = v;
    }
  }
}
int Time, top[maxn], id[maxn], va[maxn];
void dfs2(int x, int topf) {
  top[x] = topf;
  id[x] = ++Time;
  va[id[x]] = w[x];
  if (!son[x]) return;
  dfs2(son[x], topf);
  for (int i = head[x]; i; i = edge1[i].nxt) {
    int v = edge1[i].to;
    if (v == son[x] || v == faa[x]) continue;
    dfs2(v, v);
  }
}
void build(int now, int l, int r) {
  tree[now].l = l, tree[now].r = r, tree[now].lazy = inf;
  if (l == r) {
    tree[now].mi = inf;
    tree[now].mx = va[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(now << 1, l, mid);
  build(now << 1 | 1, mid + 1, r);
  tree[now].mi = min(tree[now << 1].mi, tree[now << 1 | 1].mi);
  tree[now].mx = max(tree[now << 1].mx, tree[now << 1 | 1].mx);
}
void push(int now, int v) {
  tree[now].mi = min(tree[now].mi, v);
  tree[now].lazy = min(tree[now].lazy, v);
}
void down(int now) {
  push(now << 1, tree[now].lazy);
  push(now << 1 | 1, tree[now].lazy);
  tree[now].lazy = inf;
}
void update(int now, int l, int r, int v) {
  if (tree[now].l >= l && tree[now].r <= r) {
    push(now, v);
    return;
  }
  if (tree[now].lazy != inf) down(now);
  int mid = (tree[now].l + tree[now].r) >> 1;
  if (l <= mid) update(now << 1, l, r, v);
  if (r > mid) update(now << 1 | 1, l, r, v);
  tree[now].mi = min(tree[now << 1].mi, tree[now << 1 | 1].mi);
  tree[now].mx = max(tree[now << 1].mx, tree[now << 1 | 1].mx);
}
int query1(int now, int l, int r) {
  if (tree[now].l >= l && tree[now].r <= r) return tree[now].mx;
  if (tree[now].lazy != inf) down(now);
  int mid = (tree[now].l + tree[now].r) >> 1;
  int orz = 0;
  if (l <= mid) orz = max(orz, query1(now << 1, l, r));
  if (r > mid) orz = max(orz, query1(now << 1 | 1, l, r));
  return orz;
}
int query2(int now, int l, int r) {
  if (tree[now].l >= l && tree[now].r <= r) return tree[now].mi;
  if (tree[now].lazy != inf) down(now);
  int mid = (tree[now].l + tree[now].r) >> 1;
  int orz = inf;
  if (l <= mid) orz = min(orz, query2(now << 1, l, r));
  if (r > mid) orz = min(orz, query2(now << 1 | 1, l, r));
  return orz;
}
int link1(int x, int y) {
  int maxx = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    maxx = max(query1(1, id[top[x]], id[x]), maxx);
    x = faa[top[x]];
  }
  if (dep[x] < dep[y]) swap(x, y);
  maxx = max(maxx, query1(1, id[y] + 1, id[x]));
  return maxx - 1;
}
int link2(int x, int y) {
  int minn = inf;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    minn = min(minn, query2(1, id[top[x]], id[x]));
    x = faa[top[x]];
  }
  if (dep[x] < dep[y]) swap(x, y);
  minn = min(minn, query2(1, id[y] + 1, id[x]));
  return (minn == inf) ? -1 : minn - 1;
}
void linkup(int x, int y, int v) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    update(1, id[top[x]], id[x], v);
    x = faa[top[x]];
  }
  if (dep[x] < dep[y]) swap(x, y);
  update(1, id[y] + 1, id[x], v);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].val);
    edge[i].id = i;
    edge2[i].x = edge[i].x;
    edge2[i].y = edge[i].y;
    edge2[i].val = edge[i].val;
    edge2[i].id = edge[i].id;
  }
  kruskal();
  for (int i = 1; i <= m; i++) {
    if (vis[i]) {
      add(edge2[i].x, edge2[i].y, edge2[i].val);
      add(edge2[i].y, edge2[i].x, edge2[i].val);
    }
  }
  dfs1(1, 0);
  dfs2(1, 1);
  build(1, 1, n);
  for (int i = 1; i <= m; i++)
    if (!vis[i]) linkup(edge2[i].x, edge2[i].y, edge2[i].val);
  for (int i = 1; i <= m; i++) {
    if (!vis[i])
      printf("%d\n", link1(edge2[i].x, edge2[i].y));
    else
      printf("%d ", link2(edge2[i].x, edge2[i].y));
  }
  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;
const int maxn = 2e5 + 7;
const int inf = 0x7fffffff;
struct sb {
  int l, r, mi, mx, lazy;
} tree[maxn * 5];
struct node {
  int x, y, val, id;
} edge[maxn * 4];
struct jjj {
  int x, y, val, id;
} edge2[maxn * 4];
struct node1 {
  int nxt, to, val;
} edge1[maxn * 3];
bool vis[maxn];
int n, m;
int fa[maxn];
int head[maxn], cnt;
void add(int x, int y, int v) {
  edge1[++cnt].nxt = head[x];
  edge1[cnt].to = y;
  edge1[cnt].val = v;
  head[x] = cnt;
}
int get(int x) {
  if (x == fa[x])
    return x;
  else
    return fa[x] = get(fa[x]);
}
bool cmp(node a, node b) { return a.val < b.val; }
int ans, tot;
void kruskal() {
  sort(edge + 1, edge + 1 + m, cmp);
  for (int i = 1; i <= m; i++) {
    int f1 = get(edge[i].x);
    int f2 = get(edge[i].y);
    if (f1 != f2) {
      fa[f1] = f2;
      ans += edge[i].val;
      vis[edge[i].id] = true;
      tot++;
      if (tot == n - 1) break;
    }
  }
}
int dep[maxn], faa[maxn], w[maxn], size[maxn], son[maxn];
void dfs1(int x, int f) {
  size[x] = 1;
  faa[x] = f;
  dep[x] = dep[f] + 1;
  int maxson = -1;
  for (int i = head[x]; i; i = edge1[i].nxt) {
    int v = edge1[i].to;
    if (v == faa[x]) continue;
    w[v] = edge1[i].val;
    dfs1(v, x);
    size[x] += size[v];
    if (size[v] > maxson) {
      maxson = size[v];
      son[x] = v;
    }
  }
}
int Time, top[maxn], id[maxn], va[maxn];
void dfs2(int x, int topf) {
  top[x] = topf;
  id[x] = ++Time;
  va[id[x]] = w[x];
  if (!son[x]) return;
  dfs2(son[x], topf);
  for (int i = head[x]; i; i = edge1[i].nxt) {
    int v = edge1[i].to;
    if (v == son[x] || v == faa[x]) continue;
    dfs2(v, v);
  }
}
void build(int now, int l, int r) {
  tree[now].l = l, tree[now].r = r, tree[now].lazy = inf;
  if (l == r) {
    tree[now].mi = inf;
    tree[now].mx = va[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(now << 1, l, mid);
  build(now << 1 | 1, mid + 1, r);
  tree[now].mi = min(tree[now << 1].mi, tree[now << 1 | 1].mi);
  tree[now].mx = max(tree[now << 1].mx, tree[now << 1 | 1].mx);
}
void push(int now, int v) {
  tree[now].mi = min(tree[now].mi, v);
  tree[now].lazy = min(tree[now].lazy, v);
}
void down(int now) {
  push(now << 1, tree[now].lazy);
  push(now << 1 | 1, tree[now].lazy);
  tree[now].lazy = inf;
}
void update(int now, int l, int r, int v) {
  if (tree[now].l >= l && tree[now].r <= r) {
    push(now, v);
    return;
  }
  if (tree[now].lazy != inf) down(now);
  int mid = (tree[now].l + tree[now].r) >> 1;
  if (l <= mid) update(now << 1, l, r, v);
  if (r > mid) update(now << 1 | 1, l, r, v);
  tree[now].mi = min(tree[now << 1].mi, tree[now << 1 | 1].mi);
  tree[now].mx = max(tree[now << 1].mx, tree[now << 1 | 1].mx);
}
int query1(int now, int l, int r) {
  if (tree[now].l >= l && tree[now].r <= r) return tree[now].mx;
  if (tree[now].lazy != inf) down(now);
  int mid = (tree[now].l + tree[now].r) >> 1;
  int orz = 0;
  if (l <= mid) orz = max(orz, query1(now << 1, l, r));
  if (r > mid) orz = max(orz, query1(now << 1 | 1, l, r));
  return orz;
}
int query2(int now, int l, int r) {
  if (tree[now].l >= l && tree[now].r <= r) return tree[now].mi;
  if (tree[now].lazy != inf) down(now);
  int mid = (tree[now].l + tree[now].r) >> 1;
  int orz = inf;
  if (l <= mid) orz = min(orz, query2(now << 1, l, r));
  if (r > mid) orz = min(orz, query2(now << 1 | 1, l, r));
  return orz;
}
int link1(int x, int y) {
  int maxx = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    maxx = max(query1(1, id[top[x]], id[x]), maxx);
    x = faa[top[x]];
  }
  if (dep[x] < dep[y]) swap(x, y);
  maxx = max(maxx, query1(1, id[y] + 1, id[x]));
  return maxx - 1;
}
int link2(int x, int y) {
  int minn = inf;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    minn = min(minn, query2(1, id[top[x]], id[x]));
    x = faa[top[x]];
  }
  if (dep[x] < dep[y]) swap(x, y);
  minn = min(minn, query2(1, id[y] + 1, id[x]));
  return (minn == inf) ? -1 : minn - 1;
}
void linkup(int x, int y, int v) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    update(1, id[top[x]], id[x], v);
    x = faa[top[x]];
  }
  if (dep[x] < dep[y]) swap(x, y);
  update(1, id[y] + 1, id[x], v);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].val);
    edge[i].id = i;
    edge2[i].x = edge[i].x;
    edge2[i].y = edge[i].y;
    edge2[i].val = edge[i].val;
    edge2[i].id = edge[i].id;
  }
  kruskal();
  for (int i = 1; i <= m; i++) {
    if (vis[i]) {
      add(edge2[i].x, edge2[i].y, edge2[i].val);
      add(edge2[i].y, edge2[i].x, edge2[i].val);
    }
  }
  dfs1(1, 0);
  dfs2(1, 1);
  build(1, 1, n);
  for (int i = 1; i <= m; i++)
    if (!vis[i]) linkup(edge2[i].x, edge2[i].y, edge2[i].val);
  for (int i = 1; i <= m; i++) {
    if (!vis[i])
      printf("%d\n", link1(edge2[i].x, edge2[i].y));
    else
      printf("%d ", link2(edge2[i].x, edge2[i].y));
  }
  return 0;
}

```