#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010, inf = 2e9;
struct zs {
  int too, pre, dis;
} e[maxn * 3];
struct tjm {
  int max, min, delta;
} tree[maxn * 4];
struct poi {
  int x, y, z, pos;
} a[maxn];
int n, m, x, y, z, tot, cnt;
int fa[maxn], fq[maxn], d[maxn], son[maxn], size[maxn], last[maxn], len[maxn],
    mx[maxn], w[maxn], ans[maxn], top[maxn];
bool ty[maxn];
void read(int &k) {
  int f = 1;
  k = 0;
  char c = getchar();
  while (c < '0' || c > '9') c == '-' && (f = -1), c = getchar();
  while (c <= '9' && c >= '0') k = k * 10 + c - '0', c = getchar();
  k *= f;
}
bool cmp(poi a, poi b) { return a.z < b.z; }
int gf(int x) { return fa[x] == x ? x : fa[x] = gf(fa[x]); }
void add(int x, int y, int z) {
  e[++tot].too = y;
  e[tot].dis = z;
  e[tot].pre = last[x];
  last[x] = tot;
}
void dfs1(int x) {
  size[x] = 1;
  for (int i = last[x]; i; i = e[i].pre) {
    int too = e[i].too;
    if (too == fq[x]) continue;
    fq[too] = x;
    d[too] = d[x] + 1;
    len[too] = e[i].dis;
    dfs1(too);
    if (size[too] > size[son[x]]) son[x] = too;
    size[x] += size[too];
  }
}
void dfs2(int x, int tp) {
  w[x] = ++cnt;
  mx[cnt] = len[x];
  top[x] = tp;
  if (son[x]) dfs2(son[x], tp);
  for (int i = last[x]; i; i = e[i].pre)
    if (e[i].too != fq[x] && e[i].too != son[x]) dfs2(e[i].too, e[i].too);
}
void pushup(int x, int l, int r) {
  if (l == r) return;
  tree[x].min = min(tree[x << 1].min, tree[x << 1 | 1].min);
  tree[x].max = max(tree[x << 1].max, tree[x << 1 | 1].max);
}
void pushdown(int x, int l, int r) {
  if (l == r) return;
  if (tree[x].delta != inf) {
    tree[x << 1].min = min(tree[x << 1].min, tree[x].delta);
    tree[x << 1 | 1].min = min(tree[x << 1 | 1].min, tree[x].delta);
    tree[x << 1].delta = min(tree[x << 1].delta, tree[x].delta);
    tree[x << 1 | 1].delta = min(tree[x << 1 | 1].delta, tree[x].delta);
    tree[x].delta = inf;
  }
}
void build(int x, int l, int r) {
  if (l == r) {
    tree[x].min = tree[x].delta = inf;
    tree[x].max = mx[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  tree[x].delta = inf;
  pushup(x, l, r);
}
void update(int x, int l, int r, int cl, int cr, int delta) {
  if (cl <= l && r <= cr)
    tree[x].min = min(tree[x].min, delta),
    tree[x].delta = min(tree[x].delta, delta);
  else {
    pushdown(x, l, r);
    int mid = (l + r) >> 1;
    if (cl <= mid) update(x << 1, l, mid, cl, cr, delta);
    if (cr > mid) update(x << 1 | 1, mid + 1, r, cl, cr, delta);
    pushup(x, l, r);
  }
}
int query(int x, int l, int r, int cx) {
  if (l == cx && cx == r) return tree[x].min;
  pushdown(x, l, r);
  int mid = (l + r) >> 1, ret = inf;
  if (cx <= mid) ret = query(x << 1, l, mid, cx);
  if (cx > mid) ret = query(x << 1 | 1, mid + 1, r, cx);
  return ret;
}
int query2(int x, int l, int r, int cl, int cr) {
  if (cl <= l && r <= cr) return tree[x].max;
  pushdown(x, l, r);
  int mid = (l + r) >> 1, ret = 0;
  if (cl <= mid) ret = query2(x << 1, l, mid, cl, cr);
  if (cr > mid) ret = max(ret, query2(x << 1 | 1, mid + 1, r, cl, cr));
  return ret;
}
int work(int x, int y, int delta) {
  int f1 = top[x], f2 = top[y], ans = 0;
  while (f1 != f2) {
    if (d[f1] < d[f2]) swap(x, y), swap(f1, f2);
    if (delta != -1)
      update(1, 1, n, w[f1], w[x], delta);
    else
      ans = max(ans, query2(1, 1, n, w[f1], w[x]));
    x = fq[f1];
    f1 = top[x];
  }
  if (x == y) return ans;
  if (d[x] < d[y]) swap(x, y);
  if (delta != -1)
    update(1, 1, n, w[son[y]], w[x], delta);
  else
    return max(ans, query2(1, 1, n, w[son[y]], w[x]));
  return ans;
}
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= m; i++)
    read(a[i].x), read(a[i].y), read(a[i].z), a[i].pos = i;
  sort(a + 1, a + 1 + m, cmp);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int fx = gf(a[i].x), fy = gf(a[i].y);
    if (fx == fy) continue;
    add(a[i].x, a[i].y, a[i].z);
    add(a[i].y, a[i].x, a[i].z);
    fa[fx] = fy;
    ty[i] = 1;
  }
  dfs1(1);
  dfs2(1, 1);
  build(1, 1, n);
  for (int i = 1; i <= m; i++)
    if (!ty[i]) {
      ans[a[i].pos] = work(a[i].x, a[i].y, -1) - 1;
      work(a[i].x, a[i].y, a[i].z);
    }
  for (int i = 1; i <= m; i++)
    if (ty[i])
      ans[a[i].pos] =
          query(1, 1, n, w[d[a[i].x] > d[a[i].y] ? a[i].x : a[i].y]) - 1;
  for (int i = 1; i <= m; i++) printf("%d ", ans[i] == inf - 1 ? -1 : ans[i]);
  return 0;
}

