#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T _min(T A, T B) {
  return A < B ? A : B;
}
template <typename T>
inline T _max(T A, T B) {
  return A > B ? A : B;
}
template <typename T>
inline char MIN(T& A, T B) {
  return A > B ? (A = B, 1) : 0;
}
template <typename T>
inline char MAX(T& A, T B) {
  return A < B ? (A = B, 1) : 0;
}
template <typename T>
inline void _swap(T& A, T& B) {
  A ^= B ^= A ^= B;
}
template <typename T>
inline T read(T& x) {
  x = 0;
  int f = 0;
  char c;
  while (!isdigit(c = getchar()))
    if (c == '-') f = 1;
  while (isdigit(c)) x = x * 10 + (c & 15), c = getchar();
  return f ? x = -x : x;
}
const int N = 2e5 + 7, INF = 0x3f3f3f3f;
struct thxorz {
  int to, nxt, w;
} G[N << 1];
int Head[N], tot = 1;
int ans[N], used[N];
int n, m;
inline void Addedge(int x, int y, int z) {
  G[++tot].to = y, G[tot].nxt = Head[x], Head[x] = tot, G[tot].w = z;
  G[++tot].to = x, G[tot].nxt = Head[y], Head[y] = tot, G[tot].w = z;
}
int fa[N], topfa[N], dep[N], son[N], sum[N], pos[N], A[N], cnt;
void dfs1(int x, int f) {
  fa[x] = f;
  dep[x] = dep[G[f ^ 1].to] + 1;
  sum[x] = 1;
  int tmp = -1;
  for (register int j = Head[x]; j; j = G[j].nxt)
    if ((j ^ 1) ^ f)
      dfs1(G[j].to, j), sum[x] += sum[G[j].to],
          MAX(tmp, sum[G[j].to]) && (son[x] = G[j].to);
}
void dfs2(int x, int topf) {
  topfa[x] = topf;
  pos[x] = cnt, A[cnt++] = G[fa[x]].w;
  if (!son[x]) return;
  dfs2(son[x], topf);
  for (register int j = Head[x]; j; j = G[j].nxt)
    if ((j ^ 1) ^ fa[x] && G[j].to ^ son[x]) dfs2(G[j].to, G[j].to);
}
struct segment_tree {
  int maxv[N << 2], cov[N << 2], tag[N << 2];
  segment_tree() {
    memset(cov, 0x3f, sizeof cov), memset(tag, 0x3f, sizeof tag);
  }
  inline void pushdown(int i) {
    if (tag[i] < INF) {
      MIN(tag[i << 1], tag[i]), MIN(tag[i << 1 | 1], tag[i]);
      MIN(cov[i << 1], tag[i]), MIN(cov[i << 1 | 1], tag[i]);
      tag[i] = INF;
    }
  }
  void Build(int i, int L, int R) {
    if (L == R) {
      maxv[i] = A[L];
      return;
    }
    Build(i << 1, L, L + R >> 1), Build(i << 1 | 1, (L + R >> 1) + 1, R);
    maxv[i] = _max(maxv[i << 1], maxv[i << 1 | 1]);
  }
  int UPD(int i, int L, int R, int ql, int qr, int val) {
    if (ql <= L && qr >= R) {
      MIN(cov[i], val);
      MIN(tag[i], val);
      return maxv[i];
    }
    int mid = L + R >> 1, ret = 0;
    pushdown(i);
    if (ql <= mid) MAX(ret, UPD(i << 1, L, mid, ql, qr, val));
    if (qr > mid) MAX(ret, UPD(i << 1 | 1, mid + 1, R, ql, qr, val));
    cov[i] = _min(cov[i << 1], cov[i << 1 | 1]);
    return ret;
  }
  int Query_cover(int i, int L, int R, int x) {
    if (L == R) return cov[i];
    int mid = L + R >> 1;
    pushdown(i);
    if (x <= mid) return Query_cover(i << 1, L, mid, x);
    return Query_cover(i << 1 | 1, mid + 1, R, x);
  }
} T;
struct wphorz {
  int u, v, w, id;
  inline bool operator<(const wphorz& A) const { return w < A.w; }
} e[N];
struct dsu {
  int anc[N];
  inline void Clear() {
    for (register int i = 1; i <= n; ++i) anc[i] = i;
  }
  inline int Find(int x) { return anc[x] == x ? x : anc[x] = Find(anc[x]); }
} S;
inline void Kruskal() {
  sort(e + 1, e + m + 1);
  S.Clear();
  for (register int i = 1; i <= m; ++i)
    if (S.Find(e[i].u) ^ S.Find(e[i].v))
      S.anc[S.anc[e[i].u]] = S.anc[e[i].v], Addedge(e[i].u, e[i].v, e[i].w),
      used[i] = 1;
}
inline int qry_and_upd(int x, int y, int val) {
  int ret = 0;
  while (topfa[x] ^ topfa[y]) {
    if (dep[topfa[x]] < dep[topfa[y]]) _swap(x, y);
    MAX(ret, T.UPD(1, 1, n - 1, pos[topfa[x]], pos[x], val));
    x = G[fa[topfa[x]] ^ 1].to;
  }
  if (dep[x] > dep[y]) _swap(x, y);
  return _max(ret, x == y ? 0 : T.UPD(1, 1, n - 1, pos[x] + 1, pos[y], val));
}
inline void process() {
  for (register int i = 1; i <= m; ++i)
    if (!used[i]) ans[e[i].id] = qry_and_upd(e[i].u, e[i].v, e[i].w) - 1;
  for (register int i = 1; i <= m; ++i)
    if (used[i]) {
      if (dep[e[i].u] < dep[e[i].v]) _swap(e[i].u, e[i].v);
      ans[e[i].id] = T.Query_cover(1, 1, n - 1, pos[e[i].u]);
      ans[e[i].id] < INF ? ans[e[i].id]-- : ans[e[i].id] = -1;
    }
  for (register int i = 1; i <= m; ++i) printf("%d ", ans[i]);
  puts("");
}
int main() {
  read(n), read(m);
  for (register int i = 1; i <= m; ++i)
    read(e[i].u), read(e[i].v), read(e[i].w), e[i].id = i;
  Kruskal();
  dfs1(1, 0), dfs2(1, 1);
  T.Build(1, 1, n - 1);
  process();
  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;
template <typename T>
inline T _min(T A, T B) {
  return A < B ? A : B;
}
template <typename T>
inline T _max(T A, T B) {
  return A > B ? A : B;
}
template <typename T>
inline char MIN(T& A, T B) {
  return A > B ? (A = B, 1) : 0;
}
template <typename T>
inline char MAX(T& A, T B) {
  return A < B ? (A = B, 1) : 0;
}
template <typename T>
inline void _swap(T& A, T& B) {
  A ^= B ^= A ^= B;
}
template <typename T>
inline T read(T& x) {
  x = 0;
  int f = 0;
  char c;
  while (!isdigit(c = getchar()))
    if (c == '-') f = 1;
  while (isdigit(c)) x = x * 10 + (c & 15), c = getchar();
  return f ? x = -x : x;
}
const int N = 2e5 + 7, INF = 0x3f3f3f3f;
struct thxorz {
  int to, nxt, w;
} G[N << 1];
int Head[N], tot = 1;
int ans[N], used[N];
int n, m;
inline void Addedge(int x, int y, int z) {
  G[++tot].to = y, G[tot].nxt = Head[x], Head[x] = tot, G[tot].w = z;
  G[++tot].to = x, G[tot].nxt = Head[y], Head[y] = tot, G[tot].w = z;
}
int fa[N], topfa[N], dep[N], son[N], sum[N], pos[N], A[N], cnt;
void dfs1(int x, int f) {
  fa[x] = f;
  dep[x] = dep[G[f ^ 1].to] + 1;
  sum[x] = 1;
  int tmp = -1;
  for (register int j = Head[x]; j; j = G[j].nxt)
    if ((j ^ 1) ^ f)
      dfs1(G[j].to, j), sum[x] += sum[G[j].to],
          MAX(tmp, sum[G[j].to]) && (son[x] = G[j].to);
}
void dfs2(int x, int topf) {
  topfa[x] = topf;
  pos[x] = cnt, A[cnt++] = G[fa[x]].w;
  if (!son[x]) return;
  dfs2(son[x], topf);
  for (register int j = Head[x]; j; j = G[j].nxt)
    if ((j ^ 1) ^ fa[x] && G[j].to ^ son[x]) dfs2(G[j].to, G[j].to);
}
struct segment_tree {
  int maxv[N << 2], cov[N << 2], tag[N << 2];
  segment_tree() {
    memset(cov, 0x3f, sizeof cov), memset(tag, 0x3f, sizeof tag);
  }
  inline void pushdown(int i) {
    if (tag[i] < INF) {
      MIN(tag[i << 1], tag[i]), MIN(tag[i << 1 | 1], tag[i]);
      MIN(cov[i << 1], tag[i]), MIN(cov[i << 1 | 1], tag[i]);
      tag[i] = INF;
    }
  }
  void Build(int i, int L, int R) {
    if (L == R) {
      maxv[i] = A[L];
      return;
    }
    Build(i << 1, L, L + R >> 1), Build(i << 1 | 1, (L + R >> 1) + 1, R);
    maxv[i] = _max(maxv[i << 1], maxv[i << 1 | 1]);
  }
  int UPD(int i, int L, int R, int ql, int qr, int val) {
    if (ql <= L && qr >= R) {
      MIN(cov[i], val);
      MIN(tag[i], val);
      return maxv[i];
    }
    int mid = L + R >> 1, ret = 0;
    pushdown(i);
    if (ql <= mid) MAX(ret, UPD(i << 1, L, mid, ql, qr, val));
    if (qr > mid) MAX(ret, UPD(i << 1 | 1, mid + 1, R, ql, qr, val));
    cov[i] = _min(cov[i << 1], cov[i << 1 | 1]);
    return ret;
  }
  int Query_cover(int i, int L, int R, int x) {
    if (L == R) return cov[i];
    int mid = L + R >> 1;
    pushdown(i);
    if (x <= mid) return Query_cover(i << 1, L, mid, x);
    return Query_cover(i << 1 | 1, mid + 1, R, x);
  }
} T;
struct wphorz {
  int u, v, w, id;
  inline bool operator<(const wphorz& A) const { return w < A.w; }
} e[N];
struct dsu {
  int anc[N];
  inline void Clear() {
    for (register int i = 1; i <= n; ++i) anc[i] = i;
  }
  inline int Find(int x) { return anc[x] == x ? x : anc[x] = Find(anc[x]); }
} S;
inline void Kruskal() {
  sort(e + 1, e + m + 1);
  S.Clear();
  for (register int i = 1; i <= m; ++i)
    if (S.Find(e[i].u) ^ S.Find(e[i].v))
      S.anc[S.anc[e[i].u]] = S.anc[e[i].v], Addedge(e[i].u, e[i].v, e[i].w),
      used[i] = 1;
}
inline int qry_and_upd(int x, int y, int val) {
  int ret = 0;
  while (topfa[x] ^ topfa[y]) {
    if (dep[topfa[x]] < dep[topfa[y]]) _swap(x, y);
    MAX(ret, T.UPD(1, 1, n - 1, pos[topfa[x]], pos[x], val));
    x = G[fa[topfa[x]] ^ 1].to;
  }
  if (dep[x] > dep[y]) _swap(x, y);
  return _max(ret, x == y ? 0 : T.UPD(1, 1, n - 1, pos[x] + 1, pos[y], val));
}
inline void process() {
  for (register int i = 1; i <= m; ++i)
    if (!used[i]) ans[e[i].id] = qry_and_upd(e[i].u, e[i].v, e[i].w) - 1;
  for (register int i = 1; i <= m; ++i)
    if (used[i]) {
      if (dep[e[i].u] < dep[e[i].v]) _swap(e[i].u, e[i].v);
      ans[e[i].id] = T.Query_cover(1, 1, n - 1, pos[e[i].u]);
      ans[e[i].id] < INF ? ans[e[i].id]-- : ans[e[i].id] = -1;
    }
  for (register int i = 1; i <= m; ++i) printf("%d ", ans[i]);
  puts("");
}
int main() {
  read(n), read(m);
  for (register int i = 1; i <= m; ++i)
    read(e[i].u), read(e[i].v), read(e[i].w), e[i].id = i;
  Kruskal();
  dfs1(1, 0), dfs2(1, 1);
  T.Build(1, 1, n - 1);
  process();
  return 0;
}

```