#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MOD2 = 1007681537;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline long long isqrt(long long k) {
  long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
const int maxn = 2e5 + 5;
const int logn = 20;
int n, m;
vector<pair<int, int> > adj[maxn];
int w[maxn];
int lev[maxn];
int p[logn][maxn];
int val[logn][maxn];
vector<int> events[maxn];
vector<pair<pair<int, int>, int> > edges;
int dj[maxn];
int ans[maxn];
int size[maxn];
int heavy[maxn];
void init() {
  for (int i = (0); i < (n); i++) dj[i] = i;
}
int find(int u) { return dj[u] == u ? dj[u] : dj[u] = find(dj[u]); }
int join(int u, int v) {
  int p = find(u);
  int q = find(v);
  if (p != q) {
    dj[p] = q;
    return 1;
  }
  return 0;
}
void dfs(int u, int dad) {
  for (int i = (1); i < (logn); i++) {
    p[i][u] = p[i - 1][p[i - 1][u]];
    val[i][u] = val[i - 1][u];
    chkmax(val[i][u], val[i - 1][p[i - 1][u]]);
  }
  size[u] = 1;
  heavy[u] = -1;
  for (int i = (0); i < (int((adj[u]).size())); i++) {
    int v = adj[u][i].first;
    int ix = adj[u][i].second;
    if (v != dad) {
      lev[v] = lev[u] + 1;
      p[0][v] = u;
      val[0][v] = w[ix];
      dfs(v, u);
      size[u] += size[v];
      if (heavy[u] == -1 || size[heavy[u]] < size[v]) {
        heavy[u] = v;
      }
    }
  }
}
int lca(int u, int v) {
  if (lev[u] < lev[v]) swap(u, v);
  for (int i = (logn)-1; i >= (0); i--) {
    if (lev[p[i][u]] >= lev[v]) {
      u = p[i][u];
    }
  }
  if (u == v) return u;
  for (int i = (logn)-1; i >= (0); i--) {
    if (p[i][u] != p[i][v]) {
      u = p[i][u];
      v = p[i][v];
    }
  }
  return p[0][u];
}
int query(int u, int a) {
  int res = -INF;
  if (u == a) return res;
  for (int i = (logn)-1; i >= (0); i--) {
    if (lev[p[i][u]] > lev[a]) {
      chkmax(res, val[i][u]);
      u = p[i][u];
    }
  }
  chkmax(res, val[0][u]);
  return res;
}
multiset<int>* upd(int u, int prv) {
  multiset<int>* res;
  if (!~heavy[u]) {
    res = new multiset<int>();
  } else {
    for (int i = (0); i < (int((adj[u]).size())); i++) {
      int v = adj[u][i].first;
      int ix = adj[u][i].second;
      if (v == heavy[u]) {
        res = upd(v, ix);
      }
    }
  }
  for (int i = (0); i < (int((adj[u]).size())); i++) {
    int v = adj[u][i].first;
    int ix = adj[u][i].second;
    if (ix != prv && v != heavy[u]) {
      multiset<int>* r = upd(v, ix);
      for (__typeof((*r).begin()) it = (*r).begin(); it != (*r).end(); it++) {
        res->insert(*it);
      }
    }
  }
  for (int i = (0); i < (int((events[u]).size())); i++) {
    int ix = events[u][i];
    if (ix >= 0) {
      res->insert(w[ix]);
    } else {
      ix = -ix - 1;
      res->erase(res->find(w[ix]));
    }
  }
  if (~prv) {
    ans[prv] = int((*res).size()) ? *res->begin() - 1 : INF + INF;
  }
  return res;
}
void solve() {
  cin >> n >> m;
  priority_queue<pair<pair<int, int>, pair<int, int> > > pq;
  for (int i = (0); i < (m); i++) {
    int u, v;
    cin >> u >> v >> w[i];
    u--, v--;
    pq.push(make_pair(make_pair(-w[i], i), make_pair(u, v)));
  }
  init();
  while (int((pq).size())) {
    int u = pq.top().second.first;
    int v = pq.top().second.second;
    int ix = pq.top().first.second;
    pq.pop();
    if (join(u, v)) {
      adj[u].push_back(make_pair(v, ix));
      adj[v].push_back(make_pair(u, ix));
    } else {
      edges.push_back(make_pair(make_pair(u, v), ix));
    }
  }
  val[0][0] = -INF, dfs(0, -1);
  for (int i = (0); i < (int((edges).size())); i++) {
    int u = edges[i].first.first;
    int v = edges[i].first.second;
    int ix = edges[i].second;
    int a = lca(u, v);
    events[u].push_back(ix);
    events[v].push_back(ix);
    events[a].push_back(-ix - 1);
    events[a].push_back(-ix - 1);
    ans[ix] = max(query(u, a), query(v, a)) - 1;
  }
  upd(0, -1);
  for (int i = (0); i < (m); i++)
    if (ans[i] > INF + 10) ans[i] = -1;
  for (int i = (0); i < (m); i++) cout << ans[i] << " \n"[i == m - 1];
}
int main() {
  int JUDGE_ONLINE = 1;
  if (fopen("in.txt", "r")) {
    JUDGE_ONLINE = 0;
    assert(freopen("in.txt", "r", stdin));
  } else {
    ios_base::sync_with_stdio(0), cin.tie(0);
  }
  solve();
  if (!JUDGE_ONLINE) {
  }
  return 0;
}

### Prompt

