#include <bits/stdc++.h>
const double Pi = 3.14159265;
const double eps = 1e-10;
const double pi = acos(-1);
const int N = 4e5 + 10;
const int M = 2e5 + 10;
const unsigned long long base = 163;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
using namespace std;
int n, m;
template <typename T>
struct LinkCutTree {
  bool rev[N];
  int ch[N][2], fa[N], st[N];
  T MAX[N], val[N], val2[N], MIN[N];
  inline bool notroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
  inline void pushdown(int x) {
    if (val2[x] != INF) {
      if (ch[x][0])
        MIN[ch[x][0]] = min(MIN[ch[x][0]], val2[x]),
        val2[ch[x][0]] = min(val2[ch[x][0]], val2[x]);
      if (ch[x][1])
        MIN[ch[x][1]] = min(MIN[ch[x][1]], val2[x]),
        val2[ch[x][1]] = min(val2[ch[x][1]], val2[x]);
      val2[x] = INF;
    }
    if (!rev[x]) return;
    swap(ch[x][0], ch[x][1]);
    if (ch[x][0]) rev[ch[x][0]] ^= 1;
    if (ch[x][1]) rev[ch[x][1]] ^= 1;
    rev[x] ^= 1;
  }
  inline void pushup(int x) {
    MAX[x] = val[x];
    if (ch[x][0]) MAX[x] = max(MAX[ch[x][0]], MAX[x]);
    if (ch[x][1]) MAX[x] = max(MAX[ch[x][1]], MAX[x]);
  }
  inline void rotate(int x) {
    int y = fa[x], z = fa[y], k = (ch[y][1] == x), w = ch[x][!k];
    if (notroot(y)) ch[z][ch[z][1] == y] = x;
    ch[x][!k] = y;
    ch[y][k] = w;
    if (w) fa[w] = y;
    fa[y] = x;
    fa[x] = z;
    pushup(y);
  }
  inline void splay(int x) {
    int y = x, z = 0;
    st[++z] = y;
    while (notroot(y)) st[++z] = y = fa[y];
    while (z) pushdown(st[z--]);
    while (notroot(x)) {
      y = fa[x];
      z = fa[y];
      if (notroot(y)) rotate((ch[y][0] == x) ^ (ch[z][0] == y) ? x : y);
      rotate(x);
    }
    pushup(x);
  }
  inline void access(int x) {
    int rson = 0;
    for (; x; rson = x, x = fa[x]) {
      splay(x);
      ch[x][1] = rson;
      pushup(x);
    }
  }
  inline int findroot(int x) {
    access(x);
    splay(x);
    while (ch[x][0]) pushdown(x), x = ch[x][0];
    splay(x);
    return x;
  }
  inline void makeroot(int x) {
    access(x);
    splay(x);
    rev[x] ^= 1;
  }
  inline void link(int x, int y) {
    makeroot(x);
    fa[x] = y;
  }
  inline void split(int x, int y) {
    makeroot(x);
    access(y);
    splay(y);
  }
  inline bool cut(int x, int y) {
    makeroot(x);
    if (findroot(y) != x || fa[y] != x || ch[y][0]) return 0;
    ch[x][1] = fa[y] = 0;
    pushup(x);
    return 1;
  }
  inline void update(int x, int y) {
    splay(x);
    val[x] = y;
  }
};
LinkCutTree<int> lct;
struct edge {
  int u, v, c, id;
  bool operator<(const edge &oth) const { return c < oth.c; }
} E[N];
int fa[N];
bool vis[N];
int findfa(int x) { return fa[x] == x ? x : fa[x] = findfa(fa[x]); }
void work() {
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int u = E[i].u, v = E[i].v;
    u = findfa(u), v = findfa(v);
    if (u == v) continue;
    vis[E[i].id] = 1;
    fa[u] = v;
    int now = E[i].id + n;
    lct.val[now] = E[i].c;
    lct.val2[now] = INF;
    lct.link(now, E[i].u);
    lct.link(E[i].v, now);
  }
}
int ans[N];
int vv[N];
int main() {
  scanf("%d%d", &n, &m);
  memset(ans, -1, sizeof(ans));
  for (int i = 1; i <= n + m; ++i) lct.MIN[i] = lct.val2[i] = INF;
  for (int i = 1; i <= m; ++i) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    E[i] = (edge){u, v, c, i};
  }
  sort(E + 1, E + 1 + m);
  work();
  for (int i = 1; i <= m; ++i) {
    int id = E[i].id, u = E[i].u, v = E[i].v;
    if (!vis[id]) {
      lct.split(u, v);
      lct.val2[v] = min(lct.val2[v], E[i].c - 1);
      ans[id] = lct.MAX[v] - 1;
    }
  }
  for (int i = 1; i <= m; ++i) {
    int id = E[i].id;
    if (!vis[id]) {
    } else {
      lct.access(id + n);
      ans[id] = lct.MIN[id + n] == INF ? -1 : lct.MIN[id + n];
    }
  }
  for (int i = 1; i <= m; ++i) {
    printf("%d ", ans[i]);
  }
  return 0;
}

### Prompt

