#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 2e9;
struct edg {
  int u, v, w, id;
  edg(int u = 0, int v = 0, int w = 0, int id = 0) : u(u), v(v), w(w), id(id) {}
};
inline bool cmp(edg a1, edg b1) { return a1.w < b1.w; }
edg b[maxn];
struct node {
  int lc, rc;
  int val;
};
node t1[maxn * 25];
int top;
inline int newn() {
  top++;
  t1[top].val = inf;
  return top;
}
inline void co(int u) { t1[u].val = min(t1[t1[u].lc].val, t1[t1[u].rc].val); }
inline int insert(int u, int l, int r, int id, int x) {
  if (u == 0) u = newn();
  if (l == r) {
    t1[u].val = min(t1[u].val, x);
    return u;
  }
  int mid = (l + r) / 2;
  if (id <= mid)
    t1[u].lc = insert(t1[u].lc, l, mid, id, x);
  else
    t1[u].rc = insert(t1[u].rc, mid + 1, r, id, x);
  co(u);
  return u;
}
inline int qmin(int u, int l, int r, int ql, int qr) {
  if (ql > qr) return inf;
  if (u == 0) return inf;
  if (ql <= l && r <= qr) return t1[u].val;
  int mid = (l + r) / 2, res = inf;
  if (ql <= mid) res = min(res, qmin(t1[u].lc, l, mid, ql, qr));
  if (mid + 1 <= qr) res = min(res, qmin(t1[u].rc, mid + 1, r, ql, qr));
  return res;
}
inline int merge(int p, int q, int l, int r) {
  if (p == 0 || q == 0) return max(p, q);
  if (l == r) {
    t1[p].val = min(t1[q].val, t1[p].val);
    return p;
  }
  int mid = (l + r) / 2;
  t1[p].lc = merge(t1[p].lc, t1[q].lc, l, mid);
  t1[p].rc = merge(t1[p].rc, t1[q].rc, mid + 1, r);
  co(p);
  return p;
}
int ufs[maxn];
inline int find(int u) {
  if (u == ufs[u])
    return u;
  else
    return ufs[u] = find(ufs[u]);
}
inline bool join(int u, int v) {
  if (find(u) == find(v)) return false;
  ufs[find(u)] = find(v);
  return true;
}
int anc[maxn][22], val[maxn][22], dep[maxn], pre[maxn], pst[maxn], tt;
vector<pair<int, int> > adj[maxn];
int m, n;
int mem[maxn], ise[maxn];
inline void dfs(int u, int fa) {
  pre[u] = ++tt;
  dep[u] = dep[fa] + 1;
  anc[u][0] = fa;
  for (int i = 1; i <= 20; i++) {
    anc[u][i] = anc[anc[u][i - 1]][i - 1];
    val[u][i] = max(val[u][i - 1], val[anc[u][i - 1]][i - 1]);
  }
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    int w = adj[u][i].second;
    if (v == fa) continue;
    val[v][0] = w;
    dfs(v, u);
  }
  pst[u] = tt;
}
inline int calc(int u, int v) {
  int res = 0;
  if (dep[u] < dep[v]) swap(u, v);
  for (int i = 20; i >= 0; i--) {
    if (dep[anc[u][i]] >= dep[v]) {
      res = max(res, val[u][i]);
      u = anc[u][i];
    }
  }
  if (u == v) return res;
  for (int i = 20; i >= 0; i--) {
    if (anc[u][i] != anc[v][i]) {
      res = max(res, val[v][i]);
      res = max(res, val[u][i]);
      u = anc[u][i];
      v = anc[v][i];
    }
  }
  res = max(res, max(val[u][0], val[v][0]));
  return res;
}
vector<pair<int, int> > add[maxn];
int rt[maxn], a[maxn];
inline void dfs1(int u, int fa) {
  for (int i = 0; i < add[u].size(); i++) {
    int tp = add[u][i].first;
    int w = add[u][i].second;
    rt[u] = insert(rt[u], 1, n, tp, w);
  }
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    if (v == fa) continue;
    dfs1(v, u);
    rt[u] = merge(rt[u], rt[v], 1, n);
  }
  a[u] =
      min(qmin(rt[u], 1, n, 1, pre[u] - 1), qmin(rt[u], 1, n, pst[u] + 1, n));
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    ufs[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &b[i].u, &b[i].v, &b[i].w);
    b[i].id = i;
  }
  t1[0].val = inf;
  sort(b + 1, b + m + 1, cmp);
  for (int i = 1; i <= m; i++) {
    int w = b[i].w, u = b[i].u, v = b[i].v;
    if (join(u, v)) {
      adj[u].push_back(make_pair(v, w));
      adj[v].push_back(make_pair(u, w));
      ise[i] = 1;
    }
  }
  dfs(1, 0);
  for (int i = 1; i <= m; i++) {
    if (ise[i] == 0) {
      int u = b[i].u, v = b[i].v, w = b[i].w;
      add[u].push_back(make_pair(pre[v], w));
      add[v].push_back(make_pair(pre[u], w));
    }
  }
  dfs1(1, 0);
  for (int i = 1; i <= m; i++) {
    int u = b[i].u, v = b[i].v;
    if (ise[i]) {
      if (dep[u] < dep[v]) swap(u, v);
      int res = a[u];
      if (res == inf) {
        mem[b[i].id] = -1;
        continue;
      } else {
        mem[b[i].id] = res - 1;
      }
    } else {
      mem[b[i].id] = calc(u, v) - 1;
    }
  }
  for (int i = 1; i <= m; i++) {
    printf("%d ", mem[i]);
  }
}