Develop a solution in cpp to the problem described below:

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 MOD = (int)1e9 + 7;
const int MOD2 = 1007681537;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline long long isqrt(long long k) {
  long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
const int maxn = 2e5 + 5;
const int logn = 20;
int n, m;
vector<pair<int, int> > adj[maxn];
int w[maxn];
int lev[maxn];
int p[logn][maxn];
int val[logn][maxn];
vector<int> events[maxn];
vector<pair<pair<int, int>, int> > edges;
int dj[maxn];
int ans[maxn];
int size[maxn];
int heavy[maxn];
void init() {
  for (int i = (0); i < (n); i++) dj[i] = i;
}
int find(int u) { return dj[u] == u ? dj[u] : dj[u] = find(dj[u]); }
int join(int u, int v) {
  int p = find(u);
  int q = find(v);
  if (p != q) {
    dj[p] = q;
    return 1;
  }
  return 0;
}
void dfs(int u, int dad) {
  for (int i = (1); i < (logn); i++) {
    p[i][u] = p[i - 1][p[i - 1][u]];
    val[i][u] = val[i - 1][u];
    chkmax(val[i][u], val[i - 1][p[i - 1][u]]);
  }
  size[u] = 1;
  heavy[u] = -1;
  for (int i = (0); i < (int((adj[u]).size())); i++) {
    int v = adj[u][i].first;
    int ix = adj[u][i].second;
    if (v != dad) {
      lev[v] = lev[u] + 1;
      p[0][v] = u;
      val[0][v] = w[ix];
      dfs(v, u);
      size[u] += size[v];
      if (heavy[u] == -1 || size[heavy[u]] < size[v]) {
        heavy[u] = v;
      }
    }
  }
}
int lca(int u, int v) {
  if (lev[u] < lev[v]) swap(u, v);
  for (int i = (logn)-1; i >= (0); i--) {
    if (lev[p[i][u]] >= lev[v]) {
      u = p[i][u];
    }
  }
  if (u == v) return u;
  for (int i = (logn)-1; i >= (0); i--) {
    if (p[i][u] != p[i][v]) {
      u = p[i][u];
      v = p[i][v];
    }
  }
  return p[0][u];
}
int query(int u, int a) {
  int res = -INF;
  if (u == a) return res;
  for (int i = (logn)-1; i >= (0); i--) {
    if (lev[p[i][u]] > lev[a]) {
      chkmax(res, val[i][u]);
      u = p[i][u];
    }
  }
  chkmax(res, val[0][u]);
  return res;
}
multiset<int>* upd(int u, int prv) {
  multiset<int>* res;
  if (!~heavy[u]) {
    res = new multiset<int>();
  } else {
    for (int i = (0); i < (int((adj[u]).size())); i++) {
      int v = adj[u][i].first;
      int ix = adj[u][i].second;
      if (v == heavy[u]) {
        res = upd(v, ix);
      }
    }
  }
  for (int i = (0); i < (int((adj[u]).size())); i++) {
    int v = adj[u][i].first;
    int ix = adj[u][i].second;
    if (ix != prv && v != heavy[u]) {
      multiset<int>* r = upd(v, ix);
      for (__typeof((*r).begin()) it = (*r).begin(); it != (*r).end(); it++) {
        res->insert(*it);
      }
    }
  }
  for (int i = (0); i < (int((events[u]).size())); i++) {
    int ix = events[u][i];
    if (ix >= 0) {
      res->insert(w[ix]);
    } else {
      ix = -ix - 1;
      res->erase(res->find(w[ix]));
    }
  }
  if (~prv) {
    ans[prv] = int((*res).size()) ? *res->begin() - 1 : INF + INF;
  }
  return res;
}
void solve() {
  cin >> n >> m;
  priority_queue<pair<pair<int, int>, pair<int, int> > > pq;
  for (int i = (0); i < (m); i++) {
    int u, v;
    cin >> u >> v >> w[i];
    u--, v--;
    pq.push(make_pair(make_pair(-w[i], i), make_pair(u, v)));
  }
  init();
  while (int((pq).size())) {
    int u = pq.top().second.first;
    int v = pq.top().second.second;
    int ix = pq.top().first.second;
    pq.pop();
    if (join(u, v)) {
      adj[u].push_back(make_pair(v, ix));
      adj[v].push_back(make_pair(u, ix));
    } else {
      edges.push_back(make_pair(make_pair(u, v), ix));
    }
  }
  val[0][0] = -INF, dfs(0, -1);
  for (int i = (0); i < (int((edges).size())); i++) {
    int u = edges[i].first.first;
    int v = edges[i].first.second;
    int ix = edges[i].second;
    int a = lca(u, v);
    events[u].push_back(ix);
    events[v].push_back(ix);
    events[a].push_back(-ix - 1);
    events[a].push_back(-ix - 1);
    ans[ix] = max(query(u, a), query(v, a)) - 1;
  }
  upd(0, -1);
  for (int i = (0); i < (m); i++)
    if (ans[i] > INF + 10) ans[i] = -1;
  for (int i = (0); i < (m); i++) cout << ans[i] << " \n"[i == m - 1];
}
int main() {
  int JUDGE_ONLINE = 1;
  if (fopen("in.txt", "r")) {
    JUDGE_ONLINE = 0;
    assert(freopen("in.txt", "r", stdin));
  } else {
    ios_base::sync_with_stdio(0), cin.tie(0);
  }
  solve();
  if (!JUDGE_ONLINE) {
  }
  return 0;
}

```