#include <bits/stdc++.h>
using namespace std;
long long n, m, first[200020], nxt[200020 << 1], tot = 0, cnt = 0;
long long head[200020], nxxt[200020 << 1], grand[200020][20], gw[200020][20],
    tot2 = 0, root, maxn = 19, depth[200020];
long long x, y, z, father[200020], gans[200020 << 1][20], tree[200020 << 1],
    ans[200020 << 1];
struct csx {
  long long u, v, w, id;
} edge[200020 << 1], line[200020 << 1];
void add(long long from, long long to, long long cost, long long d) {
  tot++;
  edge[tot].id = d;
  nxt[tot] = first[from];
  first[from] = tot;
  edge[tot].u = from;
  edge[tot].v = to;
  edge[tot].w = cost;
  return;
}
void add2(long long from, long long to, long long cost) {
  tot2++;
  nxxt[tot2] = head[from];
  head[from] = tot2;
  line[tot2].u = from;
  line[tot2].v = to;
  line[tot2].w = cost;
  return;
}
long long getfather(long long x) {
  long long r = x;
  while (father[r] != r) r = father[r];
  long long y = x;
  while (y != r) {
    long long k = father[y];
    father[y] = r;
    y = k;
  }
  return r;
}
bool cmp(const csx a, const csx b) {
  if (a.w < b.w)
    return true;
  else
    return false;
}
void build(long long point) {
  for (long long i = 1; i <= maxn; i++)
    grand[point][i] = grand[grand[point][i - 1]][i - 1],
    gw[point][i] = max(gw[point][i - 1], gw[grand[point][i - 1]][i - 1]);
  for (long long j = head[point]; j != -1; j = nxxt[j]) {
    if (grand[point][0] != line[j].v) {
      grand[line[j].v][0] = point;
      gw[line[j].v][0] = line[j].w;
      depth[line[j].v] = depth[point] + 1;
      build(line[j].v);
    }
  }
  return;
}
void Kruskal() {
  sort(edge + 1, edge + 1 + tot, cmp);
  for (long long i = 1; i <= tot; i++) {
    long long fu = getfather(edge[i].u), fv = getfather(edge[i].v);
    if (fu != fv) {
      cnt++;
      father[fu] = fv;
      add2(edge[i].u, edge[i].v, edge[i].w);
      add2(edge[i].v, edge[i].u, edge[i].w);
      tree[edge[i].id] = 1;
    }
  }
  root = getfather(1);
  maxn = 19;
  depth[root] = 1;
  build(root);
  return;
}
long long LCA(long long a, long long b) {
  long long rtans = -INT_MAX;
  if (depth[a] < depth[b]) swap(a, b);
  for (long long i = maxn; i >= 0; i--)
    if (depth[a] > depth[b] && depth[grand[a][i]] >= depth[b]) {
      rtans = max(rtans, gw[a][i]);
      a = grand[a][i];
    }
  for (long long i = maxn; i >= 0; i--) {
    if (grand[a][i] != grand[b][i]) {
      rtans = max(rtans, gw[a][i]);
      rtans = max(rtans, gw[b][i]);
      a = grand[a][i];
      b = grand[b][i];
    }
  }
  if (a != b) {
    rtans = max(rtans, gw[a][0]);
    rtans = max(rtans, gw[b][0]);
  }
  return rtans;
}
void LCA2(long long a, long long b, long long w) {
  if (depth[a] < depth[b]) swap(a, b);
  for (long long i = maxn; i >= 0; i--)
    if (depth[a] > depth[b] && depth[grand[a][i]] >= depth[b]) {
      gans[a][i] = min(gans[a][i], w);
      a = grand[a][i];
    }
  for (long long i = maxn; i >= 0; i--) {
    if (grand[a][i] != grand[b][i]) {
      gans[a][i] = min(gans[a][i], w);
      gans[b][i] = min(gans[b][i], w);
      a = grand[a][i];
      b = grand[b][i];
    }
  }
  if (a != b) {
    gans[a][0] = min(gans[a][0], w);
    gans[b][0] = min(gans[b][0], w);
  }
  return;
}
void shrink() {
  for (long long j = maxn; j >= 0; j--) {
    for (long long i = 1; i <= n; i++) {
      gans[i][j - 1] = min(gans[i][j - 1], gans[i][j]);
      gans[grand[i][j - 1]][j - 1] =
          min(gans[grand[i][j - 1]][j - 1], gans[i][j]);
    }
  }
  return;
}
long long solve(long long a, long long b) {
  if (depth[a] < depth[b]) swap(a, b);
  if (gans[a][0] == gans[0][0])
    return 0;
  else
    return gans[a][0];
}
int main() {
  for (long long i = 0; i < 200020; i++) first[i] = head[i] = -1;
  memset(ans, 0x3f, sizeof(ans));
  memset(gans, 0x3f, sizeof(gans));
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) father[i] = i;
  for (long long i = 1; i <= m; i++)
    cin >> x >> y >> z, add(x, y, z, i), add(y, x, z, i);
  Kruskal();
  for (long long i = 1; i <= tot; i++)
    if (!tree[edge[i].id]) LCA2(edge[i].u, edge[i].v, edge[i].w);
  shrink();
  for (long long i = 1; i <= tot; i++)
    if (!tree[edge[i].id])
      ans[edge[i].id] = min(LCA(edge[i].u, edge[i].v) - 1, ans[edge[i].id]);
    else if (tree[edge[i].id])
      ans[edge[i].id] = min(solve(edge[i].u, edge[i].v) - 1, ans[edge[i].id]);
  for (long long i = 1; i <= m; i++) cout << ans[i] << " ";
  return 0;
}