### 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;
const int maxn = 2e5 + 5;
const int inf = 2e9;
struct edg {
  int u, v, w, id;
  edg(int u = 0, int v = 0, int w = 0, int id = 0) : u(u), v(v), w(w), id(id) {}
};
inline bool cmp(edg a1, edg b1) { return a1.w < b1.w; }
edg b[maxn];
struct node {
  int lc, rc;
  int val;
};
node t1[maxn * 25];
int top;
inline int newn() {
  top++;
  t1[top].val = inf;
  return top;
}
inline void co(int u) { t1[u].val = min(t1[t1[u].lc].val, t1[t1[u].rc].val); }
inline int insert(int u, int l, int r, int id, int x) {
  if (u == 0) u = newn();
  if (l == r) {
    t1[u].val = min(t1[u].val, x);
    return u;
  }
  int mid = (l + r) / 2;
  if (id <= mid)
    t1[u].lc = insert(t1[u].lc, l, mid, id, x);
  else
    t1[u].rc = insert(t1[u].rc, mid + 1, r, id, x);
  co(u);
  return u;
}
inline int qmin(int u, int l, int r, int ql, int qr) {
  if (ql > qr) return inf;
  if (u == 0) return inf;
  if (ql <= l && r <= qr) return t1[u].val;
  int mid = (l + r) / 2, res = inf;
  if (ql <= mid) res = min(res, qmin(t1[u].lc, l, mid, ql, qr));
  if (mid + 1 <= qr) res = min(res, qmin(t1[u].rc, mid + 1, r, ql, qr));
  return res;
}
inline int merge(int p, int q, int l, int r) {
  if (p == 0 || q == 0) return max(p, q);
  if (l == r) {
    t1[p].val = min(t1[q].val, t1[p].val);
    return p;
  }
  int mid = (l + r) / 2;
  t1[p].lc = merge(t1[p].lc, t1[q].lc, l, mid);
  t1[p].rc = merge(t1[p].rc, t1[q].rc, mid + 1, r);
  co(p);
  return p;
}
int ufs[maxn];
inline int find(int u) {
  if (u == ufs[u])
    return u;
  else
    return ufs[u] = find(ufs[u]);
}
inline bool join(int u, int v) {
  if (find(u) == find(v)) return false;
  ufs[find(u)] = find(v);
  return true;
}
int anc[maxn][22], val[maxn][22], dep[maxn], pre[maxn], pst[maxn], tt;
vector<pair<int, int> > adj[maxn];
int m, n;
int mem[maxn], ise[maxn];
inline void dfs(int u, int fa) {
  pre[u] = ++tt;
  dep[u] = dep[fa] + 1;
  anc[u][0] = fa;
  for (int i = 1; i <= 20; i++) {
    anc[u][i] = anc[anc[u][i - 1]][i - 1];
    val[u][i] = max(val[u][i - 1], val[anc[u][i - 1]][i - 1]);
  }
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    int w = adj[u][i].second;
    if (v == fa) continue;
    val[v][0] = w;
    dfs(v, u);
  }
  pst[u] = tt;
}
inline int calc(int u, int v) {
  int res = 0;
  if (dep[u] < dep[v]) swap(u, v);
  for (int i = 20; i >= 0; i--) {
    if (dep[anc[u][i]] >= dep[v]) {
      res = max(res, val[u][i]);
      u = anc[u][i];
    }
  }
  if (u == v) return res;
  for (int i = 20; i >= 0; i--) {
    if (anc[u][i] != anc[v][i]) {
      res = max(res, val[v][i]);
      res = max(res, val[u][i]);
      u = anc[u][i];
      v = anc[v][i];
    }
  }
  res = max(res, max(val[u][0], val[v][0]));
  return res;
}
vector<pair<int, int> > add[maxn];
int rt[maxn], a[maxn];
inline void dfs1(int u, int fa) {
  for (int i = 0; i < add[u].size(); i++) {
    int tp = add[u][i].first;
    int w = add[u][i].second;
    rt[u] = insert(rt[u], 1, n, tp, w);
  }
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i].first;
    if (v == fa) continue;
    dfs1(v, u);
    rt[u] = merge(rt[u], rt[v], 1, n);
  }
  a[u] =
      min(qmin(rt[u], 1, n, 1, pre[u] - 1), qmin(rt[u], 1, n, pst[u] + 1, n));
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    ufs[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &b[i].u, &b[i].v, &b[i].w);
    b[i].id = i;
  }
  t1[0].val = inf;
  sort(b + 1, b + m + 1, cmp);
  for (int i = 1; i <= m; i++) {
    int w = b[i].w, u = b[i].u, v = b[i].v;
    if (join(u, v)) {
      adj[u].push_back(make_pair(v, w));
      adj[v].push_back(make_pair(u, w));
      ise[i] = 1;
    }
  }
  dfs(1, 0);
  for (int i = 1; i <= m; i++) {
    if (ise[i] == 0) {
      int u = b[i].u, v = b[i].v, w = b[i].w;
      add[u].push_back(make_pair(pre[v], w));
      add[v].push_back(make_pair(pre[u], w));
    }
  }
  dfs1(1, 0);
  for (int i = 1; i <= m; i++) {
    int u = b[i].u, v = b[i].v;
    if (ise[i]) {
      if (dep[u] < dep[v]) swap(u, v);
      int res = a[u];
      if (res == inf) {
        mem[b[i].id] = -1;
        continue;
      } else {
        mem[b[i].id] = res - 1;
      }
    } else {
      mem[b[i].id] = calc(u, v) - 1;
    }
  }
  for (int i = 1; i <= m; i++) {
    printf("%d ", mem[i]);
  }
}

```