#include <bits/stdc++.h>
using namespace std;
static inline void canhazfast() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
template <typename T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T>
T extgcd(T a, T b, T &x, T &y) {
  T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
  while (b) {
    T q = a / b;
    a %= b;
    swap(a, b);
    x0 -= q * x1;
    swap(x0, x1);
    y0 -= q * y1;
    swap(y0, y1);
  }
  x = x0;
  y = y0;
  return a;
}
static inline int ctz(unsigned x) { return __builtin_ctz(x); }
static inline int ctzll(unsigned long long x) { return __builtin_ctzll(x); }
static inline int clz(unsigned x) { return __builtin_clz(x); }
static inline int clzll(unsigned long long x) { return __builtin_clzll(x); }
static inline int popcnt(unsigned x) { return __builtin_popcount(x); }
static inline int popcntll(unsigned long long x) {
  return __builtin_popcountll(x);
}
static inline int bsr(unsigned x) { return 31 ^ clz(x); }
static inline int bsrll(unsigned long long x) { return 63 ^ clzll(x); }
struct E {
  int x, y, w, i;
};
bool cmp_edge(const E &a, const E &b) { return a.w < b.w; }
E edges[200016];
int ans[200016];
vector<pair<int, int> > adj[200016];
int id[200016], rnk[200016];
int d[200016], pe[200016];
int p[200016][20], pw[200016][20];
int fin(int x) { return id[x] == x ? x : id[x] = fin(id[x]); }
int uni(int x, int y) {
  if (rnk[x] < rnk[y]) swap(x, y);
  id[y] = x;
  rnk[x] = max(rnk[x], rnk[y] + 1);
  return x;
}
void dfs(int u) {
  for (int i = 0; p[u][i]; ++i) {
    int x = p[u][i];
    p[u][i + 1] = p[x][i];
    pw[u][i + 1] = max(pw[u][i], pw[x][i]);
  }
  for (const pair<int, int> &ee : adj[u]) {
    int v = ee.first;
    if (v == p[u][0]) continue;
    int i = ee.second;
    d[v] = d[u] + 1;
    pe[v] = edges[i].i;
    p[v][0] = u;
    pw[v][0] = edges[i].w;
    dfs(v);
  }
}
int get_path_max(int x, int y) {
  int res = 0;
  if (d[x] < d[y]) swap(x, y);
  for (int i = 0, dif = d[x] - d[y]; dif; ++i, dif >>= 1) {
    if (dif & 1) {
      res = max(res, pw[x][i]);
      x = p[x][i];
    }
  }
  if (x == y) return res;
  for (int i = bsr(d[x]); i >= 0; --i) {
    int px = p[x][i], py = p[y][i];
    if (px == py) continue;
    res = max(res, max(pw[x][i], pw[y][i]));
    x = px;
    y = py;
  }
  return max(res, max(pw[x][0], pw[y][0]));
}
int step(int u, int w) {
  int v = fin(p[u][0]);
  ans[pe[u]] = w;
  id[u] = v;
  return v;
}
void compress_path(int x, int y, int w) {
  x = fin(x);
  y = fin(y);
  if (d[x] < d[y]) swap(x, y);
  while (x != y) {
    if (d[x] > d[y])
      x = step(x, w);
    else
      y = step(y, w);
  }
}
int main() {
  canhazfast();
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> edges[i].x >> edges[i].y >> edges[i].w;
    edges[i].i = i;
  }
  sort(edges, edges + m, cmp_edge);
  iota(id + 1, id + 1 + n, 1);
  for (int i = 0; i < m; ++i) {
    int j = edges[i].i;
    int x = edges[i].x, y = edges[i].y;
    int rx = fin(x), ry = fin(y);
    if (rx == ry) {
      ans[j] = -1;
      continue;
    }
    ans[j] = 1010101010;
    adj[x].emplace_back(y, i);
    adj[y].emplace_back(x, i);
    uni(rx, ry);
  }
  pe[1] = -1;
  dfs(1);
  iota(id + 1, id + 1 + n, 1);
  for (int i = 0; i < m; ++i) {
    int j = edges[i].i;
    if (ans[j] != -1) continue;
    int x = edges[i].x, y = edges[i].y;
    ans[j] = get_path_max(x, y);
    compress_path(x, y, edges[i].w);
  }
  for (int i = 0; i < m; ++i) {
    if (ans[i] == 1010101010)
      ans[i] = -1;
    else
      --ans[i];
    cout << ans[i] << ' ';
  }
  return 0;
}

