#include <bits/stdc++.h>
using namespace std;
bool endline = false;
template <class T>
istream& operator>>(istream& inp, vector<T>& v) {
  for (auto& it : v) inp >> it;
  return inp;
}
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
  for (auto& it : v) out << it << (endline ? "\n" : " ");
  return out;
}
template <class T, class U>
istream& operator>>(istream& inp, pair<T, U>& v) {
  inp >> v.first >> v.second;
  return inp;
}
template <class T, class U>
ostream& operator<<(ostream& out, pair<T, U>& v) {
  out << v.first << ' ' << v.second;
  return out;
}
void debug() { cout << endl; }
template <typename H, typename... T>
void debug(H a, T... b) {
  cout << a << ' ';
  debug(b...);
}
void FAST_IO(string filein = "", string fileout = "", string fileerr = "") {
  if (fopen(filein.c_str(), "r")) {
    freopen(filein.c_str(), "r", stdin);
    freopen(fileout.c_str(), "w", stdout);
  }
  cin.tie(0), cout.tie(0)->sync_with_stdio(0);
}
void Hollwo_Pelw();
signed main() {
  FAST_IO(".inp", ".out");
  int testcases = 1;
  for (int test = 1; test <= testcases; test++) {
    Hollwo_Pelw();
  }
  return 0;
}
const int allmod[3] = {(int)1e9 + 7, 998244353, (int)1e9 + 9};
const int mod = allmod[0];
const int MAXN = 2e5 + 5;
const int inf = 2e9;
const long long linf = 1e18;
const long double eps = 1e-9;
int n, m;
struct disjoint_set_union {
  int par[MAXN];
  disjoint_set_union() {
    for (int i = 1; i < MAXN; i++) par[i] = i;
  }
  int find(int u) { return par[u] = (par[u] == u ? u : find(par[u])); }
  bool merge(int u, int v) {
    if ((u = find(u)) == (v = find(v))) return 0;
    par[u] = v;
    return 1;
  }
} dsu;
struct edge {
  int u, v, w, id;
  bool operator<(const edge& oth) const { return w < oth.w; }
};
vector<pair<int, int> > adj[MAXN];
int par[MAXN][19], mn[MAXN][19];
int second[MAXN], E[MAXN], timer;
int sz[MAXN], nxt[MAXN];
void get_sz(int u, int p, int w) {
  par[u][0] = p, mn[u][0] = w;
  for (int i = 1, v; i <= 18; i++) {
    v = par[u][i - 1];
    par[u][i] = par[v][i - 1];
    mn[u][i] = max(mn[v][i - 1], mn[u][i - 1]);
  }
  sz[u] = 1;
  nxt[u] = u;
  for (auto& v : adj[u]) {
    if (v.first == p) swap(adj[u].back(), v);
    if (v.first == p) continue;
    get_sz(v.first, u, v.second);
    sz[u] += sz[v.first];
    if (sz[v.first] > sz[adj[u][0].first]) swap(adj[u][0], v);
  }
  if (adj[u].back().first == p) adj[u].pop_back();
}
void dfs_hld(int u) {
  second[u] = ++timer;
  for (auto& v : adj[u]) {
    if (v == adj[u][0]) nxt[v.first] = nxt[u];
    dfs_hld(v.first);
  }
  E[u] = timer;
}
void init_hld() {
  get_sz(1, 1, -inf);
  dfs_hld(1);
}
bool is_ancestor(int u, int v) {
  return second[u] <= second[v] && E[v] <= E[u];
}
int lca(int u, int v) {
  if (is_ancestor(u, v)) return u;
  if (is_ancestor(v, u)) return v;
  for (int i = 18; i >= 0; i--) {
    if (!is_ancestor(par[u][i], v)) u = par[u][i];
  }
  return par[u][0];
}
int lift_val(int u, int p) {
  if (u == p) return -inf;
  int res = -inf;
  for (int i = 18; i >= 0; i--) {
    if (!is_ancestor(par[u][i], p)) {
      res = max(res, mn[u][i]);
      u = par[u][i];
    }
  }
  return max(res, mn[u][0]);
}
int lift(int u, int p) {
  for (int i = 18; i >= 0; i--) {
    if (!is_ancestor(par[u][i], p)) {
      u = par[u][i];
    }
  }
  return u;
}
struct segment_tree {
  int st[MAXN << 2], lz[MAXN << 2];
  segment_tree() {
    for (int i = 1; i < (MAXN << 2); i++) st[i] = lz[i] = inf;
  }
  void apply(int id, int v) {
    lz[id] = min(lz[id], v);
    st[id] = min(st[id], v);
  }
  void push(int id) {
    apply(id << 1, lz[id]);
    apply(id << 1 | 1, lz[id]);
  }
  void upd(int l, int r, int v, int id = 1, int tl = 1, int tr = n) {
    if (l > r || l > tr || tl > r) return;
    if (l <= tl && tr <= r) return apply(id, v);
    int tm = tl + tr >> 1;
    push(id);
    upd(l, r, v, id << 1, tl, tm), upd(l, r, v, id << 1 | 1, tm + 1, tr);
    st[id] = min(st[id << 1], st[id << 1 | 1]);
  }
  int query(int p, int id = 1, int tl = 1, int tr = n) {
    if (tl == tr) return st[id];
    int tm = tl + tr >> 1;
    push(id);
    if (p <= tm) return query(p, id << 1, tl, tm);
    return query(p, id << 1 | 1, tm + 1, tr);
  }
} segtree;
void upd_path(int u, int p, int w) {
  while (1) {
    if (nxt[u] == nxt[p]) {
      segtree.upd(second[p], second[u], w);
      return;
    }
    segtree.upd(second[nxt[u]], second[u], w);
    u = par[nxt[u]][0];
  }
}
int get_path(int u, int v, int w) {
  int l = lca(u, v);
  if (l != u) upd_path(u, lift(u, l), w);
  if (l != v) upd_path(v, lift(v, l), w);
  return max(lift_val(u, l), lift_val(v, l));
}
int ans[MAXN];
void Hollwo_Pelw() {
  cin >> n >> m;
  vector<edge> edges;
  for (int i = 1, u, v, w; i <= m; i++) {
    cin >> u >> v >> w;
    edges.push_back({u, v, w, i});
  }
  sort(edges.begin(), edges.end());
  vector<edge> mst, query;
  for (auto ed : edges) {
    int u = ed.u, v = ed.v, w = ed.w;
    if (dsu.merge(u, v)) {
      adj[u].emplace_back(v, w);
      adj[v].emplace_back(u, w);
      mst.push_back(ed);
    } else {
      query.push_back(ed);
    }
  }
  init_hld();
  for (auto ed : query) {
    ans[ed.id] = get_path(ed.u, ed.v, ed.w);
  }
  for (auto ed : mst) {
    int u = ed.u, v = ed.v;
    if (par[v][0] == u) swap(u, v);
    ans[ed.id] = segtree.query(second[u]);
    if (ans[ed.id] == inf) ans[ed.id] = 0;
  }
  for (int i = 1; i <= m; i++) {
    cout << ans[i] - 1 << " ";
  }
}

