#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int dsu[MAXN];
int n, m;
int weight[MAXN];
map<pair<int, int>, int> ww;
vector<int> path[MAXN];
int Find(int u) {
  if (u == dsu[u]) return u;
  return dsu[u] = Find(dsu[u]);
}
void Union(int u, int v) {
  u = Find(u), v = Find(v);
  if (u != v) dsu[v] = u;
}
void traverse(int u, int pre) {
  if (pre) weight[u] = ww[{u, pre}];
  for (int v : path[u])
    if (v != pre) traverse(v, u);
}
int pos[MAXN], chain[MAXN], head[MAXN], tin[MAXN], tout[MAXN];
int up[MAXN][20], tin_num = 0, tout_num = 0, chain_num = 1, pos_num = 0;
int subtree[MAXN];
void hld(int u) {
  chain[u] = chain_num;
  if (!head[chain_num]) head[chain_num] = u;
  pos[u] = ++pos_num;
  int mx = -1, mxu = -1;
  for (int v : path[u])
    if (v != up[u][0] && mx < subtree[v]) {
      mx = subtree[v];
      mxu = v;
    }
  if (mxu != -1) hld(mxu);
  for (int v : path[u])
    if (v != up[u][0] && v != mxu) {
      chain_num++;
      hld(v);
    }
}
void dfs(int u, int pre) {
  up[u][0] = pre;
  for (int i = 1; i <= 18; i++) up[u][i] = up[up[u][i - 1]][i - 1];
  subtree[u] = 1;
  tin[u] = ++tin_num;
  for (int v : path[u])
    if (v != pre) dfs(v, u), subtree[u] += subtree[v];
  tout[u] = ++tout_num;
}
bool ancestor(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }
int lca(int u, int v) {
  if (ancestor(u, v)) return u;
  if (ancestor(v, u)) return v;
  for (int i = 18; i >= 0; i--)
    if (!ancestor(up[u][i], v)) u = up[u][i];
  return up[u][0];
}
int IT[MAXN * 4], lazy[MAXN * 4], base[MAXN];
void build_segtree_1(int id, int l, int r) {
  if (l == r) {
    IT[id] = weight[base[l]];
    lazy[id] = 2e9;
    return;
  }
  int mid = (l + r) / 2;
  build_segtree_1(id * 2, l, mid);
  build_segtree_1(id * 2 + 1, mid + 1, r);
  IT[id] = max(IT[id * 2], IT[id * 2 + 1]);
  lazy[id] = 2e9;
}
void build_segtree_2(int id, int l, int r) {
  if (l == r) {
    IT[id] = 2e9;
    lazy[id] = 2e9;
    return;
  }
  int mid = (l + r) / 2;
  build_segtree_2(id * 2, l, mid);
  build_segtree_2(id * 2 + 1, mid + 1, r);
  IT[id] = 2e9;
  lazy[id] = 2e9;
}
void push(int id, int l, int r) {
  IT[id] = min(IT[id], lazy[id]);
  if (l != r) {
    lazy[id * 2] = min(lazy[id * 2], lazy[id]);
    lazy[id * 2 + 1] = min(lazy[id * 2 + 1], lazy[id]);
  }
  lazy[id] = 2e9;
}
void upd_segtree(int u, int v, int val, int id = 1, int l = 1, int r = n) {
  push(id, l, r);
  if (r < u || l > v) return;
  if (l >= u && r <= v) {
    lazy[id] = val;
    push(id, l, r);
    return;
  }
  int mid = (l + r) / 2;
  upd_segtree(u, v, val, id * 2, l, mid);
  upd_segtree(u, v, val, id * 2 + 1, mid + 1, r);
  IT[id] = min(IT[id * 2], IT[id * 2 + 1]);
}
int query(int u, int v, bool mm, int id = 1, int l = 1, int r = n) {
  push(id, l, r);
  if (r < u || l > v) return (mm ? -2e9 : 2e9);
  if (l >= u && r <= v) return IT[id];
  int mid = (l + r) / 2;
  int left = query(u, v, mm, id * 2, l, mid);
  int right = query(u, v, mm, id * 2 + 1, mid + 1, r);
  if (mm)
    return max(left, right);
  else
    return min(left, right);
}
void upd_hld_(int u, int lca, int w) {
  while (chain[u] != chain[lca]) {
    upd_segtree(pos[head[chain[u]]], pos[u], w);
    u = up[head[chain[u]]][0];
  }
  if (pos[lca] + 1 <= pos[u]) upd_segtree(pos[lca] + 1, pos[u], w);
}
void upd_hld(int u, int v, int w) {
  int b = lca(u, v);
  upd_hld_(u, b, w);
  upd_hld_(v, b, w);
}
int query_hld_(bool mm, int u, int lca) {
  int mn = (mm ? -2e9 : 2e9);
  int ou = u;
  while (chain[u] != chain[lca]) {
    int val = query(pos[head[chain[u]]], pos[u], mm);
    if (mm)
      mn = max(mn, val);
    else
      mn = min(mn, val);
    u = up[head[chain[u]]][0];
  }
  if (pos[lca] + 1 <= pos[u]) {
    int val = query(pos[lca] + 1, pos[u], mm);
    if (mm)
      mn = max(mn, val);
    else
      mn = min(mn, val);
  }
  return mn;
}
int query_hld(bool mm, int u, int v) {
  int b = lca(u, v);
  int left = query_hld_(mm, u, b), right = query_hld_(mm, v, b);
  if (mm)
    return max(left, right);
  else
    return min(left, right);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  vector<pair<int, pair<int, int>>> edges, oedges;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) dsu[i] = i;
  for (int i = 1, u, v, w; i <= m; i++) {
    cin >> u >> v >> w;
    edges.push_back({w, {u, v}});
  }
  oedges = edges;
  sort(edges.begin(), edges.end());
  for (auto uvw : edges) {
    int w = uvw.first, u = uvw.second.first, v = uvw.second.second;
    if (Find(u) != Find(v)) {
      Union(u, v);
      ww[{u, v}] = ww[{v, u}] = w;
      path[u].push_back(v);
      path[v].push_back(u);
    }
  }
  traverse(1, 0);
  tout[0] = 1e9;
  dfs(1, 0);
  hld(1);
  for (int i = 1; i <= n; i++) base[pos[i]] = i;
  vector<int> ans(m + 1);
  build_segtree_2(1, 1, n);
  for (auto uvw : edges) {
    int u = uvw.second.first, v = uvw.second.second, w = uvw.first;
    if (ww[{u, v}]) continue;
    upd_hld(u, v, w);
  }
  for (int i = 1; i <= m; i++) {
    auto uvw = oedges[i - 1];
    int u = uvw.second.first, v = uvw.second.second;
    if (!ww[{u, v}]) continue;
    if (v == up[u][0]) swap(u, v);
    int res = query_hld(0, u, v);
    if (res == 2e9)
      ans[i] = -1;
    else
      ans[i] = res - 1;
  }
  build_segtree_1(1, 1, n);
  for (int i = 1; i <= m; i++) {
    auto uvw = oedges[i - 1];
    int u = uvw.second.first, v = uvw.second.second;
    if (ww[{u, v}]) continue;
    int res = query_hld(1, u, v);
    if (res == -2e9)
      ans[i] = -1;
    else
      ans[i] = res - 1;
  }
  for (int i = 1; i <= m; i++) cout << ans[i] << ' ';
  cout << '\n';
}

