#include <bits/stdc++.h>
using namespace std;
int n;
int m;
int belong[200002];
inline int root(int b) {
  if (belong[b] == -1) {
    return b;
  }
  belong[b] = root(belong[b]);
  return belong[b];
}
void merge(int a, int b) {
  a = root(a);
  b = root(b);
  if (a == b) return;
  belong[a] = b;
}
struct st {
  int a;
  int b;
  int c;
  bool flag;
  int id;
};
vector<st> v;
bool cmp(st &a, st &b) { return a.c < b.c; }
int lcc[25][200002];
int dep[200002];
vector<int> g[200002];
int dp[200002];
inline void dfs(int b, int pr = -1, int d = 0) {
  dep[b] = d;
  lcc[0][b] = pr;
  dp[b] = 1;
  for (int i = 0; i < g[b].size(); i++) {
    int go = g[b][i];
    if (go != pr) {
      dfs(go, b, d + 1);
      dp[b] += dp[go];
    }
  }
}
void init_lca() {
  for (int i = 0; i + 1 < 25; i++) {
    for (int j = 0; j < 200002; j++) {
      if (lcc[i][j] == -1) {
        lcc[i + 1][j] = -1;
        continue;
      }
      lcc[i + 1][j] = lcc[i][lcc[i][j]];
    }
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b]) {
    swap(a, b);
  }
  for (int i = 0; i < 25; i++) {
    if (((dep[a] - dep[b]) >> i) & 1) {
      a = lcc[i][a];
    }
  }
  if (a == b) {
    return a;
  }
  for (int i = 25 - 1; i >= 0; i--) {
    if (lcc[i][a] != lcc[i][b]) {
      a = lcc[i][a];
      b = lcc[i][b];
    }
  }
  return lcc[0][a];
}
class MAX_SEG {
  vector<int> seg;
  vector<int> lazy;

 public:
  int sz;
  void resize(int n) {
    sz = n;
    seg.assign(n * 4, 0);
    lazy = seg;
  }
  void update(int b) {
    seg[b] = max(seg[b], lazy[b]);
    if (b * 2 + 2 < lazy.size()) {
      lazy[b * 2 + 1] = max(lazy[b * 2 + 1], lazy[b]);
      lazy[b * 2 + 2] = max(lazy[b * 2 + 2], lazy[b]);
    }
  }
  void add(int b, int l, int r, int ll, int rr, int x) {
    update(b);
    if (r <= ll || rr <= l) {
      return;
    }
    if (ll <= l && r <= rr) {
      lazy[b] = max(lazy[b], x);
      update(b);
      return;
    }
    add(b * 2 + 1, l, (l + r) >> 1, ll, rr, x);
    add(b * 2 + 2, (l + r) >> 1, r, ll, rr, x);
    seg[b] = max(seg[b * 2 + 1], seg[b * 2 + 2]);
  }
  void add(int l, int r, int x) { add(0, 0, sz, l, r, x); }
  inline int query(int b, int l, int r, int ll, int rr) {
    update(b);
    if (r <= ll || rr <= l) {
      return 0;
    }
    if (ll <= l && r <= rr) {
      return seg[b];
    }
    return max(query(b * 2 + 1, l, (l + r) >> 1, ll, rr),
               query(b * 2 + 2, (l + r) >> 1, r, ll, rr));
  }
  int query(int l, int r) { return query(0, 0, sz, l, r); }
};
class MIN_SEG {
  vector<int> seg;
  vector<int> lazy;