### Prompt

Generate 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;
const int maxn = 200010, inf = 2e9;
struct zs {
  int too, pre, dis;
} e[maxn * 3];
struct tjm {
  int max, min, delta;
} tree[maxn * 4];
struct poi {
  int x, y, z, pos;
} a[maxn];
int n, m, x, y, z, tot, cnt;
int fa[maxn], fq[maxn], d[maxn], son[maxn], size[maxn], last[maxn], len[maxn],
    mx[maxn], w[maxn], ans[maxn], top[maxn];
bool ty[maxn];
void read(int &k) {
  int f = 1;
  k = 0;
  char c = getchar();
  while (c < '0' || c > '9') c == '-' && (f = -1), c = getchar();
  while (c <= '9' && c >= '0') k = k * 10 + c - '0', c = getchar();
  k *= f;
}
bool cmp(poi a, poi b) { return a.z < b.z; }
int gf(int x) { return fa[x] == x ? x : fa[x] = gf(fa[x]); }
void add(int x, int y, int z) {
  e[++tot].too = y;
  e[tot].dis = z;
  e[tot].pre = last[x];
  last[x] = tot;
}
void dfs1(int x) {
  size[x] = 1;
  for (int i = last[x]; i; i = e[i].pre) {
    int too = e[i].too;
    if (too == fq[x]) continue;
    fq[too] = x;
    d[too] = d[x] + 1;
    len[too] = e[i].dis;
    dfs1(too);
    if (size[too] > size[son[x]]) son[x] = too;
    size[x] += size[too];
  }
}
void dfs2(int x, int tp) {
  w[x] = ++cnt;
  mx[cnt] = len[x];
  top[x] = tp;
  if (son[x]) dfs2(son[x], tp);
  for (int i = last[x]; i; i = e[i].pre)
    if (e[i].too != fq[x] && e[i].too != son[x]) dfs2(e[i].too, e[i].too);
}
void pushup(int x, int l, int r) {
  if (l == r) return;
  tree[x].min = min(tree[x << 1].min, tree[x << 1 | 1].min);
  tree[x].max = max(tree[x << 1].max, tree[x << 1 | 1].max);
}
void pushdown(int x, int l, int r) {
  if (l == r) return;
  if (tree[x].delta != inf) {
    tree[x << 1].min = min(tree[x << 1].min, tree[x].delta);
    tree[x << 1 | 1].min = min(tree[x << 1 | 1].min, tree[x].delta);
    tree[x << 1].delta = min(tree[x << 1].delta, tree[x].delta);
    tree[x << 1 | 1].delta = min(tree[x << 1 | 1].delta, tree[x].delta);
    tree[x].delta = inf;
  }
}
void build(int x, int l, int r) {
  if (l == r) {
    tree[x].min = tree[x].delta = inf;
    tree[x].max = mx[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  tree[x].delta = inf;
  pushup(x, l, r);
}
void update(int x, int l, int r, int cl, int cr, int delta) {
  if (cl <= l && r <= cr)
    tree[x].min = min(tree[x].min, delta),
    tree[x].delta = min(tree[x].delta, delta);
  else {
    pushdown(x, l, r);
    int mid = (l + r) >> 1;
    if (cl <= mid) update(x << 1, l, mid, cl, cr, delta);
    if (cr > mid) update(x << 1 | 1, mid + 1, r, cl, cr, delta);
    pushup(x, l, r);
  }
}
int query(int x, int l, int r, int cx) {
  if (l == cx && cx == r) return tree[x].min;
  pushdown(x, l, r);
  int mid = (l + r) >> 1, ret = inf;
  if (cx <= mid) ret = query(x << 1, l, mid, cx);
  if (cx > mid) ret = query(x << 1 | 1, mid + 1, r, cx);
  return ret;
}
int query2(int x, int l, int r, int cl, int cr) {
  if (cl <= l && r <= cr) return tree[x].max;
  pushdown(x, l, r);
  int mid = (l + r) >> 1, ret = 0;
  if (cl <= mid) ret = query2(x << 1, l, mid, cl, cr);
  if (cr > mid) ret = max(ret, query2(x << 1 | 1, mid + 1, r, cl, cr));
  return ret;
}
int work(int x, int y, int delta) {
  int f1 = top[x], f2 = top[y], ans = 0;
  while (f1 != f2) {
    if (d[f1] < d[f2]) swap(x, y), swap(f1, f2);
    if (delta != -1)
      update(1, 1, n, w[f1], w[x], delta);
    else
      ans = max(ans, query2(1, 1, n, w[f1], w[x]));
    x = fq[f1];
    f1 = top[x];
  }
  if (x == y) return ans;
  if (d[x] < d[y]) swap(x, y);
  if (delta != -1)
    update(1, 1, n, w[son[y]], w[x], delta);
  else
    return max(ans, query2(1, 1, n, w[son[y]], w[x]));
  return ans;
}
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= m; i++)
    read(a[i].x), read(a[i].y), read(a[i].z), a[i].pos = i;
  sort(a + 1, a + 1 + m, cmp);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int fx = gf(a[i].x), fy = gf(a[i].y);
    if (fx == fy) continue;
    add(a[i].x, a[i].y, a[i].z);
    add(a[i].y, a[i].x, a[i].z);
    fa[fx] = fy;
    ty[i] = 1;
  }
  dfs1(1);
  dfs2(1, 1);
  build(1, 1, n);
  for (int i = 1; i <= m; i++)
    if (!ty[i]) {
      ans[a[i].pos] = work(a[i].x, a[i].y, -1) - 1;
      work(a[i].x, a[i].y, a[i].z);
    }
  for (int i = 1; i <= m; i++)
    if (ty[i])
      ans[a[i].pos] =
          query(1, 1, n, w[d[a[i].x] > d[a[i].y] ? a[i].x : a[i].y]) - 1;
  for (int i = 1; i <= m; i++) printf("%d ", ans[i] == inf - 1 ? -1 : ans[i]);
  return 0;
}

```