#include <bits/stdc++.h>
using namespace std;
const int oo = 2139063143;
const int N = 1010000;
const int P = 1000000007;
const double eps = 1e-7;
template <typename T>
inline void sc(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
template <typename T>
inline void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
inline void pr(T x) {
  print(x), putchar('\n');
}
template <typename T1, typename T2>
void chkmax(T1 &A, T2 B) {
  A = A >= B ? A : B;
}
template <typename T1, typename T2>
void chkmin(T1 &A, T2 B) {
  A = A <= B ? A : B;
}
int n, m;
vector<int> G[N];
void add(int x, int y) { G[x].push_back(y), G[y].push_back(x); }
int dep[N], siz[N], son[N], faa[N];
void dfs0(int u, int fa) {
  dep[u] = dep[faa[u] = fa] + (siz[u] = 1);
  for (int v : G[u])
    if (v != fa) {
      dfs0(v, u);
      siz[u] += siz[v];
      if (siz[v] > siz[son[u]]) son[u] = v;
    }
}
int tf[N], dfn[N], ddd[N];
void dfs00(int u, int tf) {
  ::tf[u] = tf;
  ddd[dfn[u] = ++dfn[0]] = u;
  if (son[u]) dfs00(son[u], tf);
  for (int v : G[u])
    if (v != faa[u] && v != son[u]) dfs00(v, v);
}
int lca(int x, int y) {
  while (tf[x] != tf[y]) {
    if (dep[tf[x]] < dep[tf[y]]) swap(x, y);
    x = faa[tf[x]];
  }
  return dep[x] < dep[y] ? x : y;
}
int get(int x, int y) {
  int z = x;
  while (tf[x] != tf[y]) x = faa[z = tf[x]];
  return x == y ? z : son[y];
}
bool OP;
struct TREE {
  int w, d;
} t[N << 2];
void pushd(int p, int d) {
  if (OP)
    chkmin(t[p].w, d), chkmin(t[p].d, d);
  else
    chkmax(t[p].w, d), chkmax(t[p].d, d);
}
void push(int p) {
  if (OP) {
    if (t[p].d != oo) {
      int ls = p << 1, rs = ls | 1;
      pushd(ls, t[p].d), pushd(rs, t[p].d);
      t[p].d = oo;
    }
  } else {
    if (t[p].d) {
      int ls = p << 1, rs = ls | 1;
      pushd(ls, t[p].d), pushd(rs, t[p].d);
      t[p].d = 0;
    }
  }
}
void build(int p, int l, int r) {
  if (OP)
    t[p].w = t[p].d = oo;
  else
    t[p].w = t[p].d = 0;
  if (l == r) return;
  int mid = (l + r) >> 1, ls = p << 1, rs = ls | 1;
  build(ls, l, mid), build(rs, mid + 1, r);
}
void change0(int p, int l, int r, int L, int R, int d) {
  if (L <= l && r <= R) return pushd(p, d), void();
  int mid = (l + r) >> 1, ls = p << 1, rs = ls | 1;
  push(p);
  if (L <= mid) change0(ls, l, mid, L, R, d);
  if (R > mid) change0(rs, mid + 1, r, L, R, d);
  if (OP)
    t[p].w = min(t[ls].w, t[rs].w);
  else
    t[p].w = max(t[ls].w, t[rs].w);
}
int query0(int p, int l, int r, int L, int R) {
  if (L <= l && r <= R) return t[p].w;
  int mid = (l + r) >> 1, ls = p << 1, rs = ls | 1, ans = OP ? oo : 0;
  push(p);
  if (L <= mid) ans = query0(ls, l, mid, L, R);
  if (R > mid)
    OP ? chkmin(ans, query0(rs, mid + 1, r, L, R))
       : chkmax(ans, query0(rs, mid + 1, r, L, R));
  return ans;
}
void change(int x, int y, int d) {
  while (tf[x] != tf[y]) {
    change0(1, 1, n, dfn[tf[x]], dfn[x], d);
    x = faa[tf[x]];
  }
  change0(1, 1, n, dfn[y], dfn[x], d);
}
int query(int x, int y) {
  int ans = OP ? oo : 0;
  while (tf[x] != tf[y]) {
    if (OP)
      chkmin(ans, query0(1, 1, n, dfn[tf[x]], dfn[x]));
    else
      chkmax(ans, query0(1, 1, n, dfn[tf[x]], dfn[x]));
    x = faa[tf[x]];
  }
  if (OP)
    chkmin(ans, query0(1, 1, n, dfn[y], dfn[x]));
  else
    chkmax(ans, query0(1, 1, n, dfn[y], dfn[x]));
  return ans;
}
int fa[N];
int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
int merge(int x, int y) {
  x = getf(x), y = getf(y);
  if (x != y) return fa[x] = y, 1;
  return 0;
}
int x[N], y[N], w[N], id[N], ans[N];
bool in[N];
int main() {
  sc(n), sc(m);
  for (int i = 1; i <= m; i++) sc(x[i]), sc(y[i]), sc(w[i]), id[i] = i;
  sort(id + 1, id + m + 1, [](int A, int B) { return w[A] < w[B]; });
  int k = 0;
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int j = 1; j <= m; j++) {
    int i = id[j];
    if ((in[i] = merge(x[i], y[i]))) ++k, add(x[i], y[i]);
    if (k == n - 1) break;
  }
  dfs0(1, 0), dfs00(1, 1);
  for (int i = 1; i <= m; i++)
    if (dep[x[i]] < dep[y[i]]) swap(x[i], y[i]);
  build(1, 1, n);
  for (int i = 1; i <= m; i++)
    if (in[i]) {
      int x = ::x[i];
      change0(1, 1, n, dfn[x], dfn[x], w[i]);
    }
  for (int i = 1; i <= m; i++)
    if (!in[i]) {
      int x = ::x[i], y = ::y[i], z = lca(x, y);
      if (z != x && z != y)
        ans[i] = max(query(x, get(x, z)), query(y, get(y, z)));
      else
        ans[i] = query(x, get(x, y));
    }
  OP = 1;
  build(1, 1, n);
  for (int i = 1; i <= m; i++)
    if (!in[i]) {
      int x = ::x[i], y = ::y[i], z = lca(x, y);
      if (z != x && z != y)
        change(x, get(x, z), w[i]), change(y, get(y, z), w[i]);
      else
        change(x, get(x, y), w[i]);
    }
  for (int i = 1; i <= m; i++)
    if (in[i]) {
      int x = ::x[i];
      ans[i] = query0(1, 1, n, dfn[x], dfn[x]);
    }
  for (int i = 1; i <= m; i++) printf("%d ", ans[i] > 1e9 ? -1 : (ans[i] - 1));
  return 0;
}

