#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void print(int x) {
  if (x < 0) x *= -1, putchar('-');
  if (x >= 10) print(x / 10);
  putchar((x % 10) | 48);
}
const int N = 2e5 + 50;
int n, m;
int ans[N], len[N];
pair<int, int> edge[N];
bool leaf[N];
namespace SegmentTree {
vector<pair<int, int> > e[N];
int maxn[N * 4], ano[N * 4], tag[N * 4], siz[N], son[N], fa[N], dfn[N], top[N];
int val[N], dep[N], id;
void add(int u, int v, int w) {
  e[u].push_back(make_pair(v, w));
  e[v].push_back(make_pair(u, w));
}
void pushup(int p) {
  maxn[p] = max(maxn[p << 1], maxn[p << 1 | 1]);
  ano[p] = min(ano[p << 1], ano[p << 1 | 1]);
}
void pushdown(int p) {
  if (tag[p] != -1) {
    if (tag[p] < ano[p << 1]) ano[p << 1] = tag[p];
    if (tag[p] < ano[p << 1 | 1]) ano[p << 1 | 1] = tag[p];
    tag[p << 1] = (tag[p << 1] == -1 ? tag[p] : min(tag[p << 1], tag[p]));
    tag[p << 1 | 1] =
        (tag[p << 1 | 1] == -1 ? tag[p] : min(tag[p << 1 | 1], tag[p]));
    tag[p] = -1;
  }
}
void change(int p, int l, int r, int L, int R, int k) {
  if (l >= L && r <= R) {
    if (k < ano[p]) ano[p] = k;
    tag[p] = (tag[p] == -1 ? k : min(tag[p], k));
    return;
  }
  pushdown(p);
  int mid = l + r >> 1;
  if (L <= mid) change(p << 1, l, mid, L, R, k);
  if (R > mid) change(p << 1 | 1, mid + 1, r, L, R, k);
  pushup(p);
}
int xxx(int p, int l, int r, int L, int R) {
  if (l >= L && r <= R) {
    return ano[p];
  }
  pushdown(p);
  pushup(p);
  int mid = l + r >> 1;
  int res = 0x3f3f3f3f;
  if (L <= mid) res = min(res, xxx(p << 1, l, mid, L, R));
  if (R > mid) res = min(res, xxx(p << 1 | 1, mid + 1, r, L, R));
  return res;
}
void build(int p, int l, int r) {
  tag[p] = -1;
  if (l == r) {
    maxn[p] = val[l];
    ano[p] = 0x3f3f3f3f;
    return;
  }
  int mid = l + r >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  pushup(p);
}
int query1(int p, int l, int r, int L, int R) {
  if (l >= L && r <= R) return maxn[p];
  int mid = l + r >> 1;
  int res = 0;
  if (L <= mid) res = max(res, query1(p << 1, l, mid, L, R));
  if (R > mid) res = max(res, query1(p << 1 | 1, mid + 1, r, L, R));
  return res;
}
int query2(int x, int y) {
  int res = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    res = max(res, query1(1, 1, id, dfn[top[x]], dfn[x]));
    x = fa[top[x]];
  }
  if (x == y) return res;
  if (dep[x] > dep[y]) swap(x, y);
  res = max(res, query1(1, 1, id, dfn[x] + 1, dfn[y]));
  return res;
}
void dfs1(int x, int f) {
  fa[x] = f;
  dep[x] = dep[fa[x]] + 1, siz[x] = 1;
  for (auto i : e[x]) {
    int v = i.first;
    if (v == f) continue;
    dfs1(v, x);
    siz[x] += siz[v];
    if (siz[v] > siz[son[x]]) son[x] = v;
  }
}
void dfs2(int x, int topf, int worth) {
  dfn[x] = ++id;
  val[id] = worth;
  top[x] = topf;
  if (!son[x]) return;
  for (auto i : e[x]) {
    int v = i.first;
    if (v == son[x]) {
      dfs2(v, topf, i.second);
    }
  }
  for (auto i : e[x]) {
    int v = i.first;
    if (v == fa[x] || v == son[x]) continue;
    dfs2(v, v, i.second);
  }
}
void UPD(int x, int y, int k) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    change(1, 1, id, dfn[top[x]], dfn[x], k);
    x = fa[top[x]];
  }
  if (x == y) return;
  if (dep[x] > dep[y]) swap(x, y);
  change(1, 1, id, dfn[x] + 1, dfn[y], k);
}
int another(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  return xxx(1, 1, id, dfn[x], dfn[x]);
}
void solve() {
  memset(ano, 0x3f, sizeof ano);
  dfs1(1, 0);
  dfs2(1, 1, 0);
  build(1, 1, id);
}
}  // namespace SegmentTree
namespace MST {
int cnt;
int fa[N];
struct edge {
  int u, v, w, id;
} e[N];
void add(int u, int v, int w, int id) { e[++cnt] = edge{u, v, w, id}; }
bool cmp(edge x, edge y) { return x.w < y.w; }
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void kruskal() {
  int t = 1;
  for (int i = 1; i <= m; i++) {
    int fx = find(e[i].u);
    int fy = find(e[i].v);
    if (fx == fy) continue;
    ++t;
    SegmentTree::add(e[i].u, e[i].v, e[i].w);
    leaf[e[i].id] = 1;
    fa[fx] = fy;
    if (t == n) break;
  }
}
void solve() {
  for (int i = 1; i <= n; i++) fa[i] = i;
  sort(e + 1, e + 1 + m, cmp);
  kruskal();
}
}  // namespace MST
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read(), w = read();
    edge[i].first = u, edge[i].second = v;
    len[i] = w;
    MST::add(u, v, w, i);
  }
  MST::solve();
  SegmentTree::solve();
  for (int i = 1; i <= m; i++)
    if (!leaf[i]) SegmentTree::UPD(edge[i].first, edge[i].second, len[i]);
  for (int i = 1; i <= m; i++) {
    if (ans[i] == -1) continue;
    if (leaf[i]) {
      ans[i] = SegmentTree::another(edge[i].first, edge[i].second) - 1;
      if (ans[i] > 0x3f3f3f15) ans[i] = -1;
      continue;
    }
    int maxn = SegmentTree::query2(edge[i].first, edge[i].second);
    ans[i] = maxn - 1;
  }
  for (int i = 1; i <= m; i++) print(ans[i]), putchar(' ');
  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;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void print(int x) {
  if (x < 0) x *= -1, putchar('-');
  if (x >= 10) print(x / 10);
  putchar((x % 10) | 48);
}
const int N = 2e5 + 50;
int n, m;
int ans[N], len[N];
pair<int, int> edge[N];
bool leaf[N];
namespace SegmentTree {
vector<pair<int, int> > e[N];
int maxn[N * 4], ano[N * 4], tag[N * 4], siz[N], son[N], fa[N], dfn[N], top[N];
int val[N], dep[N], id;
void add(int u, int v, int w) {
  e[u].push_back(make_pair(v, w));
  e[v].push_back(make_pair(u, w));
}
void pushup(int p) {
  maxn[p] = max(maxn[p << 1], maxn[p << 1 | 1]);
  ano[p] = min(ano[p << 1], ano[p << 1 | 1]);
}
void pushdown(int p) {
  if (tag[p] != -1) {
    if (tag[p] < ano[p << 1]) ano[p << 1] = tag[p];
    if (tag[p] < ano[p << 1 | 1]) ano[p << 1 | 1] = tag[p];
    tag[p << 1] = (tag[p << 1] == -1 ? tag[p] : min(tag[p << 1], tag[p]));
    tag[p << 1 | 1] =
        (tag[p << 1 | 1] == -1 ? tag[p] : min(tag[p << 1 | 1], tag[p]));
    tag[p] = -1;
  }
}
void change(int p, int l, int r, int L, int R, int k) {
  if (l >= L && r <= R) {
    if (k < ano[p]) ano[p] = k;
    tag[p] = (tag[p] == -1 ? k : min(tag[p], k));
    return;
  }
  pushdown(p);
  int mid = l + r >> 1;
  if (L <= mid) change(p << 1, l, mid, L, R, k);
  if (R > mid) change(p << 1 | 1, mid + 1, r, L, R, k);
  pushup(p);
}
int xxx(int p, int l, int r, int L, int R) {
  if (l >= L && r <= R) {
    return ano[p];
  }
  pushdown(p);
  pushup(p);
  int mid = l + r >> 1;
  int res = 0x3f3f3f3f;
  if (L <= mid) res = min(res, xxx(p << 1, l, mid, L, R));
  if (R > mid) res = min(res, xxx(p << 1 | 1, mid + 1, r, L, R));
  return res;
}
void build(int p, int l, int r) {
  tag[p] = -1;
  if (l == r) {
    maxn[p] = val[l];
    ano[p] = 0x3f3f3f3f;
    return;
  }
  int mid = l + r >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  pushup(p);
}
int query1(int p, int l, int r, int L, int R) {
  if (l >= L && r <= R) return maxn[p];
  int mid = l + r >> 1;
  int res = 0;
  if (L <= mid) res = max(res, query1(p << 1, l, mid, L, R));
  if (R > mid) res = max(res, query1(p << 1 | 1, mid + 1, r, L, R));
  return res;
}
int query2(int x, int y) {
  int res = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    res = max(res, query1(1, 1, id, dfn[top[x]], dfn[x]));
    x = fa[top[x]];
  }
  if (x == y) return res;
  if (dep[x] > dep[y]) swap(x, y);
  res = max(res, query1(1, 1, id, dfn[x] + 1, dfn[y]));
  return res;
}
void dfs1(int x, int f) {
  fa[x] = f;
  dep[x] = dep[fa[x]] + 1, siz[x] = 1;
  for (auto i : e[x]) {
    int v = i.first;
    if (v == f) continue;
    dfs1(v, x);
    siz[x] += siz[v];
    if (siz[v] > siz[son[x]]) son[x] = v;
  }
}
void dfs2(int x, int topf, int worth) {
  dfn[x] = ++id;
  val[id] = worth;
  top[x] = topf;
  if (!son[x]) return;
  for (auto i : e[x]) {
    int v = i.first;
    if (v == son[x]) {
      dfs2(v, topf, i.second);
    }
  }
  for (auto i : e[x]) {
    int v = i.first;
    if (v == fa[x] || v == son[x]) continue;
    dfs2(v, v, i.second);
  }
}
void UPD(int x, int y, int k) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    change(1, 1, id, dfn[top[x]], dfn[x], k);
    x = fa[top[x]];
  }
  if (x == y) return;
  if (dep[x] > dep[y]) swap(x, y);
  change(1, 1, id, dfn[x] + 1, dfn[y], k);
}
int another(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  return xxx(1, 1, id, dfn[x], dfn[x]);
}
void solve() {
  memset(ano, 0x3f, sizeof ano);
  dfs1(1, 0);
  dfs2(1, 1, 0);
  build(1, 1, id);
}
}  // namespace SegmentTree
namespace MST {
int cnt;
int fa[N];
struct edge {
  int u, v, w, id;
} e[N];
void add(int u, int v, int w, int id) { e[++cnt] = edge{u, v, w, id}; }
bool cmp(edge x, edge y) { return x.w < y.w; }
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void kruskal() {
  int t = 1;
  for (int i = 1; i <= m; i++) {
    int fx = find(e[i].u);
    int fy = find(e[i].v);
    if (fx == fy) continue;
    ++t;
    SegmentTree::add(e[i].u, e[i].v, e[i].w);
    leaf[e[i].id] = 1;
    fa[fx] = fy;
    if (t == n) break;
  }
}
void solve() {
  for (int i = 1; i <= n; i++) fa[i] = i;
  sort(e + 1, e + 1 + m, cmp);
  kruskal();
}
}  // namespace MST
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read(), w = read();
    edge[i].first = u, edge[i].second = v;
    len[i] = w;
    MST::add(u, v, w, i);
  }
  MST::solve();
  SegmentTree::solve();
  for (int i = 1; i <= m; i++)
    if (!leaf[i]) SegmentTree::UPD(edge[i].first, edge[i].second, len[i]);
  for (int i = 1; i <= m; i++) {
    if (ans[i] == -1) continue;
    if (leaf[i]) {
      ans[i] = SegmentTree::another(edge[i].first, edge[i].second) - 1;
      if (ans[i] > 0x3f3f3f15) ans[i] = -1;
      continue;
    }
    int maxn = SegmentTree::query2(edge[i].first, edge[i].second);
    ans[i] = maxn - 1;
  }
  for (int i = 1; i <= m; i++) print(ans[i]), putchar(' ');
  return 0;
}

```