#include <bits/stdc++.h>
using namespace std;
const int M = 200010;
struct segment_tree {
  int tree[4 * M];
  segment_tree() {}
  void build() { fill(tree, tree + 4 * M, 1000000011); }
  void update(int i, int l, int r, int st, int ed, int val) {
    if (l >= st && r <= ed)
      tree[i] = min(tree[i], val);
    else if (l > ed || r < st)
      return;
    else {
      int m = (l + r) / 2;
      update(2 * i, l, m, st, ed, val);
      update(2 * i + 1, m + 1, r, st, ed, val);
    }
  }
  int query(int i, int l, int r, int p, int carry = 1000000011) {
    if (l == r && l == p)
      return min(carry, tree[i]);
    else if (l <= p && r >= p) {
      int m = (l + r) / 2, a = query(2 * i, l, m, p, min(carry, tree[i])),
          b = query(2 * i + 1, m + 1, r, p, min(carry, tree[i]));
      return min(a, b);
    } else
      return 1000000011;
  }
};
struct HLD {
  int N, nC, nP;
  vector<int> Edge[M];
  int SubT[M], Par[M], Level[M];
  int ChainLdr[M], Chain[M], Pos[M];
  segment_tree t;
  HLD(int N = 0) {
    this->N = N;
    nC = nP = 0;
    t.build();
  }
  void clear(int N = 0) {
    for (int i = 0; i < M; i++) Edge[i].clear();
    this->N = N;
    nC = nP = 0;
    t.build();
  }
  void addEdge(int u, int v) {
    Edge[u].push_back(v);
    Edge[v].push_back(u);
  }
  int Explore(int u, int p, int l) {
    SubT[u] = 1;
    Par[u] = p;
    Level[u] = l;
    for (int i = 0; i < Edge[u].size(); i++) {
      int v = Edge[u][i];
      if (p == v) continue;
      SubT[u] += Explore(v, u, l + 1);
    }
    return SubT[u];
  }
  void HeavyLight(int u, int k, bool isHead) {
    if (isHead) k = ++nC, ChainLdr[k] = u;
    Chain[u] = k;
    Pos[u] = ++nP;
    int mx = -1;
    for (int i = 0; i < Edge[u].size(); i++) {
      int v = Edge[u][i];
      if (Par[u] == v) continue;
      if (mx == -1)
        mx = v;
      else if (SubT[v] > SubT[mx])
        mx = v;
    }
    if (mx == -1) return;
    HeavyLight(mx, k, false);
    for (int i = 0; i < Edge[u].size(); i++) {
      int v = Edge[u][i];
      if (Par[u] == v || mx == v) continue;
      HeavyLight(v, 0, true);
    }
  }
  void preprocess(int u = 0) {
    Explore(u, -1, 0);
    HeavyLight(u, 0, true);
  }
  int LCA(int u, int v) {
    while (Chain[u] != Chain[v]) {
      if (Level[ChainLdr[Chain[u]]] < Level[ChainLdr[Chain[v]]])
        v = Par[ChainLdr[Chain[v]]];
      else
        u = Par[ChainLdr[Chain[u]]];
    }
    if (Level[u] < Level[v])
      return u;
    else
      return v;
  }
  void update(int u, int v, int val) {
    while (Chain[u] != Chain[v]) {
      if (Level[ChainLdr[Chain[u]]] < Level[ChainLdr[Chain[v]]]) {
        t.update(1, 0, N, Pos[ChainLdr[Chain[v]]], Pos[v], val);
        v = Par[ChainLdr[Chain[v]]];
      } else {
        t.update(1, 0, N, Pos[ChainLdr[Chain[u]]], Pos[u], val);
        u = Par[ChainLdr[Chain[u]]];
      }
    }
    if (Level[u] < Level[v])
      t.update(1, 0, N, Pos[u] + 1, Pos[v], val);
    else if (Level[v] < Level[u])
      t.update(1, 0, N, Pos[v] + 1, Pos[u], val);
  }
  int query(int u, int v) {
    if (Level[u] < Level[v])
      return t.query(1, 0, N, Pos[v]);
    else
      return t.query(1, 0, N, Pos[u]);
  }
};
HLD H(M);
struct data {
  int u, v, cost, id;
  data(int a = 0, int b = 0, int c = 0) { u = a, v = b, cost = c; }
  bool operator<(const data &d) const { return cost < d.cost; }
};
vector<data> E;
int parent[M];
map<pair<int, int>, int> inMST, isbridge;
int f(int u) { return parent[u] = parent[u] == u ? u : f(parent[u]); }
bool Add(int first, int second) {
  int u = f(first), v = f(second);
  if (u == v) return false;
  parent[u] = v;
  return true;
}
vector<int> A[M];
bool visited[M];
int disc[M], low[M], bpar[M];
void find_bridges(int u) {
  static int time = 0;
  visited[u] = true;
  disc[u] = low[u] = ++time;
  int i, v;
  for (i = 0; i < A[u].size(); i++) {
    v = A[u][i];
    if (!visited[v]) {
      bpar[v] = u;
      find_bridges(v);
      low[u] = min(low[u], low[v]);
      if (low[v] > disc[u]) isbridge[make_pair(min(u, v), max(u, v))] = 1;
    } else if (v != bpar[u])
      low[u] = min(low[u], disc[v]);
  }
}
const int LOGN = 21;
int par[M][LOGN], MAX[M][LOGN], level[M];
vector<int> W[M];
void initialize(int n) {
  memset((par), (-1), sizeof(par));
  memset((MAX), (-1), sizeof(MAX));
  memset((visited), (false), sizeof(visited));
}
void dfs(int n, int l) {
  int m = A[n].size(), i, v;
  visited[n] = true;
  level[n] = l;
  for (i = 0; i < m; i++) {
    v = A[n][i];
    if (!visited[v]) {
      par[v][0] = n;
      MAX[v][0] = W[n][i];
      dfs(v, l + 1);
    }
  }
}
void make_par(int n) {
  int i, j;
  for (i = 1; i < LOGN; i++) {
    for (j = 1; j <= n; j++) {
      if (par[j][i - 1] != -1) {
        par[j][i] = par[par[j][i - 1]][i - 1];
        MAX[j][i] = max(MAX[j][i - 1], MAX[par[j][i - 1]][i - 1]);
      }
    }
  }
}
int max_in_path(int p, int q) {
  int i, j, temp, maxx = 0;
  if (level[p] < level[q]) swap(p, q);
  for (i = LOGN - 1; i >= 0; i--) {
    if (level[p] - (1 << i) >= level[q]) {
      maxx = max(maxx, MAX[p][i]);
      p = par[p][i];
    }
  }
  if (p == q) return maxx;
  for (i = LOGN - 1; i >= 0; i--) {
    if (par[p][i] != -1 && par[p][i] != par[q][i]) {
      maxx = max(maxx, MAX[p][i]);
      maxx = max(maxx, MAX[q][i]);
      p = par[p][i];
      q = par[q][i];
    }
  }
  return max(maxx, max(MAX[p][0], MAX[q][0]));
}
int ans[M];
int main() {
  ios_base::sync_with_stdio(false);
  int n, m, i, j, k, q;
  scanf("%d%d", &n, &m);
  inMST.clear();
  E.clear();
  H.clear(n);
  for (i = 0; i <= n; i++) parent[i] = i;
  for (int pp = 0; pp < m; pp++) {
    scanf("%d%d%d", &i, &j, &k);
    A[i].push_back(j);
    A[j].push_back(i);
    E.push_back(data(i, j, k));
    E.back().id = pp;
  }
  memset((visited), (0), sizeof(visited));
  memset((bpar), (-1), sizeof(bpar));
  find_bridges(1);
  for (i = 1; i <= n; i++) A[i].clear();
  int sum = 0;
  sort((E).begin(), (E).end());
  for (i = 0; i < E.size(); i++) {
    if (Add(E[i].u, E[i].v)) {
      int u = E[i].u, v = E[i].v, w = E[i].cost;
      H.addEdge(u, v);
      inMST[make_pair(min(u, v), max(u, v))] = 1;
      A[u].push_back(v);
      A[v].push_back(u);
      W[u].push_back(w);
      W[v].push_back(w);
    }
  }
  initialize(n);
  dfs(1, 0);
  make_par(n);
  H.preprocess(1);
  for (i = 0; i < E.size(); i++) {
    if (inMST[make_pair(min(E[i].u, E[i].v), max(E[i].u, E[i].v))] == 0)
      H.update(E[i].u, E[i].v, E[i].cost);
  }
  for (auto e : E) {
    i = e.u;
    j = e.v;
    k = e.cost;
    int id = e.id;
    if (isbridge[make_pair(min(i, j), max(i, j))]) {
      ans[id] = -1;
    } else if (inMST[make_pair(min(i, j), max(i, j))]) {
      int w = H.query(i, j);
      ans[id] = w - 1;
    } else {
      ans[id] = max_in_path(i, j) - 1;
    }
  }
  for (i = 0; i < m; i++) printf("%d ", ans[i]);
  printf("\n");
  return 0;
}