 public:
  int sz;
  void resize(int n) {
    seg.assign(n * 4, INT_MAX);
    lazy = seg;
    sz = n;
  }
  void update(int b) {
    seg[b] = min(seg[b], lazy[b]);
    if (b * 2 + 2 < lazy.size()) {
      lazy[b * 2 + 1] = min(lazy[b * 2 + 1], lazy[b]);
      lazy[b * 2 + 2] = min(lazy[b * 2 + 2], lazy[b]);
    }
  }
  void add(int b, int l, int r, int ll, int rr, int x) {
    update(b);
    if (r <= ll || rr <= l) {
      return;
    }
    if (ll <= l && r <= rr) {
      lazy[b] = min(lazy[b], x);
      update(b);
      return;
    }
    add(b * 2 + 1, l, (l + r) >> 1, ll, rr, x);
    add(b * 2 + 2, (l + r) >> 1, r, ll, rr, x);
    seg[b] = min(seg[b * 2 + 1], seg[b * 2 + 2]);
  }
  inline int query(int b, int l, int r, int ll, int rr) {
    update(b);
    if (r <= ll || rr <= l) {
      return INT_MAX;
    }
    if (ll <= l && r <= rr) {
      return seg[b];
    }
    return min(query(b * 2 + 1, l, (l + r) >> 1, ll, rr),
               query(b * 2 + 2, (l + r) >> 1, r, ll, rr));
  }
  void add(int l, int r, int x) { add(0, 0, sz, l, r, x); }
  int query(int l, int r) { return query(0, 0, sz, l, r); }
};
int B[200002];
int A[200002];
struct HL {
  MAX_SEG mx;
  MIN_SEG mi;
};
vector<HL> hl;
int o[200002];
inline void dfs2(int b, int pr = -1, int be = 0, int att = 0) {
  pair<int, int> p = make_pair(-1, -1);
  B[b] = be;
  A[b] = att;
  if (pr != -1 && g[b].size() == 1) {
    hl[be].mi.resize(att + 1);
    hl[be].mx.resize(att + 1);
    return;
  }
  for (int i = 0; i < g[b].size(); i++) {
    int go = g[b][i];
    if (go == pr) continue;
    p = max(p, make_pair(dp[g[b][i]], i));
  }
  for (int i = 0; i < g[b].size(); i++) {
    int go = g[b][i];
    if (go == pr) continue;
    if (i == p.second) {
      dfs2(g[b][i], b, be, att + 1);
    } else {
      hl.push_back(HL());
      o[hl.size() - 1] = b;
      dfs2(g[b][i], b, hl.size() - 1, 0);
    }
  }
}
void path_min(int r, int c, int x) {
  while (B[r] != B[c]) {
    hl[B[c]].mi.add(0, A[c] + 1, x);
    c = o[B[c]];
  }
  if (r != c) hl[B[c]].mi.add(A[r] + 1, A[c] + 1, x);
}
int path_min_sum(int r, int c) {
  int ans = INT_MAX;
  while (B[r] != B[c]) {
    ans = min(ans, hl[B[c]].mi.query(0, A[c] + 1));
    c = o[B[c]];
  }
  if (r != c) {
    ans = min(ans, hl[B[c]].mi.query(A[r] + 1, A[c] + 1));
  }
  return ans;
}
int path_max_sum(int r, int c) {
  int ans = 0;
  while (B[r] != B[c]) {
    ans = max(ans, hl[B[c]].mx.query(0, A[c] + 1));
    c = o[B[c]];
  }
  if (r != c) {
    ans = max(ans, hl[B[c]].mx.query(A[r] + 1, A[c] + 1));
  }
  return ans;
}
int ans[200002];
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    v.push_back({a, b, c, false, i});
  }
  sort(v.begin(), v.end(), cmp);
  memset(belong, -1, sizeof(belong));
  for (int i = 0; i < v.size(); i++) {
    int a = v[i].a;
    int b = v[i].b;
    if (root(a) == root(b)) {
      v[i].flag = false;
      continue;
    }
    merge(a, b);
    v[i].flag = true;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  hl.push_back(HL());
  dfs(0);
  init_lca();
  dfs2(0);
  for (int i = 0; i < v.size(); i++) {
    if (v[i].flag == false) {
      int a = v[i].a;
      int b = v[i].b;
      int lc = lca(a, b);
      path_min(lc, a, v[i].c);
      path_min(lc, b, v[i].c);
    } else {
      int a = v[i].a;
      int b = v[i].b;
      if (dep[a] > dep[b]) {
        swap(a, b);
      }
      hl[B[b]].mx.add(A[b], A[b] + 1, v[i].c);
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (v[i].flag == true) {
      int a = v[i].a;
      int b = v[i].b;
      int lc = lca(a, b);
      int f = min(path_min_sum(lc, a), path_min_sum(lc, b));
      if (f == INT_MAX) {
        ans[v[i].id] = -1;
      } else {
        ans[v[i].id] = f - 1;
      }
      continue;
    } else {
      int a = v[i].a;
      int b = v[i].b;
      int lc = lca(a, b);
      int f = max(path_max_sum(lc, a), path_max_sum(lc, b));
      ans[v[i].id] = f - 1;
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (i) printf(" ");
    printf("%d", ans[i]);
  }
  puts("");
  return 0;
}

### Prompt

In CPP, your task is to solve 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 n;
int m;
int belong[200002];
inline int root(int b) {
  if (belong[b] == -1) {
    return b;
  }
  belong[b] = root(belong[b]);
  return belong[b];
}
void merge(int a, int b) {
  a = root(a);
  b = root(b);
  if (a == b) return;
  belong[a] = b;
}
struct st {
  int a;
  int b;
  int c;
  bool flag;
  int id;
};
vector<st> v;
bool cmp(st &a, st &b) { return a.c < b.c; }
int lcc[25][200002];
int dep[200002];
vector<int> g[200002];
int dp[200002];
inline void dfs(int b, int pr = -1, int d = 0) {
  dep[b] = d;
  lcc[0][b] = pr;
  dp[b] = 1;
  for (int i = 0; i < g[b].size(); i++) {
    int go = g[b][i];
    if (go != pr) {
      dfs(go, b, d + 1);
      dp[b] += dp[go];
    }
  }
}
void init_lca() {
  for (int i = 0; i + 1 < 25; i++) {
    for (int j = 0; j < 200002; j++) {
      if (lcc[i][j] == -1) {
        lcc[i + 1][j] = -1;
        continue;
      }
      lcc[i + 1][j] = lcc[i][lcc[i][j]];
    }
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b]) {
    swap(a, b);
  }
  for (int i = 0; i < 25; i++) {
    if (((dep[a] - dep[b]) >> i) & 1) {
      a = lcc[i][a];
    }
  }
  if (a == b) {
    return a;
  }
  for (int i = 25 - 1; i >= 0; i--) {
    if (lcc[i][a] != lcc[i][b]) {
      a = lcc[i][a];
      b = lcc[i][b];
    }
  }
  return lcc[0][a];
}
class MAX_SEG {
  vector<int> seg;
  vector<int> lazy;

 public:
  int sz;
  void resize(int n) {
    sz = n;
    seg.assign(n * 4, 0);
    lazy = seg;
  }
  void update(int b) {
    seg[b] = max(seg[b], lazy[b]);
    if (b * 2 + 2 < lazy.size()) {
      lazy[b * 2 + 1] = max(lazy[b * 2 + 1], lazy[b]);
      lazy[b * 2 + 2] = max(lazy[b * 2 + 2], lazy[b]);
    }
  }
  void add(int b, int l, int r, int ll, int rr, int x) {
    update(b);
    if (r <= ll || rr <= l) {
      return;
    }
    if (ll <= l && r <= rr) {
      lazy[b] = max(lazy[b], x);
      update(b);
      return;
    }
    add(b * 2 + 1, l, (l + r) >> 1, ll, rr, x);
    add(b * 2 + 2, (l + r) >> 1, r, ll, rr, x);
    seg[b] = max(seg[b * 2 + 1], seg[b * 2 + 2]);
  }
  void add(int l, int r, int x) { add(0, 0, sz, l, r, x); }
  inline int query(int b, int l, int r, int ll, int rr) {
    update(b);
    if (r <= ll || rr <= l) {
      return 0;
    }
    if (ll <= l && r <= rr) {
      return seg[b];
    }
    return max(query(b * 2 + 1, l, (l + r) >> 1, ll, rr),
               query(b * 2 + 2, (l + r) >> 1, r, ll, rr));
  }
  int query(int l, int r) { return query(0, 0, sz, l, r); }
};
class MIN_SEG {
  vector<int> seg;
  vector<int> lazy;

 public:
  int sz;
  void resize(int n) {
    seg.assign(n * 4, INT_MAX);
    lazy = seg;
    sz = n;
  }
  void update(int b) {
    seg[b] = min(seg[b], lazy[b]);
    if (b * 2 + 2 < lazy.size()) {
      lazy[b * 2 + 1] = min(lazy[b * 2 + 1], lazy[b]);
      lazy[b * 2 + 2] = min(lazy[b * 2 + 2], lazy[b]);
    }
  }
  void add(int b, int l, int r, int ll, int rr, int x) {
    update(b);
    if (r <= ll || rr <= l) {
      return;
    }
    if (ll <= l && r <= rr) {
      lazy[b] = min(lazy[b], x);
      update(b);
      return;
    }
    add(b * 2 + 1, l, (l + r) >> 1, ll, rr, x);
    add(b * 2 + 2, (l + r) >> 1, r, ll, rr, x);
    seg[b] = min(seg[b * 2 + 1], seg[b * 2 + 2]);
  }
  inline int query(int b, int l, int r, int ll, int rr) {
    update(b);
    if (r <= ll || rr <= l) {
      return INT_MAX;
    }
    if (ll <= l && r <= rr) {
      return seg[b];
    }
    return min(query(b * 2 + 1, l, (l + r) >> 1, ll, rr),
               query(b * 2 + 2, (l + r) >> 1, r, ll, rr));
  }
  void add(int l, int r, int x) { add(0, 0, sz, l, r, x); }
  int query(int l, int r) { return query(0, 0, sz, l, r); }
};
int B[200002];
int A[200002];
struct HL {
  MAX_SEG mx;
  MIN_SEG mi;
};
vector<HL> hl;
int o[200002];
inline void dfs2(int b, int pr = -1, int be = 0, int att = 0) {
  pair<int, int> p = make_pair(-1, -1);
  B[b] = be;
  A[b] = att;
  if (pr != -1 && g[b].size() == 1) {
    hl[be].mi.resize(att + 1);
    hl[be].mx.resize(att + 1);
    return;
  }
  for (int i = 0; i < g[b].size(); i++) {
    int go = g[b][i];
    if (go == pr) continue;
    p = max(p, make_pair(dp[g[b][i]], i));
  }
  for (int i = 0; i < g[b].size(); i++) {
    int go = g[b][i];
    if (go == pr) continue;
    if (i == p.second) {
      dfs2(g[b][i], b, be, att + 1);
    } else {
      hl.push_back(HL());
      o[hl.size() - 1] = b;
      dfs2(g[b][i], b, hl.size() - 1, 0);
    }
  }
}
void path_min(int r, int c, int x) {
  while (B[r] != B[c]) {
    hl[B[c]].mi.add(0, A[c] + 1, x);
    c = o[B[c]];
  }
  if (r != c) hl[B[c]].mi.add(A[r] + 1, A[c] + 1, x);
}
int path_min_sum(int r, int c) {
  int ans = INT_MAX;
  while (B[r] != B[c]) {
    ans = min(ans, hl[B[c]].mi.query(0, A[c] + 1));
    c = o[B[c]];
  }
  if (r != c) {
    ans = min(ans, hl[B[c]].mi.query(A[r] + 1, A[c] + 1));
  }
  return ans;
}
int path_max_sum(int r, int c) {
  int ans = 0;
  while (B[r] != B[c]) {
    ans = max(ans, hl[B[c]].mx.query(0, A[c] + 1));
    c = o[B[c]];
  }
  if (r != c) {
    ans = max(ans, hl[B[c]].mx.query(A[r] + 1, A[c] + 1));
  }
  return ans;
}
int ans[200002];
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    v.push_back({a, b, c, false, i});
  }
  sort(v.begin(), v.end(), cmp);
  memset(belong, -1, sizeof(belong));
  for (int i = 0; i < v.size(); i++) {
    int a = v[i].a;
    int b = v[i].b;
    if (root(a) == root(b)) {
      v[i].flag = false;
      continue;
    }
    merge(a, b);
    v[i].flag = true;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  hl.push_back(HL());
  dfs(0);
  init_lca();
  dfs2(0);
  for (int i = 0; i < v.size(); i++) {
    if (v[i].flag == false) {
      int a = v[i].a;
      int b = v[i].b;
      int lc = lca(a, b);
      path_min(lc, a, v[i].c);
      path_min(lc, b, v[i].c);
    } else {
      int a = v[i].a;
      int b = v[i].b;
      if (dep[a] > dep[b]) {
        swap(a, b);
      }
      hl[B[b]].mx.add(A[b], A[b] + 1, v[i].c);
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (v[i].flag == true) {
      int a = v[i].a;
      int b = v[i].b;
      int lc = lca(a, b);
      int f = min(path_min_sum(lc, a), path_min_sum(lc, b));
      if (f == INT_MAX) {
        ans[v[i].id] = -1;
      } else {
        ans[v[i].id] = f - 1;
      }
      continue;
    } else {
      int a = v[i].a;
      int b = v[i].b;
      int lc = lca(a, b);
      int f = max(path_max_sum(lc, a), path_max_sum(lc, b));
      ans[v[i].id] = f - 1;
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (i) printf(" ");
    printf("%d", ans[i]);
  }
  puts("");
  return 0;
}

```