#include <bits/stdc++.h>
using namespace std;
int const inf = 1000 * 1000 * 1000;
long long const inf64 = 1ll * inf * inf;
inline int nextRand() { return (rand() << 16) | rand(); }
template <class T, class P>
class Treap {
 private:
  struct Node {
    Node *l, *r;
    int prior;
    int cnt;
    T key;
    P value;
    P minValue;
    Node()
        : l(0),
          r(0),
          prior(nextRand()),
          key(T()),
          value(P()),
          minValue(P()),
          cnt(0) {}
    Node(T _key, P _value)
        : l(0),
          r(0),
          prior(nextRand()),
          key(_key),
          value(_value),
          minValue(_value),
          cnt(1) {}
  };
  int get_cnt(Node *v) const { return v ? v->cnt : 0; }
  void update(Node *v) {
    v->minValue = v->value;
    v->cnt = 1;
    if (v->l) {
      v->minValue = min(v->minValue, v->l->minValue);
      v->cnt += v->l->cnt;
    }
    if (v->r) {
      v->minValue = min(v->minValue, v->r->minValue);
      v->cnt += v->r->cnt;
    }
  }
  void merge(Node *&v, Node *l, Node *r) {
    if (!l || !r) {
      v = l ? l : r;
      return;
    }
    if (l->prior < r->prior) {
      merge(l->r, l->r, r);
      v = l;
    } else {
      merge(r->l, l, r->l);
      v = r;
    }
    if (v) update(v);
  }
  void split_by_key(Node *v, Node *&l, Node *&r, T x) {
    if (!v) {
      l = r = 0;
      return;
    }
    if (v->key <= x) {
      split_by_key(v->r, v->r, r, x);
      l = v;
    } else {
      split_by_key(v->l, l, v->l, x);
      r = v;
    }
    if (l) update(l);
    if (r) update(r);
  }
  void split_by_cnt(Node *v, Node *&l, Node *&r, int x) {
    if (!v) {
      l = r = 0;
      return;
    }
    if (get_cnt(v->l) + 1 <= x) {
      split_by_cnt(v->r, v->r, r, x - get_cnt(v->l) - 1);
      l = v;
    } else {
      split_by_cnt(v->l, l, v->l, x);
      r = v;
    }
    if (l) update(l);
    if (r) update(r);
  }

 private:
  Node *t;

