#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
int tmpSubmit;
const int maxn = 200005;
struct Edge {
  int id;
  int val;
  int u, v;
  int choose;
  Edge() {}
  Edge(int _id, int _val, int _u, int _v)
      : id(_id), val(_val), u(_u), v(_v), choose(0) {}
  bool operator<(const Edge B) const { return val < B.val; }
} edges[maxn];
int father[maxn];
int getfather(int x) {
  return father[x] == x ? x : father[x] = getfather(father[x]);
}
vector<pair<int, int> > V[maxn];
int node2edge[maxn];
int Sz[maxn];
int son[maxn];
int pre[maxn];
int dep[maxn];
void dfs(int x, int fa) {
  Sz[x] = 1;
  dep[x] = dep[fa] + 1;
  for (pair<int, int> t : V[x])
    if (t.first != fa) {
      node2edge[t.first] = t.second;
      pre[t.first] = x;
      dfs(t.first, x);
      Sz[x] += Sz[t.first];
      if (Sz[t.first] > Sz[son[x]]) son[x] = t.first;
    }
}
int idx[maxn], idxtot;
int tp[maxn];
void repos(int x, int top) {
  tp[x] = top;
  idx[x] = ++idxtot;
  if (son[x]) repos(son[x], top);
  for (pair<int, int> t : V[x])
    if (t.first != pre[x]) {
      if (t.first == son[x]) continue;
      repos(t.first, t.first);
    }
}
int Max[maxn << 4];
int initval[maxn];
void build_max(int o, int l, int r) {
  if (l == r) {
    Max[o] = initval[l];
    return;
  }
  int mid = (l + r) >> 1;
  build_max(o << 1, l, mid);
  build_max(o << 1 | 1, mid + 1, r);
  Max[o] = max(Max[o << 1], Max[o << 1 | 1]);
}
int query_max(int o, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) {
    return Max[o];
  }
  int mid = (l + r) >> 1;
  int ret = 0;
  if (ql <= mid) ret = max(ret, query_max(o << 1, l, mid, ql, qr));
  if (qr > mid) ret = max(ret, query_max(o << 1 | 1, mid + 1, r, ql, qr));
  return ret;
}
int Min[maxn << 4], cov[maxn << 4];
inline void pushdown(int o) {
  if (cov[o] == inf) return;
  cov[o << 1] = min(cov[o << 1], cov[o]);
  Min[o << 1] = min(Min[o << 1], cov[o]);
  cov[o << 1 | 1] = min(cov[o << 1 | 1], cov[o]);
  Min[o << 1 | 1] = min(Min[o << 1 | 1], cov[o]);
  cov[o] = inf;
}
void update_min(int o, int l, int r, int ql, int qr, int val) {
  if (ql <= l && r <= qr) {
    cov[o] = min(cov[o], val);
    Min[o] = min(Min[o], val);
    return;
  }
  pushdown(o);
  int mid = (l + r) >> 1;
  if (ql <= mid) update_min(o << 1, l, mid, ql, qr, val);
  if (qr > mid) update_min(o << 1 | 1, mid + 1, r, ql, qr, val);
  Min[o] = min(Min[o << 1], Min[o << 1 | 1]);
}
int query_min(int o, int l, int r, int x) {
  if (l == r) return Min[o];
  pushdown(o);
  int mid = (l + r) >> 1;
  if (x <= mid)
    return query_min(o << 1, l, mid, x);
  else
    return query_min(o << 1 | 1, mid + 1, r, x);
}
int n;
int Query_Max(int u, int v) {
  int ret = -inf;
  while (tp[u] != tp[v]) {
    if (dep[tp[u]] < dep[tp[v]]) swap(u, v);
    ret = max(ret, query_max(1, 1, n, idx[tp[u]], idx[u]));
    u = pre[tp[u]];
  }
  ret = max(ret,
            query_max(1, 1, n, min(idx[u], idx[v]) + 1, max(idx[u], idx[v])));
  return ret;
}
void Update_Min(int u, int v, int val) {
  while (tp[u] != tp[v]) {
    if (dep[tp[u]] < dep[tp[v]]) swap(u, v);
    update_min(1, 1, n, idx[tp[u]], idx[u], val);
    u = pre[tp[u]];
  }
  update_min(1, 1, n, min(idx[u], idx[v]) + 1, max(idx[u], idx[v]), val);
}
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int val, u, v;
    scanf("%d%d%d", &u, &v, &val);
    edges[i] = Edge(i, val, u, v);
  }
  sort(edges + 1, edges + 1 + m);
  for (int i = 1; i <= n; i++) father[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = edges[i].u, v = edges[i].v;
    if (getfather(u) != getfather(v)) {
      father[getfather(u)] = getfather(v);
      edges[i].choose = 1;
    }
  }
  for (int i = 1; i <= m; i++)
    if (edges[i].choose) {
      int u = edges[i].u, v = edges[i].v;
      V[u].push_back(make_pair(v, i)), V[v].push_back(make_pair(u, i));
    }
  dfs(1, 1);
  repos(1, 1);
  for (int i = 1; i <= n; i++) {
    initval[idx[i]] = edges[node2edge[i]].val;
  }
  build_max(1, 1, n);
  memset(Min, 0x3f, sizeof(Min));
  memset(cov, 0x3f, sizeof(cov));
  static int ans[maxn];
  for (int i = 1; i <= m; i++) {
    if (edges[i].choose) continue;
    int u = edges[i].u, v = edges[i].v;
    ans[edges[i].id] = Query_Max(u, v) - 1;
    Update_Min(u, v, edges[i].val);
  }
  for (int i = 1; i <= n; i++) {
    int tmp = query_min(1, 1, n, idx[i]);
    if (tmp == inf)
      ans[edges[node2edge[i]].id] = -1;
    else
      ans[edges[node2edge[i]].id] = tmp - 1;
  }
  for (int i = 1; i <= m; i++) {
    printf("%d ", ans[i]);
  }
}