### Prompt

Develop a solution in Cpp 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;
long long n, m, first[200020], nxt[200020 << 1], tot = 0, cnt = 0;
long long head[200020], nxxt[200020 << 1], grand[200020][20], gw[200020][20],
    tot2 = 0, root, maxn = 19, depth[200020];
long long x, y, z, father[200020], gans[200020 << 1][20], tree[200020 << 1],
    ans[200020 << 1];
struct csx {
  long long u, v, w, id;
} edge[200020 << 1], line[200020 << 1];
void add(long long from, long long to, long long cost, long long d) {
  tot++;
  edge[tot].id = d;
  nxt[tot] = first[from];
  first[from] = tot;
  edge[tot].u = from;
  edge[tot].v = to;
  edge[tot].w = cost;
  return;
}
void add2(long long from, long long to, long long cost) {
  tot2++;
  nxxt[tot2] = head[from];
  head[from] = tot2;
  line[tot2].u = from;
  line[tot2].v = to;
  line[tot2].w = cost;
  return;
}
long long getfather(long long x) {
  long long r = x;
  while (father[r] != r) r = father[r];
  long long y = x;
  while (y != r) {
    long long k = father[y];
    father[y] = r;
    y = k;
  }
  return r;
}
bool cmp(const csx a, const csx b) {
  if (a.w < b.w)
    return true;
  else
    return false;
}
void build(long long point) {
  for (long long i = 1; i <= maxn; i++)
    grand[point][i] = grand[grand[point][i - 1]][i - 1],
    gw[point][i] = max(gw[point][i - 1], gw[grand[point][i - 1]][i - 1]);
  for (long long j = head[point]; j != -1; j = nxxt[j]) {
    if (grand[point][0] != line[j].v) {
      grand[line[j].v][0] = point;
      gw[line[j].v][0] = line[j].w;
      depth[line[j].v] = depth[point] + 1;
      build(line[j].v);
    }
  }
  return;
}
void Kruskal() {
  sort(edge + 1, edge + 1 + tot, cmp);
  for (long long i = 1; i <= tot; i++) {
    long long fu = getfather(edge[i].u), fv = getfather(edge[i].v);
    if (fu != fv) {
      cnt++;
      father[fu] = fv;
      add2(edge[i].u, edge[i].v, edge[i].w);
      add2(edge[i].v, edge[i].u, edge[i].w);
      tree[edge[i].id] = 1;
    }
  }
  root = getfather(1);
  maxn = 19;
  depth[root] = 1;
  build(root);
  return;
}
long long LCA(long long a, long long b) {
  long long rtans = -INT_MAX;
  if (depth[a] < depth[b]) swap(a, b);
  for (long long i = maxn; i >= 0; i--)
    if (depth[a] > depth[b] && depth[grand[a][i]] >= depth[b]) {
      rtans = max(rtans, gw[a][i]);
      a = grand[a][i];
    }
  for (long long i = maxn; i >= 0; i--) {
    if (grand[a][i] != grand[b][i]) {
      rtans = max(rtans, gw[a][i]);
      rtans = max(rtans, gw[b][i]);
      a = grand[a][i];
      b = grand[b][i];
    }
  }
  if (a != b) {
    rtans = max(rtans, gw[a][0]);
    rtans = max(rtans, gw[b][0]);
  }
  return rtans;
}
void LCA2(long long a, long long b, long long w) {
  if (depth[a] < depth[b]) swap(a, b);
  for (long long i = maxn; i >= 0; i--)
    if (depth[a] > depth[b] && depth[grand[a][i]] >= depth[b]) {
      gans[a][i] = min(gans[a][i], w);
      a = grand[a][i];
    }
  for (long long i = maxn; i >= 0; i--) {
    if (grand[a][i] != grand[b][i]) {
      gans[a][i] = min(gans[a][i], w);
      gans[b][i] = min(gans[b][i], w);
      a = grand[a][i];
      b = grand[b][i];
    }
  }
  if (a != b) {
    gans[a][0] = min(gans[a][0], w);
    gans[b][0] = min(gans[b][0], w);
  }
  return;
}
void shrink() {
  for (long long j = maxn; j >= 0; j--) {
    for (long long i = 1; i <= n; i++) {
      gans[i][j - 1] = min(gans[i][j - 1], gans[i][j]);
      gans[grand[i][j - 1]][j - 1] =
          min(gans[grand[i][j - 1]][j - 1], gans[i][j]);
    }
  }
  return;
}
long long solve(long long a, long long b) {
  if (depth[a] < depth[b]) swap(a, b);
  if (gans[a][0] == gans[0][0])
    return 0;
  else
    return gans[a][0];
}
int main() {
  for (long long i = 0; i < 200020; i++) first[i] = head[i] = -1;
  memset(ans, 0x3f, sizeof(ans));
  memset(gans, 0x3f, sizeof(gans));
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) father[i] = i;
  for (long long i = 1; i <= m; i++)
    cin >> x >> y >> z, add(x, y, z, i), add(y, x, z, i);
  Kruskal();
  for (long long i = 1; i <= tot; i++)
    if (!tree[edge[i].id]) LCA2(edge[i].u, edge[i].v, edge[i].w);
  shrink();
  for (long long i = 1; i <= tot; i++)
    if (!tree[edge[i].id])
      ans[edge[i].id] = min(LCA(edge[i].u, edge[i].v) - 1, ans[edge[i].id]);
    else if (tree[edge[i].id])
      ans[edge[i].id] = min(solve(edge[i].u, edge[i].v) - 1, ans[edge[i].id]);
  for (long long i = 1; i <= m; i++) cout << ans[i] << " ";
  return 0;
}

```