### Prompt

In cpp, your task is to solve 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 = 2e5 + 5;
int dsu[MAXN];
int n, m;
int weight[MAXN];
map<pair<int, int>, int> ww;
vector<int> path[MAXN];
int Find(int u) {
  if (u == dsu[u]) return u;
  return dsu[u] = Find(dsu[u]);
}
void Union(int u, int v) {
  u = Find(u), v = Find(v);
  if (u != v) dsu[v] = u;
}
void traverse(int u, int pre) {
  if (pre) weight[u] = ww[{u, pre}];
  for (int v : path[u])
    if (v != pre) traverse(v, u);
}
int pos[MAXN], chain[MAXN], head[MAXN], tin[MAXN], tout[MAXN];
int up[MAXN][20], tin_num = 0, tout_num = 0, chain_num = 1, pos_num = 0;
int subtree[MAXN];
void hld(int u) {
  chain[u] = chain_num;
  if (!head[chain_num]) head[chain_num] = u;
  pos[u] = ++pos_num;
  int mx = -1, mxu = -1;
  for (int v : path[u])
    if (v != up[u][0] && mx < subtree[v]) {
      mx = subtree[v];
      mxu = v;
    }
  if (mxu != -1) hld(mxu);
  for (int v : path[u])
    if (v != up[u][0] && v != mxu) {
      chain_num++;
      hld(v);
    }
}
void dfs(int u, int pre) {
  up[u][0] = pre;
  for (int i = 1; i <= 18; i++) up[u][i] = up[up[u][i - 1]][i - 1];
  subtree[u] = 1;
  tin[u] = ++tin_num;
  for (int v : path[u])
    if (v != pre) dfs(v, u), subtree[u] += subtree[v];
  tout[u] = ++tout_num;
}
bool ancestor(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }
int lca(int u, int v) {
  if (ancestor(u, v)) return u;
  if (ancestor(v, u)) return v;
  for (int i = 18; i >= 0; i--)
    if (!ancestor(up[u][i], v)) u = up[u][i];
  return up[u][0];
}
int IT[MAXN * 4], lazy[MAXN * 4], base[MAXN];
void build_segtree_1(int id, int l, int r) {
  if (l == r) {
    IT[id] = weight[base[l]];
    lazy[id] = 2e9;
    return;
  }
  int mid = (l + r) / 2;
  build_segtree_1(id * 2, l, mid);
  build_segtree_1(id * 2 + 1, mid + 1, r);
  IT[id] = max(IT[id * 2], IT[id * 2 + 1]);
  lazy[id] = 2e9;
}
void build_segtree_2(int id, int l, int r) {
  if (l == r) {
    IT[id] = 2e9;
    lazy[id] = 2e9;
    return;
  }
  int mid = (l + r) / 2;
  build_segtree_2(id * 2, l, mid);
  build_segtree_2(id * 2 + 1, mid + 1, r);
  IT[id] = 2e9;
  lazy[id] = 2e9;
}
void push(int id, int l, int r) {
  IT[id] = min(IT[id], lazy[id]);
  if (l != r) {
    lazy[id * 2] = min(lazy[id * 2], lazy[id]);
    lazy[id * 2 + 1] = min(lazy[id * 2 + 1], lazy[id]);
  }
  lazy[id] = 2e9;
}
void upd_segtree(int u, int v, int val, int id = 1, int l = 1, int r = n) {
  push(id, l, r);
  if (r < u || l > v) return;
  if (l >= u && r <= v) {
    lazy[id] = val;
    push(id, l, r);
    return;
  }
  int mid = (l + r) / 2;
  upd_segtree(u, v, val, id * 2, l, mid);
  upd_segtree(u, v, val, id * 2 + 1, mid + 1, r);
  IT[id] = min(IT[id * 2], IT[id * 2 + 1]);
}
int query(int u, int v, bool mm, int id = 1, int l = 1, int r = n) {
  push(id, l, r);
  if (r < u || l > v) return (mm ? -2e9 : 2e9);
  if (l >= u && r <= v) return IT[id];
  int mid = (l + r) / 2;
  int left = query(u, v, mm, id * 2, l, mid);
  int right = query(u, v, mm, id * 2 + 1, mid + 1, r);
  if (mm)
    return max(left, right);
  else
    return min(left, right);
}
void upd_hld_(int u, int lca, int w) {
  while (chain[u] != chain[lca]) {
    upd_segtree(pos[head[chain[u]]], pos[u], w);
    u = up[head[chain[u]]][0];
  }
  if (pos[lca] + 1 <= pos[u]) upd_segtree(pos[lca] + 1, pos[u], w);
}
void upd_hld(int u, int v, int w) {
  int b = lca(u, v);
  upd_hld_(u, b, w);
  upd_hld_(v, b, w);
}
int query_hld_(bool mm, int u, int lca) {
  int mn = (mm ? -2e9 : 2e9);
  int ou = u;
  while (chain[u] != chain[lca]) {
    int val = query(pos[head[chain[u]]], pos[u], mm);
    if (mm)
      mn = max(mn, val);
    else
      mn = min(mn, val);
    u = up[head[chain[u]]][0];
  }
  if (pos[lca] + 1 <= pos[u]) {
    int val = query(pos[lca] + 1, pos[u], mm);
    if (mm)
      mn = max(mn, val);
    else
      mn = min(mn, val);
  }
  return mn;
}
int query_hld(bool mm, int u, int v) {
  int b = lca(u, v);
  int left = query_hld_(mm, u, b), right = query_hld_(mm, v, b);
  if (mm)
    return max(left, right);
  else
    return min(left, right);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  vector<pair<int, pair<int, int>>> edges, oedges;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) dsu[i] = i;
  for (int i = 1, u, v, w; i <= m; i++) {
    cin >> u >> v >> w;
    edges.push_back({w, {u, v}});
  }
  oedges = edges;
  sort(edges.begin(), edges.end());
  for (auto uvw : edges) {
    int w = uvw.first, u = uvw.second.first, v = uvw.second.second;
    if (Find(u) != Find(v)) {
      Union(u, v);
      ww[{u, v}] = ww[{v, u}] = w;
      path[u].push_back(v);
      path[v].push_back(u);
    }
  }
  traverse(1, 0);
  tout[0] = 1e9;
  dfs(1, 0);
  hld(1);
  for (int i = 1; i <= n; i++) base[pos[i]] = i;
  vector<int> ans(m + 1);
  build_segtree_2(1, 1, n);
  for (auto uvw : edges) {
    int u = uvw.second.first, v = uvw.second.second, w = uvw.first;
    if (ww[{u, v}]) continue;
    upd_hld(u, v, w);
  }
  for (int i = 1; i <= m; i++) {
    auto uvw = oedges[i - 1];
    int u = uvw.second.first, v = uvw.second.second;
    if (!ww[{u, v}]) continue;
    if (v == up[u][0]) swap(u, v);
    int res = query_hld(0, u, v);
    if (res == 2e9)
      ans[i] = -1;
    else
      ans[i] = res - 1;
  }
  build_segtree_1(1, 1, n);
  for (int i = 1; i <= m; i++) {
    auto uvw = oedges[i - 1];
    int u = uvw.second.first, v = uvw.second.second;
    if (ww[{u, v}]) continue;
    int res = query_hld(1, u, v);
    if (res == -2e9)
      ans[i] = -1;
    else
      ans[i] = res - 1;
  }
  for (int i = 1; i <= m; i++) cout << ans[i] << ' ';
  cout << '\n';
}

```