### Prompt

Please create a solution in cpp 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;
static inline void canhazfast() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
template <typename T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T>
T extgcd(T a, T b, T &x, T &y) {
  T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
  while (b) {
    T q = a / b;
    a %= b;
    swap(a, b);
    x0 -= q * x1;
    swap(x0, x1);
    y0 -= q * y1;
    swap(y0, y1);
  }
  x = x0;
  y = y0;
  return a;
}
static inline int ctz(unsigned x) { return __builtin_ctz(x); }
static inline int ctzll(unsigned long long x) { return __builtin_ctzll(x); }
static inline int clz(unsigned x) { return __builtin_clz(x); }
static inline int clzll(unsigned long long x) { return __builtin_clzll(x); }
static inline int popcnt(unsigned x) { return __builtin_popcount(x); }
static inline int popcntll(unsigned long long x) {
  return __builtin_popcountll(x);
}
static inline int bsr(unsigned x) { return 31 ^ clz(x); }
static inline int bsrll(unsigned long long x) { return 63 ^ clzll(x); }
struct E {
  int x, y, w, i;
};
bool cmp_edge(const E &a, const E &b) { return a.w < b.w; }
E edges[200016];
int ans[200016];
vector<pair<int, int> > adj[200016];
int id[200016], rnk[200016];
int d[200016], pe[200016];
int p[200016][20], pw[200016][20];
int fin(int x) { return id[x] == x ? x : id[x] = fin(id[x]); }
int uni(int x, int y) {
  if (rnk[x] < rnk[y]) swap(x, y);
  id[y] = x;
  rnk[x] = max(rnk[x], rnk[y] + 1);
  return x;
}
void dfs(int u) {
  for (int i = 0; p[u][i]; ++i) {
    int x = p[u][i];
    p[u][i + 1] = p[x][i];
    pw[u][i + 1] = max(pw[u][i], pw[x][i]);
  }
  for (const pair<int, int> &ee : adj[u]) {
    int v = ee.first;
    if (v == p[u][0]) continue;
    int i = ee.second;
    d[v] = d[u] + 1;
    pe[v] = edges[i].i;
    p[v][0] = u;
    pw[v][0] = edges[i].w;
    dfs(v);
  }
}
int get_path_max(int x, int y) {
  int res = 0;
  if (d[x] < d[y]) swap(x, y);
  for (int i = 0, dif = d[x] - d[y]; dif; ++i, dif >>= 1) {
    if (dif & 1) {
      res = max(res, pw[x][i]);
      x = p[x][i];
    }
  }
  if (x == y) return res;
  for (int i = bsr(d[x]); i >= 0; --i) {
    int px = p[x][i], py = p[y][i];
    if (px == py) continue;
    res = max(res, max(pw[x][i], pw[y][i]));
    x = px;
    y = py;
  }
  return max(res, max(pw[x][0], pw[y][0]));
}
int step(int u, int w) {
  int v = fin(p[u][0]);
  ans[pe[u]] = w;
  id[u] = v;
  return v;
}
void compress_path(int x, int y, int w) {
  x = fin(x);
  y = fin(y);
  if (d[x] < d[y]) swap(x, y);
  while (x != y) {
    if (d[x] > d[y])
      x = step(x, w);
    else
      y = step(y, w);
  }
}
int main() {
  canhazfast();
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> edges[i].x >> edges[i].y >> edges[i].w;
    edges[i].i = i;
  }
  sort(edges, edges + m, cmp_edge);
  iota(id + 1, id + 1 + n, 1);
  for (int i = 0; i < m; ++i) {
    int j = edges[i].i;
    int x = edges[i].x, y = edges[i].y;
    int rx = fin(x), ry = fin(y);
    if (rx == ry) {
      ans[j] = -1;
      continue;
    }
    ans[j] = 1010101010;
    adj[x].emplace_back(y, i);
    adj[y].emplace_back(x, i);
    uni(rx, ry);
  }
  pe[1] = -1;
  dfs(1);
  iota(id + 1, id + 1 + n, 1);
  for (int i = 0; i < m; ++i) {
    int j = edges[i].i;
    if (ans[j] != -1) continue;
    int x = edges[i].x, y = edges[i].y;
    ans[j] = get_path_max(x, y);
    compress_path(x, y, edges[i].w);
  }
  for (int i = 0; i < m; ++i) {
    if (ans[i] == 1010101010)
      ans[i] = -1;
    else
      --ans[i];
    cout << ans[i] << ' ';
  }
  return 0;
}

```