Create a solution in Cpp for 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>
const double Pi = 3.14159265;
const double eps = 1e-10;
const double pi = acos(-1);
const int N = 4e5 + 10;
const int M = 2e5 + 10;
const unsigned long long base = 163;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
using namespace std;
int n, m;
template <typename T>
struct LinkCutTree {
  bool rev[N];
  int ch[N][2], fa[N], st[N];
  T MAX[N], val[N], val2[N], MIN[N];
  inline bool notroot(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
  inline void pushdown(int x) {
    if (val2[x] != INF) {
      if (ch[x][0])
        MIN[ch[x][0]] = min(MIN[ch[x][0]], val2[x]),
        val2[ch[x][0]] = min(val2[ch[x][0]], val2[x]);
      if (ch[x][1])
        MIN[ch[x][1]] = min(MIN[ch[x][1]], val2[x]),
        val2[ch[x][1]] = min(val2[ch[x][1]], val2[x]);
      val2[x] = INF;
    }
    if (!rev[x]) return;
    swap(ch[x][0], ch[x][1]);
    if (ch[x][0]) rev[ch[x][0]] ^= 1;
    if (ch[x][1]) rev[ch[x][1]] ^= 1;
    rev[x] ^= 1;
  }
  inline void pushup(int x) {
    MAX[x] = val[x];
    if (ch[x][0]) MAX[x] = max(MAX[ch[x][0]], MAX[x]);
    if (ch[x][1]) MAX[x] = max(MAX[ch[x][1]], MAX[x]);
  }
  inline void rotate(int x) {
    int y = fa[x], z = fa[y], k = (ch[y][1] == x), w = ch[x][!k];
    if (notroot(y)) ch[z][ch[z][1] == y] = x;
    ch[x][!k] = y;
    ch[y][k] = w;
    if (w) fa[w] = y;
    fa[y] = x;
    fa[x] = z;
    pushup(y);
  }
  inline void splay(int x) {
    int y = x, z = 0;
    st[++z] = y;
    while (notroot(y)) st[++z] = y = fa[y];
    while (z) pushdown(st[z--]);
    while (notroot(x)) {
      y = fa[x];
      z = fa[y];
      if (notroot(y)) rotate((ch[y][0] == x) ^ (ch[z][0] == y) ? x : y);
      rotate(x);
    }
    pushup(x);
  }
  inline void access(int x) {
    int rson = 0;
    for (; x; rson = x, x = fa[x]) {
      splay(x);
      ch[x][1] = rson;
      pushup(x);
    }
  }
  inline int findroot(int x) {
    access(x);
    splay(x);
    while (ch[x][0]) pushdown(x), x = ch[x][0];
    splay(x);
    return x;
  }
  inline void makeroot(int x) {
    access(x);
    splay(x);
    rev[x] ^= 1;
  }
  inline void link(int x, int y) {
    makeroot(x);
    fa[x] = y;
  }
  inline void split(int x, int y) {
    makeroot(x);
    access(y);
    splay(y);
  }
  inline bool cut(int x, int y) {
    makeroot(x);
    if (findroot(y) != x || fa[y] != x || ch[y][0]) return 0;
    ch[x][1] = fa[y] = 0;
    pushup(x);
    return 1;
  }
  inline void update(int x, int y) {
    splay(x);
    val[x] = y;
  }
};
LinkCutTree<int> lct;
struct edge {
  int u, v, c, id;
  bool operator<(const edge &oth) const { return c < oth.c; }
} E[N];
int fa[N];
bool vis[N];
int findfa(int x) { return fa[x] == x ? x : fa[x] = findfa(fa[x]); }
void work() {
  for (int i = 1; i <= n; ++i) fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int u = E[i].u, v = E[i].v;
    u = findfa(u), v = findfa(v);
    if (u == v) continue;
    vis[E[i].id] = 1;
    fa[u] = v;
    int now = E[i].id + n;
    lct.val[now] = E[i].c;
    lct.val2[now] = INF;
    lct.link(now, E[i].u);
    lct.link(E[i].v, now);
  }
}
int ans[N];
int vv[N];
int main() {
  scanf("%d%d", &n, &m);
  memset(ans, -1, sizeof(ans));
  for (int i = 1; i <= n + m; ++i) lct.MIN[i] = lct.val2[i] = INF;
  for (int i = 1; i <= m; ++i) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    E[i] = (edge){u, v, c, i};
  }
  sort(E + 1, E + 1 + m);
  work();
  for (int i = 1; i <= m; ++i) {
    int id = E[i].id, u = E[i].u, v = E[i].v;
    if (!vis[id]) {
      lct.split(u, v);
      lct.val2[v] = min(lct.val2[v], E[i].c - 1);
      ans[id] = lct.MAX[v] - 1;
    }
  }
  for (int i = 1; i <= m; ++i) {
    int id = E[i].id;
    if (!vis[id]) {
    } else {
      lct.access(id + n);
      ans[id] = lct.MIN[id + n] == INF ? -1 : lct.MIN[id + n];
    }
  }
  for (int i = 1; i <= m; ++i) {
    printf("%d ", ans[i]);
  }
  return 0;
}

```