 public:
  Treap() : t(0) {}
  void insert(T key, P value) {
    if (count(key)) return;
    Node *nl, *nr;
    nl = nr = 0;
    split_by_key(t, nl, nr, key);
    merge(nl, nl, new Node(key, value));
    merge(t, nl, nr);
  }
  int count(T key) {
    int res;
    Node *nl, *nm, *nr;
    nl = nm = nr = 0;
    split_by_key(t, nl, nr, key);
    if (!nl) {
      res = 0;
    } else {
      split_by_cnt(nl, nl, nm, get_cnt(nl) - 1);
      res = nm->key == key;
      merge(nl, nl, nm);
    }
    merge(t, nl, nr);
    return res;
  }
  void erase(T key) {
    if (!count(key)) return;
    Node *nl, *nm, *nr;
    nl = nm = nr = 0;
    split_by_key(t, nl, nr, key);
    split_by_cnt(nl, nl, nm, get_cnt(nl) - 1);
    merge(t, nl, nr);
  }
  ~Treap() {
    if (t) delete t;
  }
  P getMinValue() const { return t->minValue; }
  int size() const { return get_cnt(t); }
  bool empty() const { return get_cnt(t) == 0; }
  Treap &operator=(Treap const &other) {
    if (this == &other) return *this;
    t = other.t;
    return *this;
  }
};
int const N = 2e5 + 5;
struct edge {
  int id, u, v, w;
  edge() : id(0), u(0), v(0), w(0) {}
  edge(int uu, int vv, int ww, int idid) : id(idid), u(uu), v(vv), w(ww) {}
  void read(int idid) {
    id = idid;
    scanf("%d %d %d", &u, &v, &w);
  }
  int To(int x) { return x == u ? v : u; }
};
struct DSU {
  int n;
  vector<int> p, h;
  DSU() : n(0) {}
  DSU(int nn) : n(nn) {
    p.resize(n + 1);
    h.resize(n + 1);
    for (int i = 1; i <= n; i++) {
      p[i] = i;
      h[i] = 0;
    }
  }
  int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }
  bool merge(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return false;
    if (h[x] < h[y]) {
      swap(x, y);
    }
    p[y] = x;
    if (h[x] == h[y]) {
      h[x]++;
    }
    return true;
  }
};
int n, m;
edge e[N];
vector<int> g[N];
vector<int> f[N];
Treap<int, int> tr[N];
set<int> edgeList[N];
int tin[N];
int tout[N];
int result[N];
vector<int> dl[N];
bool used[N];
void go(int v, int par = -1) {
  static int timer = 0;
  tin[v] = timer++;
  for (int eid : g[v]) {
    int to = e[eid].To(v);
    if (to == par) continue;
    go(to, v);
  }
  tout[v] = timer++;
}
int getLca(int u, int v);
void dfs(int v, int from = -1, int par = -1) {
  used[v] = 1;
  for (int eid : g[v]) {
    int to = e[eid].To(v);
    if (to == par) continue;
    dfs(to, eid, v);
    if (edgeList[v].size() < edgeList[to].size()) {
      swap(tr[v], tr[to]);
      swap(edgeList[v], edgeList[to]);
    }
    for (int j : edgeList[to]) {
      edgeList[v].insert(j);
      tr[v].insert(j, e[j].w);
    }
    edgeList[to].clear();
  }
  for (int eid : f[v]) {
    int to = e[eid].To(v);
    if (to == par) continue;
    if (tin[v] <= tin[to] && tout[to] <= tout[v]) {
      tr[v].erase(eid);
      edgeList[v].erase(eid);
    } else {
      tr[v].insert(eid, e[eid].w);
      edgeList[v].insert(eid);
      dl[getLca(v, to)].push_back(eid);
    }
  }
  for (int eid : dl[v]) {
    tr[v].erase(eid);
    edgeList[v].erase(eid);
  }
  result[from] = tr[v].empty() ? -1 : tr[v].getMinValue() - 1;
}
int const LOG = 21;
int mx[LOG][N];
int pr[LOG][N];
int he[N];
void build(int v, int cost = 0, int par = -1) {
  pr[0][v] = par;
  mx[0][v] = cost;
  if (par == -1) {
    he[v] = 0;
  } else {
    he[v] = he[par] + 1;
  }
  for (int eid : g[v]) {
    int to = e[eid].To(v);
    if (to == par) continue;
    build(to, e[eid].w, v);
  }
}
int getLca(int u, int v) {
  if (he[u] < he[v]) swap(u, v);
  for (int i = LOG - 1; i >= 0; i--) {
    int pu = pr[i][u];
    if (pu == -1 || he[pu] < he[v]) continue;
    u = pu;
  }
  for (int i = LOG - 1; i >= 0; i--) {
    int pu = pr[i][u];
    int pv = pr[i][v];
    if (pu == -1 || pv == -1 || pu == pv) continue;
    u = pu;
    v = pv;
  }
  if (u != v) u = pr[0][u];
  return u;
}
int getVerticalMax(int P, int S) {
  int res = 0;
  for (int i = LOG - 1; i >= 0; i--) {
    int PS = pr[i][S];
    if (PS == -1 || he[PS] < he[P]) continue;
    res = max(res, mx[i][S]);
    S = PS;
  }
  return res;
}
int getMax(int u, int v) {
  int z = getLca(u, v);
  return max(getVerticalMax(z, u), getVerticalMax(z, v));
}
int main() {
  srand(time(NULL));
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    e[i].read(i);
  }
  sort(e + 1, e + m + 1, [&](edge i, edge j) { return i.w < j.w; });
  {
    DSU dsu(n);
    for (int i = 1; i <= m; i++) {
      if (dsu.merge(e[i].u, e[i].v)) {
        g[e[i].u].push_back(e[i].id);
        g[e[i].v].push_back(e[i].id);
      } else {
        f[e[i].u].push_back(e[i].id);
        f[e[i].v].push_back(e[i].id);
      }
    }
  }
  sort(e + 1, e + m + 1, [&](edge i, edge j) { return i.id < j.id; });
  for (int j = 0; j < LOG; j++) {
    for (int v = 1; v <= n; v++) {
      pr[j][v] = -1;
    }
  }
  build(1);
  for (int j = 1; j < LOG; j++) {
    for (int v = 1; v <= n; v++) {
      int pv = pr[j - 1][v];
      if (pv == -1) {
        pr[j][v] = -1;
        mx[j][v] = mx[j - 1][v];
      } else {
        pr[j][v] = pr[j - 1][pv];
        mx[j][v] = max(mx[j - 1][v], mx[j - 1][pv]);
      }
    }
  }
  go(1);
  dfs(1);
  for (int v = 1; v <= n; v++) {
    for (int eid : f[v]) {
      int u = e[eid].To(v);
      result[eid] = getMax(u, v) - 1;
    }
  }
  for (int i = 1; i <= m; i++) {
    printf("%d ", result[i]);
  }
  printf("\n");
  return 0;
}