### 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 M = 200010;
struct segment_tree {
  int tree[4 * M];
  segment_tree() {}
  void build() { fill(tree, tree + 4 * M, 1000000011); }
  void update(int i, int l, int r, int st, int ed, int val) {
    if (l >= st && r <= ed)
      tree[i] = min(tree[i], val);
    else if (l > ed || r < st)
      return;
    else {
      int m = (l + r) / 2;
      update(2 * i, l, m, st, ed, val);
      update(2 * i + 1, m + 1, r, st, ed, val);
    }
  }
  int query(int i, int l, int r, int p, int carry = 1000000011) {
    if (l == r && l == p)
      return min(carry, tree[i]);
    else if (l <= p && r >= p) {
      int m = (l + r) / 2, a = query(2 * i, l, m, p, min(carry, tree[i])),
          b = query(2 * i + 1, m + 1, r, p, min(carry, tree[i]));
      return min(a, b);
    } else
      return 1000000011;
  }
};
struct HLD {
  int N, nC, nP;
  vector<int> Edge[M];
  int SubT[M], Par[M], Level[M];
  int ChainLdr[M], Chain[M], Pos[M];
  segment_tree t;
  HLD(int N = 0) {
    this->N = N;
    nC = nP = 0;
    t.build();
  }
  void clear(int N = 0) {
    for (int i = 0; i < M; i++) Edge[i].clear();
    this->N = N;
    nC = nP = 0;
    t.build();
  }
  void addEdge(int u, int v) {
    Edge[u].push_back(v);
    Edge[v].push_back(u);
  }
  int Explore(int u, int p, int l) {
    SubT[u] = 1;
    Par[u] = p;
    Level[u] = l;
    for (int i = 0; i < Edge[u].size(); i++) {
      int v = Edge[u][i];
      if (p == v) continue;
      SubT[u] += Explore(v, u, l + 1);
    }
    return SubT[u];
  }
  void HeavyLight(int u, int k, bool isHead) {
    if (isHead) k = ++nC, ChainLdr[k] = u;
    Chain[u] = k;
    Pos[u] = ++nP;
    int mx = -1;
    for (int i = 0; i < Edge[u].size(); i++) {
      int v = Edge[u][i];
      if (Par[u] == v) continue;
      if (mx == -1)
        mx = v;
      else if (SubT[v] > SubT[mx])
        mx = v;
    }
    if (mx == -1) return;
    HeavyLight(mx, k, false);
    for (int i = 0; i < Edge[u].size(); i++) {
      int v = Edge[u][i];
      if (Par[u] == v || mx == v) continue;
      HeavyLight(v, 0, true);
    }
  }
  void preprocess(int u = 0) {
    Explore(u, -1, 0);
    HeavyLight(u, 0, true);
  }
  int LCA(int u, int v) {
    while (Chain[u] != Chain[v]) {
      if (Level[ChainLdr[Chain[u]]] < Level[ChainLdr[Chain[v]]])
        v = Par[ChainLdr[Chain[v]]];
      else
        u = Par[ChainLdr[Chain[u]]];
    }
    if (Level[u] < Level[v])
      return u;
    else
      return v;
  }
  void update(int u, int v, int val) {
    while (Chain[u] != Chain[v]) {
      if (Level[ChainLdr[Chain[u]]] < Level[ChainLdr[Chain[v]]]) {
        t.update(1, 0, N, Pos[ChainLdr[Chain[v]]], Pos[v], val);
        v = Par[ChainLdr[Chain[v]]];
      } else {
        t.update(1, 0, N, Pos[ChainLdr[Chain[u]]], Pos[u], val);
        u = Par[ChainLdr[Chain[u]]];
      }
    }
    if (Level[u] < Level[v])
      t.update(1, 0, N, Pos[u] + 1, Pos[v], val);
    else if (Level[v] < Level[u])
      t.update(1, 0, N, Pos[v] + 1, Pos[u], val);
  }
  int query(int u, int v) {
    if (Level[u] < Level[v])
      return t.query(1, 0, N, Pos[v]);
    else
      return t.query(1, 0, N, Pos[u]);
  }
};
HLD H(M);
struct data {
  int u, v, cost, id;
  data(int a = 0, int b = 0, int c = 0) { u = a, v = b, cost = c; }
  bool operator<(const data &d) const { return cost < d.cost; }
};
vector<data> E;
int parent[M];
map<pair<int, int>, int> inMST, isbridge;
int f(int u) { return parent[u] = parent[u] == u ? u : f(parent[u]); }
bool Add(int first, int second) {
  int u = f(first), v = f(second);
  if (u == v) return false;
  parent[u] = v;
  return true;
}
vector<int> A[M];
bool visited[M];
int disc[M], low[M], bpar[M];
void find_bridges(int u) {
  static int time = 0;
  visited[u] = true;
  disc[u] = low[u] = ++time;
  int i, v;
  for (i = 0; i < A[u].size(); i++) {
    v = A[u][i];
    if (!visited[v]) {
      bpar[v] = u;
      find_bridges(v);
      low[u] = min(low[u], low[v]);
      if (low[v] > disc[u]) isbridge[make_pair(min(u, v), max(u, v))] = 1;
    } else if (v != bpar[u])
      low[u] = min(low[u], disc[v]);
  }
}
const int LOGN = 21;
int par[M][LOGN], MAX[M][LOGN], level[M];
vector<int> W[M];
void initialize(int n) {
  memset((par), (-1), sizeof(par));
  memset((MAX), (-1), sizeof(MAX));
  memset((visited), (false), sizeof(visited));
}
void dfs(int n, int l) {
  int m = A[n].size(), i, v;
  visited[n] = true;
  level[n] = l;
  for (i = 0; i < m; i++) {
    v = A[n][i];
    if (!visited[v]) {
      par[v][0] = n;
      MAX[v][0] = W[n][i];
      dfs(v, l + 1);
    }
  }
}
void make_par(int n) {
  int i, j;
  for (i = 1; i < LOGN; i++) {
    for (j = 1; j <= n; j++) {
      if (par[j][i - 1] != -1) {
        par[j][i] = par[par[j][i - 1]][i - 1];
        MAX[j][i] = max(MAX[j][i - 1], MAX[par[j][i - 1]][i - 1]);
      }
    }
  }
}
int max_in_path(int p, int q) {
  int i, j, temp, maxx = 0;
  if (level[p] < level[q]) swap(p, q);
  for (i = LOGN - 1; i >= 0; i--) {
    if (level[p] - (1 << i) >= level[q]) {
      maxx = max(maxx, MAX[p][i]);
      p = par[p][i];
    }
  }
  if (p == q) return maxx;
  for (i = LOGN - 1; i >= 0; i--) {
    if (par[p][i] != -1 && par[p][i] != par[q][i]) {
      maxx = max(maxx, MAX[p][i]);
      maxx = max(maxx, MAX[q][i]);
      p = par[p][i];
      q = par[q][i];
    }
  }
  return max(maxx, max(MAX[p][0], MAX[q][0]));
}
int ans[M];
int main() {
  ios_base::sync_with_stdio(false);
  int n, m, i, j, k, q;
  scanf("%d%d", &n, &m);
  inMST.clear();
  E.clear();
  H.clear(n);
  for (i = 0; i <= n; i++) parent[i] = i;
  for (int pp = 0; pp < m; pp++) {
    scanf("%d%d%d", &i, &j, &k);
    A[i].push_back(j);
    A[j].push_back(i);
    E.push_back(data(i, j, k));
    E.back().id = pp;
  }
  memset((visited), (0), sizeof(visited));
  memset((bpar), (-1), sizeof(bpar));
  find_bridges(1);
  for (i = 1; i <= n; i++) A[i].clear();
  int sum = 0;
  sort((E).begin(), (E).end());
  for (i = 0; i < E.size(); i++) {
    if (Add(E[i].u, E[i].v)) {
      int u = E[i].u, v = E[i].v, w = E[i].cost;
      H.addEdge(u, v);
      inMST[make_pair(min(u, v), max(u, v))] = 1;
      A[u].push_back(v);
      A[v].push_back(u);
      W[u].push_back(w);
      W[v].push_back(w);
    }
  }
  initialize(n);
  dfs(1, 0);
  make_par(n);
  H.preprocess(1);
  for (i = 0; i < E.size(); i++) {
    if (inMST[make_pair(min(E[i].u, E[i].v), max(E[i].u, E[i].v))] == 0)
      H.update(E[i].u, E[i].v, E[i].cost);
  }
  for (auto e : E) {
    i = e.u;
    j = e.v;
    k = e.cost;
    int id = e.id;
    if (isbridge[make_pair(min(i, j), max(i, j))]) {
      ans[id] = -1;
    } else if (inMST[make_pair(min(i, j), max(i, j))]) {
      int w = H.query(i, j);
      ans[id] = w - 1;
    } else {
      ans[id] = max_in_path(i, j) - 1;
    }
  }
  for (i = 0; i < m; i++) printf("%d ", ans[i]);
  printf("\n");
  return 0;
}

```