### Prompt

Your task is to create 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 oo = 2139063143;
const int N = 1010000;
const int P = 1000000007;
const double eps = 1e-7;
template <typename T>
inline void sc(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
template <typename T>
inline void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
inline void pr(T x) {
  print(x), putchar('\n');
}
template <typename T1, typename T2>
void chkmax(T1 &A, T2 B) {
  A = A >= B ? A : B;
}
template <typename T1, typename T2>
void chkmin(T1 &A, T2 B) {
  A = A <= B ? A : B;
}
int n, m;
vector<int> G[N];
void add(int x, int y) { G[x].push_back(y), G[y].push_back(x); }
int dep[N], siz[N], son[N], faa[N];
void dfs0(int u, int fa) {
  dep[u] = dep[faa[u] = fa] + (siz[u] = 1);
  for (int v : G[u])
    if (v != fa) {
      dfs0(v, u);
      siz[u] += siz[v];
      if (siz[v] > siz[son[u]]) son[u] = v;
    }
}
int tf[N], dfn[N], ddd[N];
void dfs00(int u, int tf) {
  ::tf[u] = tf;
  ddd[dfn[u] = ++dfn[0]] = u;
  if (son[u]) dfs00(son[u], tf);
  for (int v : G[u])
    if (v != faa[u] && v != son[u]) dfs00(v, v);
}
int lca(int x, int y) {
  while (tf[x] != tf[y]) {
    if (dep[tf[x]] < dep[tf[y]]) swap(x, y);
    x = faa[tf[x]];
  }
  return dep[x] < dep[y] ? x : y;
}
int get(int x, int y) {
  int z = x;
  while (tf[x] != tf[y]) x = faa[z = tf[x]];
  return x == y ? z : son[y];
}
bool OP;
struct TREE {
  int w, d;
} t[N << 2];
void pushd(int p, int d) {
  if (OP)
    chkmin(t[p].w, d), chkmin(t[p].d, d);
  else
    chkmax(t[p].w, d), chkmax(t[p].d, d);
}
void push(int p) {
  if (OP) {
    if (t[p].d != oo) {
      int ls = p << 1, rs = ls | 1;
      pushd(ls, t[p].d), pushd(rs, t[p].d);
      t[p].d = oo;
    }
  } else {
    if (t[p].d) {
      int ls = p << 1, rs = ls | 1;
      pushd(ls, t[p].d), pushd(rs, t[p].d);
      t[p].d = 0;
    }
  }
}
void build(int p, int l, int r) {
  if (OP)
    t[p].w = t[p].d = oo;
  else
    t[p].w = t[p].d = 0;
  if (l == r) return;
  int mid = (l + r) >> 1, ls = p << 1, rs = ls | 1;
  build(ls, l, mid), build(rs, mid + 1, r);
}
void change0(int p, int l, int r, int L, int R, int d) {
  if (L <= l && r <= R) return pushd(p, d), void();
  int mid = (l + r) >> 1, ls = p << 1, rs = ls | 1;
  push(p);
  if (L <= mid) change0(ls, l, mid, L, R, d);
  if (R > mid) change0(rs, mid + 1, r, L, R, d);
  if (OP)
    t[p].w = min(t[ls].w, t[rs].w);
  else
    t[p].w = max(t[ls].w, t[rs].w);
}
int query0(int p, int l, int r, int L, int R) {
  if (L <= l && r <= R) return t[p].w;
  int mid = (l + r) >> 1, ls = p << 1, rs = ls | 1, ans = OP ? oo : 0;
  push(p);
  if (L <= mid) ans = query0(ls, l, mid, L, R);
  if (R > mid)
    OP ? chkmin(ans, query0(rs, mid + 1, r, L, R))
       : chkmax(ans, query0(rs, mid + 1, r, L, R));
  return ans;
}
void change(int x, int y, int d) {
  while (tf[x] != tf[y]) {
    change0(1, 1, n, dfn[tf[x]], dfn[x], d);
    x = faa[tf[x]];
  }
  change0(1, 1, n, dfn[y], dfn[x], d);
}
int query(int x, int y) {
  int ans = OP ? oo : 0;
  while (tf[x] != tf[y]) {
    if (OP)
      chkmin(ans, query0(1, 1, n, dfn[tf[x]], dfn[x]));
    else
      chkmax(ans, query0(1, 1, n, dfn[tf[x]], dfn[x]));
    x = faa[tf[x]];
  }
  if (OP)
    chkmin(ans, query0(1, 1, n, dfn[y], dfn[x]));
  else
    chkmax(ans, query0(1, 1, n, dfn[y], dfn[x]));
  return ans;
}
int fa[N];
int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
int merge(int x, int y) {
  x = getf(x), y = getf(y);
  if (x != y) return fa[x] = y, 1;
  return 0;
}
int x[N], y[N], w[N], id[N], ans[N];
bool in[N];
int main() {
  sc(n), sc(m);
  for (int i = 1; i <= m; i++) sc(x[i]), sc(y[i]), sc(w[i]), id[i] = i;
  sort(id + 1, id + m + 1, [](int A, int B) { return w[A] < w[B]; });
  int k = 0;
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int j = 1; j <= m; j++) {
    int i = id[j];
    if ((in[i] = merge(x[i], y[i]))) ++k, add(x[i], y[i]);
    if (k == n - 1) break;
  }
  dfs0(1, 0), dfs00(1, 1);
  for (int i = 1; i <= m; i++)
    if (dep[x[i]] < dep[y[i]]) swap(x[i], y[i]);
  build(1, 1, n);
  for (int i = 1; i <= m; i++)
    if (in[i]) {
      int x = ::x[i];
      change0(1, 1, n, dfn[x], dfn[x], w[i]);
    }
  for (int i = 1; i <= m; i++)
    if (!in[i]) {
      int x = ::x[i], y = ::y[i], z = lca(x, y);
      if (z != x && z != y)
        ans[i] = max(query(x, get(x, z)), query(y, get(y, z)));
      else
        ans[i] = query(x, get(x, y));
    }
  OP = 1;
  build(1, 1, n);
  for (int i = 1; i <= m; i++)
    if (!in[i]) {
      int x = ::x[i], y = ::y[i], z = lca(x, y);
      if (z != x && z != y)
        change(x, get(x, z), w[i]), change(y, get(y, z), w[i]);
      else
        change(x, get(x, y), w[i]);
    }
  for (int i = 1; i <= m; i++)
    if (in[i]) {
      int x = ::x[i];
      ans[i] = query0(1, 1, n, dfn[x], dfn[x]);
    }
  for (int i = 1; i <= m; i++) printf("%d ", ans[i] > 1e9 ? -1 : (ans[i] - 1));
  return 0;
}

```