### Prompt

Your challenge is to write 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;
int const inf = 1000 * 1000 * 1000;
long long const inf64 = 1ll * inf * inf;
inline int nextRand() { return (rand() << 16) | rand(); }
template <class T, class P>
class Treap {
 private:
  struct Node {
    Node *l, *r;
    int prior;
    int cnt;
    T key;
    P value;
    P minValue;
    Node()
        : l(0),
          r(0),
          prior(nextRand()),
          key(T()),
          value(P()),
          minValue(P()),
          cnt(0) {}
    Node(T _key, P _value)
        : l(0),
          r(0),
          prior(nextRand()),
          key(_key),
          value(_value),
          minValue(_value),
          cnt(1) {}
  };
  int get_cnt(Node *v) const { return v ? v->cnt : 0; }
  void update(Node *v) {
    v->minValue = v->value;
    v->cnt = 1;
    if (v->l) {
      v->minValue = min(v->minValue, v->l->minValue);
      v->cnt += v->l->cnt;
    }
    if (v->r) {
      v->minValue = min(v->minValue, v->r->minValue);
      v->cnt += v->r->cnt;
    }
  }
  void merge(Node *&v, Node *l, Node *r) {
    if (!l || !r) {
      v = l ? l : r;
      return;
    }
    if (l->prior < r->prior) {
      merge(l->r, l->r, r);
      v = l;
    } else {
      merge(r->l, l, r->l);
      v = r;
    }
    if (v) update(v);
  }
  void split_by_key(Node *v, Node *&l, Node *&r, T x) {
    if (!v) {
      l = r = 0;
      return;
    }
    if (v->key <= x) {
      split_by_key(v->r, v->r, r, x);
      l = v;
    } else {
      split_by_key(v->l, l, v->l, x);
      r = v;
    }
    if (l) update(l);
    if (r) update(r);
  }
  void split_by_cnt(Node *v, Node *&l, Node *&r, int x) {
    if (!v) {
      l = r = 0;
      return;
    }
    if (get_cnt(v->l) + 1 <= x) {
      split_by_cnt(v->r, v->r, r, x - get_cnt(v->l) - 1);
      l = v;
    } else {
      split_by_cnt(v->l, l, v->l, x);
      r = v;
    }
    if (l) update(l);
    if (r) update(r);
  }

 private:
  Node *t;

