#include <bits/stdc++.h>
using namespace std;
const int N = 200055;
const int M = 200055;
const int inf = 0x3f3f3f3f;
vector<int> NT, to[N], W[N];
struct edge {
  int u, v, w, id;
} E[M];
bool cmp(const edge &A, const edge &B) { return A.w < B.w; }
bool cmpid(const edge &A, const edge &B) { return A.id < B.id; }
int n, m, xx;
int fa[N], son[N], seq[N], dfn[N], pl[N], size[N], dep[N], top[N], inc = 0,
                                                                   w[N], ans[N];
struct node {
  int ls, rs;
  int mx, flag;
} tr[2 * N];
int root, tail = 0;
int getfa(int x) {
  if (pl[x] == x)
    return pl[x];
  else
    return pl[x] = getfa(pl[x]);
}
void dfs(int u, int f) {
  dep[u] = dep[f] + 1;
  fa[u] = f;
  size[u] = 1;
  int sz = to[u].size();
  for (int i = 0; i < sz; i++) {
    int v = to[u][i];
    if (v == f) continue;
    w[v] = W[u][i];
    dfs(v, u);
    size[u] += size[v];
    if (size[v] > size[son[u]]) son[u] = v;
  }
}
void gettp(int u, int f, int tp) {
  top[u] = tp;
  inc++;
  seq[inc] = u;
  dfn[u] = inc;
  if (son[u]) gettp(son[u], u, tp);
  int sz = to[u].size();
  for (int i = 0; i < sz; i++) {
    int v = to[u][i];
    if (v == f || v == son[u]) continue;
    gettp(v, u, v);
  }
}
void update(int nd) {
  int ls = tr[nd].ls;
  int rs = tr[nd].rs;
  tr[nd].mx = max(tr[ls].mx, tr[rs].mx);
}
void build(int &nd, int lf, int rg) {
  nd = ++tail;
  tr[nd].flag = inf;
  if (lf == rg) {
    tr[nd].mx = E[w[seq[lf]]].w;
    return;
  }
  int mid = (lf + rg) >> 1;
  build(tr[nd].ls, lf, mid);
  build(tr[nd].rs, mid + 1, rg);
  update(nd);
}
int modify(int nd, int lf, int rg, int L, int R, int val) {
  if (lf > rg) return -inf;
  if (L <= lf && rg <= R) {
    tr[nd].flag = min(val, tr[nd].flag);
    return tr[nd].mx;
  }
  int mid = (lf + rg) >> 1;
  int ret = -inf;
  if (L <= mid) ret = max(ret, modify(tr[nd].ls, lf, mid, L, R, val));
  if (R > mid) ret = max(ret, modify(tr[nd].rs, mid + 1, rg, L, R, val));
  return ret;
}
int modify(int u, int v, int ww) {
  int ret = -inf;
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]]) swap(u, v);
    ret = max(ret, modify(root, 1, n, dfn[top[u]], dfn[u], ww));
    u = fa[top[u]];
  }
  if (dep[u] < dep[v]) swap(u, v);
  ret = max(ret, modify(root, 1, n, dfn[son[v]], dfn[u], ww));
  return ret;
}
int query(int nd, int lf, int rg, int pos) {
  if (lf == rg) return tr[nd].flag;
  int mid = (lf + rg) >> 1;
  int ret = tr[nd].flag;
  if (pos <= mid)
    ret = min(ret, query(tr[nd].ls, lf, mid, pos));
  else
    ret = min(ret, query(tr[nd].rs, mid + 1, rg, pos));
  return ret;
}
void getans(int u, int f) {
  int sz = to[u].size();
  for (int i = 0; i < sz; i++) {
    int v = to[u][i];
    if (v == f) continue;
    ans[w[v]] = query(root, 1, n, dfn[v]) - 1;
    getans(v, u);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w);
    E[i].id = i;
  }
  sort(E + 1, E + m + 1, cmp);
  for (int i = 1; i <= n; i++) pl[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = E[i].u;
    int v = E[i].v;
    int fx = getfa(u);
    int fy = getfa(v);
    if (fx != fy) {
      pl[fx] = fy;
      to[u].push_back(v);
      to[v].push_back(u);
      W[u].push_back(E[i].id);
      W[v].push_back(E[i].id);
    } else {
      NT.push_back(E[i].id);
    }
  }
  sort(E + 1, E + m + 1, cmpid);
  dfs(1, 1);
  gettp(1, 1, 1);
  build(root, 1, n);
  int sz = NT.size();
  for (int i = 0; i < sz; i++) {
    int tmp = NT[i];
    int u = E[tmp].u;
    int v = E[tmp].v;
    ans[E[tmp].id] = modify(u, v, E[tmp].w) - 1;
  }
  getans(1, 1);
  for (int i = 1; i <= m; i++)
    if (ans[i] >= inf - 1)
      printf("-1 ");
    else
      printf("%d ", ans[i]);
  return 0;
}