### 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;
bool endline = false;
template <class T>
istream& operator>>(istream& inp, vector<T>& v) {
  for (auto& it : v) inp >> it;
  return inp;
}
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
  for (auto& it : v) out << it << (endline ? "\n" : " ");
  return out;
}
template <class T, class U>
istream& operator>>(istream& inp, pair<T, U>& v) {
  inp >> v.first >> v.second;
  return inp;
}
template <class T, class U>
ostream& operator<<(ostream& out, pair<T, U>& v) {
  out << v.first << ' ' << v.second;
  return out;
}
void debug() { cout << endl; }
template <typename H, typename... T>
void debug(H a, T... b) {
  cout << a << ' ';
  debug(b...);
}
void FAST_IO(string filein = "", string fileout = "", string fileerr = "") {
  if (fopen(filein.c_str(), "r")) {
    freopen(filein.c_str(), "r", stdin);
    freopen(fileout.c_str(), "w", stdout);
  }
  cin.tie(0), cout.tie(0)->sync_with_stdio(0);
}
void Hollwo_Pelw();
signed main() {
  FAST_IO(".inp", ".out");
  int testcases = 1;
  for (int test = 1; test <= testcases; test++) {
    Hollwo_Pelw();
  }
  return 0;
}
const int allmod[3] = {(int)1e9 + 7, 998244353, (int)1e9 + 9};
const int mod = allmod[0];
const int MAXN = 2e5 + 5;
const int inf = 2e9;
const long long linf = 1e18;
const long double eps = 1e-9;
int n, m;
struct disjoint_set_union {
  int par[MAXN];
  disjoint_set_union() {
    for (int i = 1; i < MAXN; i++) par[i] = i;
  }
  int find(int u) { return par[u] = (par[u] == u ? u : find(par[u])); }
  bool merge(int u, int v) {
    if ((u = find(u)) == (v = find(v))) return 0;
    par[u] = v;
    return 1;
  }
} dsu;
struct edge {
  int u, v, w, id;
  bool operator<(const edge& oth) const { return w < oth.w; }
};
vector<pair<int, int> > adj[MAXN];
int par[MAXN][19], mn[MAXN][19];
int second[MAXN], E[MAXN], timer;
int sz[MAXN], nxt[MAXN];
void get_sz(int u, int p, int w) {
  par[u][0] = p, mn[u][0] = w;
  for (int i = 1, v; i <= 18; i++) {
    v = par[u][i - 1];
    par[u][i] = par[v][i - 1];
    mn[u][i] = max(mn[v][i - 1], mn[u][i - 1]);
  }
  sz[u] = 1;
  nxt[u] = u;
  for (auto& v : adj[u]) {
    if (v.first == p) swap(adj[u].back(), v);
    if (v.first == p) continue;
    get_sz(v.first, u, v.second);
    sz[u] += sz[v.first];
    if (sz[v.first] > sz[adj[u][0].first]) swap(adj[u][0], v);
  }
  if (adj[u].back().first == p) adj[u].pop_back();
}
void dfs_hld(int u) {
  second[u] = ++timer;
  for (auto& v : adj[u]) {
    if (v == adj[u][0]) nxt[v.first] = nxt[u];
    dfs_hld(v.first);
  }
  E[u] = timer;
}
void init_hld() {
  get_sz(1, 1, -inf);
  dfs_hld(1);
}
bool is_ancestor(int u, int v) {
  return second[u] <= second[v] && E[v] <= E[u];
}
int lca(int u, int v) {
  if (is_ancestor(u, v)) return u;
  if (is_ancestor(v, u)) return v;
  for (int i = 18; i >= 0; i--) {
    if (!is_ancestor(par[u][i], v)) u = par[u][i];
  }
  return par[u][0];
}
int lift_val(int u, int p) {
  if (u == p) return -inf;
  int res = -inf;
  for (int i = 18; i >= 0; i--) {
    if (!is_ancestor(par[u][i], p)) {
      res = max(res, mn[u][i]);
      u = par[u][i];
    }
  }
  return max(res, mn[u][0]);
}
int lift(int u, int p) {
  for (int i = 18; i >= 0; i--) {
    if (!is_ancestor(par[u][i], p)) {
      u = par[u][i];
    }
  }
  return u;
}
struct segment_tree {
  int st[MAXN << 2], lz[MAXN << 2];
  segment_tree() {
    for (int i = 1; i < (MAXN << 2); i++) st[i] = lz[i] = inf;
  }
  void apply(int id, int v) {
    lz[id] = min(lz[id], v);
    st[id] = min(st[id], v);
  }
  void push(int id) {
    apply(id << 1, lz[id]);
    apply(id << 1 | 1, lz[id]);
  }
  void upd(int l, int r, int v, int id = 1, int tl = 1, int tr = n) {
    if (l > r || l > tr || tl > r) return;
    if (l <= tl && tr <= r) return apply(id, v);
    int tm = tl + tr >> 1;
    push(id);
    upd(l, r, v, id << 1, tl, tm), upd(l, r, v, id << 1 | 1, tm + 1, tr);
    st[id] = min(st[id << 1], st[id << 1 | 1]);
  }
  int query(int p, int id = 1, int tl = 1, int tr = n) {
    if (tl == tr) return st[id];
    int tm = tl + tr >> 1;
    push(id);
    if (p <= tm) return query(p, id << 1, tl, tm);
    return query(p, id << 1 | 1, tm + 1, tr);
  }
} segtree;
void upd_path(int u, int p, int w) {
  while (1) {
    if (nxt[u] == nxt[p]) {
      segtree.upd(second[p], second[u], w);
      return;
    }
    segtree.upd(second[nxt[u]], second[u], w);
    u = par[nxt[u]][0];
  }
}
int get_path(int u, int v, int w) {
  int l = lca(u, v);
  if (l != u) upd_path(u, lift(u, l), w);
  if (l != v) upd_path(v, lift(v, l), w);
  return max(lift_val(u, l), lift_val(v, l));
}
int ans[MAXN];
void Hollwo_Pelw() {
  cin >> n >> m;
  vector<edge> edges;
  for (int i = 1, u, v, w; i <= m; i++) {
    cin >> u >> v >> w;
    edges.push_back({u, v, w, i});
  }
  sort(edges.begin(), edges.end());
  vector<edge> mst, query;
  for (auto ed : edges) {
    int u = ed.u, v = ed.v, w = ed.w;
    if (dsu.merge(u, v)) {
      adj[u].emplace_back(v, w);
      adj[v].emplace_back(u, w);
      mst.push_back(ed);
    } else {
      query.push_back(ed);
    }
  }
  init_hld();
  for (auto ed : query) {
    ans[ed.id] = get_path(ed.u, ed.v, ed.w);
  }
  for (auto ed : mst) {
    int u = ed.u, v = ed.v;
    if (par[v][0] == u) swap(u, v);
    ans[ed.id] = segtree.query(second[u]);
    if (ans[ed.id] == inf) ans[ed.id] = 0;
  }
  for (int i = 1; i <= m; i++) {
    cout << ans[i] - 1 << " ";
  }
}

```