 public:
  Treap() : t(0) {}
  void insert(T key, P value) {
    if (count(key)) return;
    Node *nl, *nr;
    nl = nr = 0;
    split_by_key(t, nl, nr, key);
    merge(nl, nl, new Node(key, value));
    merge(t, nl, nr);
  }
  int count(T key) {
    int res;
    Node *nl, *nm, *nr;
    nl = nm = nr = 0;
    split_by_key(t, nl, nr, key);
    if (!nl) {
      res = 0;
    } else {
      split_by_cnt(nl, nl, nm, get_cnt(nl) - 1);
      res = nm->key == key;
      merge(nl, nl, nm);
    }
    merge(t, nl, nr);
    return res;
  }
  void erase(T key) {
    if (!count(key)) return;
    Node *nl, *nm, *nr;
    nl = nm = nr = 0;
    split_by_key(t, nl, nr, key);
    split_by_cnt(nl, nl, nm, get_cnt(nl) - 1);
    merge(t, nl, nr);
  }
  ~Treap() {
    if (t) delete t;
  }
  P getMinValue() const { return t->minValue; }
  int size() const { return get_cnt(t); }
  bool empty() const { return get_cnt(t) == 0; }
  Treap &operator=(Treap const &other) {
    if (this == &other) return *this;
    t = other.t;
    return *this;
  }
};
int const N = 2e5 + 5;
struct edge {
  int id, u, v, w;
  edge() : id(0), u(0), v(0), w(0) {}
  edge(int uu, int vv, int ww, int idid) : id(idid), u(uu), v(vv), w(ww) {}
  void read(int idid) {
    id = idid;
    scanf("%d %d %d", &u, &v, &w);
  }
  int To(int x) { return x == u ? v : u; }
};
struct DSU {
  int n;
  vector<int> p, h;
  DSU() : n(0) {}
  DSU(int nn) : n(nn) {
    p.resize(n + 1);
    h.resize(n + 1);
    for (int i = 1; i <= n; i++) {
      p[i] = i;
      h[i] = 0;
    }
  }
  int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }
  bool merge(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return false;
    if (h[x] < h[y]) {
      swap(x, y);
    }
    p[y] = x;
    if (h[x] == h[y]) {
      h[x]++;
    }
    return true;
  }
};
int n, m;
edge e[N];
vector<int> g[N];
vector<int> f[N];
Treap<int, int> tr[N];
set<int> edgeList[N];
int tin[N];
int tout[N];
int result[N];
vector<int> dl[N];
bool used[N];
void go(int v, int par = -1) {
  static int timer = 0;
  tin[v] = timer++;
  for (int eid : g[v]) {
    int to = e[eid].To(v);
    if (to == par) continue;
    go(to, v);
  }
  tout[v] = timer++;
}
int getLca(int u, int v);
void dfs(int v, int from = -1, int par = -1) {
  used[v] = 1;
  for (int eid : g[v]) {
    int to = e[eid].To(v);
    if (to == par) continue;
    dfs(to, eid, v);
    if (edgeList[v].size() < edgeList[to].size()) {
      swap(tr[v], tr[to]);
      swap(edgeList[v], edgeList[to]);
    }
    for (int j : edgeList[to]) {
      edgeList[v].insert(j);
      tr[v].insert(j, e[j].w);
    }
    edgeList[to].clear();
  }
  for (int eid : f[v]) {
    int to = e[eid].To(v);
    if (to == par) continue;
    if (tin[v] <= tin[to] && tout[to] <= tout[v]) {
      tr[v].erase(eid);
      edgeList[v].erase(eid);
    } else {
      tr[v].insert(eid, e[eid].w);
      edgeList[v].insert(eid);
      dl[getLca(v, to)].push_back(eid);
    }
  }
  for (int eid : dl[v]) {
    tr[v].erase(eid);
    edgeList[v].erase(eid);
  }
  result[from] = tr[v].empty() ? -1 : tr[v].getMinValue() - 1;
}
int const LOG = 21;
int mx[LOG][N];
int pr[LOG][N];
int he[N];
void build(int v, int cost = 0, int par = -1) {
  pr[0][v] = par;
  mx[0][v] = cost;
  if (par == -1) {
    he[v] = 0;
  } else {
    he[v] = he[par] + 1;
  }
  for (int eid : g[v]) {
    int to = e[eid].To(v);
    if (to == par) continue;
    build(to, e[eid].w, v);
  }
}
int getLca(int u, int v) {
  if (he[u] < he[v]) swap(u, v);
  for (int i = LOG - 1; i >= 0; i--) {
    int pu = pr[i][u];
    if (pu == -1 || he[pu] < he[v]) continue;
    u = pu;
  }
  for (int i = LOG - 1; i >= 0; i--) {
    int pu = pr[i][u];
    int pv = pr[i][v];
    if (pu == -1 || pv == -1 || pu == pv) continue;
    u = pu;
    v = pv;
  }
  if (u != v) u = pr[0][u];
  return u;
}
int getVerticalMax(int P, int S) {
  int res = 0;
  for (int i = LOG - 1; i >= 0; i--) {
    int PS = pr[i][S];
    if (PS == -1 || he[PS] < he[P]) continue;
    res = max(res, mx[i][S]);
    S = PS;
  }
  return res;
}
int getMax(int u, int v) {
  int z = getLca(u, v);
  return max(getVerticalMax(z, u), getVerticalMax(z, v));
}
int main() {
  srand(time(NULL));
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    e[i].read(i);
  }
  sort(e + 1, e + m + 1, [&](edge i, edge j) { return i.w < j.w; });
  {
    DSU dsu(n);
    for (int i = 1; i <= m; i++) {
      if (dsu.merge(e[i].u, e[i].v)) {
        g[e[i].u].push_back(e[i].id);
        g[e[i].v].push_back(e[i].id);
      } else {
        f[e[i].u].push_back(e[i].id);
        f[e[i].v].push_back(e[i].id);
      }
    }
  }
  sort(e + 1, e + m + 1, [&](edge i, edge j) { return i.id < j.id; });
  for (int j = 0; j < LOG; j++) {
    for (int v = 1; v <= n; v++) {
      pr[j][v] = -1;
    }
  }
  build(1);
  for (int j = 1; j < LOG; j++) {
    for (int v = 1; v <= n; v++) {
      int pv = pr[j - 1][v];
      if (pv == -1) {
        pr[j][v] = -1;
        mx[j][v] = mx[j - 1][v];
      } else {
        pr[j][v] = pr[j - 1][pv];
        mx[j][v] = max(mx[j - 1][v], mx[j - 1][pv]);
      }
    }
  }
  go(1);
  dfs(1);
  for (int v = 1; v <= n; v++) {
    for (int eid : f[v]) {
      int u = e[eid].To(v);
      result[eid] = getMax(u, v) - 1;
    }
  }
  for (int i = 1; i <= m; i++) {
    printf("%d ", result[i]);
  }
  printf("\n");
  return 0;
}

```