### Prompt

Create a solution in CPP for 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 N = 200055;
const int M = 200055;
const int inf = 0x3f3f3f3f;
vector<int> NT, to[N], W[N];
struct edge {
  int u, v, w, id;
} E[M];
bool cmp(const edge &A, const edge &B) { return A.w < B.w; }
bool cmpid(const edge &A, const edge &B) { return A.id < B.id; }
int n, m, xx;
int fa[N], son[N], seq[N], dfn[N], pl[N], size[N], dep[N], top[N], inc = 0,
                                                                   w[N], ans[N];
struct node {
  int ls, rs;
  int mx, flag;
} tr[2 * N];
int root, tail = 0;
int getfa(int x) {
  if (pl[x] == x)
    return pl[x];
  else
    return pl[x] = getfa(pl[x]);
}
void dfs(int u, int f) {
  dep[u] = dep[f] + 1;
  fa[u] = f;
  size[u] = 1;
  int sz = to[u].size();
  for (int i = 0; i < sz; i++) {
    int v = to[u][i];
    if (v == f) continue;
    w[v] = W[u][i];
    dfs(v, u);
    size[u] += size[v];
    if (size[v] > size[son[u]]) son[u] = v;
  }
}
void gettp(int u, int f, int tp) {
  top[u] = tp;
  inc++;
  seq[inc] = u;
  dfn[u] = inc;
  if (son[u]) gettp(son[u], u, tp);
  int sz = to[u].size();
  for (int i = 0; i < sz; i++) {
    int v = to[u][i];
    if (v == f || v == son[u]) continue;
    gettp(v, u, v);
  }
}
void update(int nd) {
  int ls = tr[nd].ls;
  int rs = tr[nd].rs;
  tr[nd].mx = max(tr[ls].mx, tr[rs].mx);
}
void build(int &nd, int lf, int rg) {
  nd = ++tail;
  tr[nd].flag = inf;
  if (lf == rg) {
    tr[nd].mx = E[w[seq[lf]]].w;
    return;
  }
  int mid = (lf + rg) >> 1;
  build(tr[nd].ls, lf, mid);
  build(tr[nd].rs, mid + 1, rg);
  update(nd);
}
int modify(int nd, int lf, int rg, int L, int R, int val) {
  if (lf > rg) return -inf;
  if (L <= lf && rg <= R) {
    tr[nd].flag = min(val, tr[nd].flag);
    return tr[nd].mx;
  }
  int mid = (lf + rg) >> 1;
  int ret = -inf;
  if (L <= mid) ret = max(ret, modify(tr[nd].ls, lf, mid, L, R, val));
  if (R > mid) ret = max(ret, modify(tr[nd].rs, mid + 1, rg, L, R, val));
  return ret;
}
int modify(int u, int v, int ww) {
  int ret = -inf;
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]]) swap(u, v);
    ret = max(ret, modify(root, 1, n, dfn[top[u]], dfn[u], ww));
    u = fa[top[u]];
  }
  if (dep[u] < dep[v]) swap(u, v);
  ret = max(ret, modify(root, 1, n, dfn[son[v]], dfn[u], ww));
  return ret;
}
int query(int nd, int lf, int rg, int pos) {
  if (lf == rg) return tr[nd].flag;
  int mid = (lf + rg) >> 1;
  int ret = tr[nd].flag;
  if (pos <= mid)
    ret = min(ret, query(tr[nd].ls, lf, mid, pos));
  else
    ret = min(ret, query(tr[nd].rs, mid + 1, rg, pos));
  return ret;
}
void getans(int u, int f) {
  int sz = to[u].size();
  for (int i = 0; i < sz; i++) {
    int v = to[u][i];
    if (v == f) continue;
    ans[w[v]] = query(root, 1, n, dfn[v]) - 1;
    getans(v, u);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w);
    E[i].id = i;
  }
  sort(E + 1, E + m + 1, cmp);
  for (int i = 1; i <= n; i++) pl[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = E[i].u;
    int v = E[i].v;
    int fx = getfa(u);
    int fy = getfa(v);
    if (fx != fy) {
      pl[fx] = fy;
      to[u].push_back(v);
      to[v].push_back(u);
      W[u].push_back(E[i].id);
      W[v].push_back(E[i].id);
    } else {
      NT.push_back(E[i].id);
    }
  }
  sort(E + 1, E + m + 1, cmpid);
  dfs(1, 1);
  gettp(1, 1, 1);
  build(root, 1, n);
  int sz = NT.size();
  for (int i = 0; i < sz; i++) {
    int tmp = NT[i];
    int u = E[tmp].u;
    int v = E[tmp].v;
    ans[E[tmp].id] = modify(u, v, E[tmp].w) - 1;
  }
  getans(1, 1);
  for (int i = 1; i <= m; i++)
    if (ans[i] >= inf - 1)
      printf("-1 ");
    else
      printf("%d ", ans[i]);
  return 0;
}

```