### Prompt

Please formulate 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;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
int tmpSubmit;
const int maxn = 200005;
struct Edge {
  int id;
  int val;
  int u, v;
  int choose;
  Edge() {}
  Edge(int _id, int _val, int _u, int _v)
      : id(_id), val(_val), u(_u), v(_v), choose(0) {}
  bool operator<(const Edge B) const { return val < B.val; }
} edges[maxn];
int father[maxn];
int getfather(int x) {
  return father[x] == x ? x : father[x] = getfather(father[x]);
}
vector<pair<int, int> > V[maxn];
int node2edge[maxn];
int Sz[maxn];
int son[maxn];
int pre[maxn];
int dep[maxn];
void dfs(int x, int fa) {
  Sz[x] = 1;
  dep[x] = dep[fa] + 1;
  for (pair<int, int> t : V[x])
    if (t.first != fa) {
      node2edge[t.first] = t.second;
      pre[t.first] = x;
      dfs(t.first, x);
      Sz[x] += Sz[t.first];
      if (Sz[t.first] > Sz[son[x]]) son[x] = t.first;
    }
}
int idx[maxn], idxtot;
int tp[maxn];
void repos(int x, int top) {
  tp[x] = top;
  idx[x] = ++idxtot;
  if (son[x]) repos(son[x], top);
  for (pair<int, int> t : V[x])
    if (t.first != pre[x]) {
      if (t.first == son[x]) continue;
      repos(t.first, t.first);
    }
}
int Max[maxn << 4];
int initval[maxn];
void build_max(int o, int l, int r) {
  if (l == r) {
    Max[o] = initval[l];
    return;
  }
  int mid = (l + r) >> 1;
  build_max(o << 1, l, mid);
  build_max(o << 1 | 1, mid + 1, r);
  Max[o] = max(Max[o << 1], Max[o << 1 | 1]);
}
int query_max(int o, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) {
    return Max[o];
  }
  int mid = (l + r) >> 1;
  int ret = 0;
  if (ql <= mid) ret = max(ret, query_max(o << 1, l, mid, ql, qr));
  if (qr > mid) ret = max(ret, query_max(o << 1 | 1, mid + 1, r, ql, qr));
  return ret;
}
int Min[maxn << 4], cov[maxn << 4];
inline void pushdown(int o) {
  if (cov[o] == inf) return;
  cov[o << 1] = min(cov[o << 1], cov[o]);
  Min[o << 1] = min(Min[o << 1], cov[o]);
  cov[o << 1 | 1] = min(cov[o << 1 | 1], cov[o]);
  Min[o << 1 | 1] = min(Min[o << 1 | 1], cov[o]);
  cov[o] = inf;
}
void update_min(int o, int l, int r, int ql, int qr, int val) {
  if (ql <= l && r <= qr) {
    cov[o] = min(cov[o], val);
    Min[o] = min(Min[o], val);
    return;
  }
  pushdown(o);
  int mid = (l + r) >> 1;
  if (ql <= mid) update_min(o << 1, l, mid, ql, qr, val);
  if (qr > mid) update_min(o << 1 | 1, mid + 1, r, ql, qr, val);
  Min[o] = min(Min[o << 1], Min[o << 1 | 1]);
}
int query_min(int o, int l, int r, int x) {
  if (l == r) return Min[o];
  pushdown(o);
  int mid = (l + r) >> 1;
  if (x <= mid)
    return query_min(o << 1, l, mid, x);
  else
    return query_min(o << 1 | 1, mid + 1, r, x);
}
int n;
int Query_Max(int u, int v) {
  int ret = -inf;
  while (tp[u] != tp[v]) {
    if (dep[tp[u]] < dep[tp[v]]) swap(u, v);
    ret = max(ret, query_max(1, 1, n, idx[tp[u]], idx[u]));
    u = pre[tp[u]];
  }
  ret = max(ret,
            query_max(1, 1, n, min(idx[u], idx[v]) + 1, max(idx[u], idx[v])));
  return ret;
}
void Update_Min(int u, int v, int val) {
  while (tp[u] != tp[v]) {
    if (dep[tp[u]] < dep[tp[v]]) swap(u, v);
    update_min(1, 1, n, idx[tp[u]], idx[u], val);
    u = pre[tp[u]];
  }
  update_min(1, 1, n, min(idx[u], idx[v]) + 1, max(idx[u], idx[v]), val);
}
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int val, u, v;
    scanf("%d%d%d", &u, &v, &val);
    edges[i] = Edge(i, val, u, v);
  }
  sort(edges + 1, edges + 1 + m);
  for (int i = 1; i <= n; i++) father[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = edges[i].u, v = edges[i].v;
    if (getfather(u) != getfather(v)) {
      father[getfather(u)] = getfather(v);
      edges[i].choose = 1;
    }
  }
  for (int i = 1; i <= m; i++)
    if (edges[i].choose) {
      int u = edges[i].u, v = edges[i].v;
      V[u].push_back(make_pair(v, i)), V[v].push_back(make_pair(u, i));
    }
  dfs(1, 1);
  repos(1, 1);
  for (int i = 1; i <= n; i++) {
    initval[idx[i]] = edges[node2edge[i]].val;
  }
  build_max(1, 1, n);
  memset(Min, 0x3f, sizeof(Min));
  memset(cov, 0x3f, sizeof(cov));
  static int ans[maxn];
  for (int i = 1; i <= m; i++) {
    if (edges[i].choose) continue;
    int u = edges[i].u, v = edges[i].v;
    ans[edges[i].id] = Query_Max(u, v) - 1;
    Update_Min(u, v, edges[i].val);
  }
  for (int i = 1; i <= n; i++) {
    int tmp = query_min(1, 1, n, idx[i]);
    if (tmp == inf)
      ans[edges[node2edge[i]].id] = -1;
    else
      ans[edges[node2edge[i]].id] = tmp - 1;
  }
  for (int i = 1; i <= m; i++) {
    printf("%d ", ans[i]);
  }
}

```