#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define pi pair<int,int>
#define f first
#define s second
#define fr freopen("in.txt","r",stdin)
#define ll long long
#define pb push_back
struct Edge
{
    int u, v, c, ind;
    Edge(){}
    Edge(int _u, int _v, int _c, int _ind) {
        u = _u;
        v = _v;
        c = _c;
        ind = _ind;
    }
};
bool MSTEdge[200011];
int pa[200011];
int sz[200011];
Edge edge[200011];
int n, m;
int ans[200011];
bool func(Edge a, Edge b) {
    return a.c < b.c;
}
int f(int x) {
    if(x == pa[x]) return x;
    return pa[x] = f(pa[x]);
}
bool join(int u, int v) {
    u = f(u);
    v = f(v);
    if(u != v) {
        if(sz[u] < sz[v]) swap(u, v);
        sz[u] += sz[v];
        pa[v] = u;
        return 1;
    }
    return 0;
}
vector<pi> tree[200011];
vector<pi> badGraph[200011];
void makeSpanningTree() {
    vector<Edge> E;
    rep(i, m) {
        E.push_back(edge[i]);
    }
    for(int i=1;i<=n;i++) {
        pa[i] = i;
        sz[i] = 1;
    }
    sort(E.begin(), E.end(), func);
    rep(i, m) {
        if(join(E[i].u, E[i].v)) {
            MSTEdge[E[i].ind] = 1;
            tree[E[i].u].push_back(make_pair(E[i].v, E[i].ind));
            tree[E[i].v].push_back(make_pair(E[i].u, E[i].ind));
        }
        else{
            MSTEdge[E[i].ind] = 0;
            badGraph[E[i].u].push_back(make_pair(E[i].v, E[i].ind));
            badGraph[E[i].v].push_back(make_pair(E[i].u, E[i].ind));
        }
    }
}
int dp[200011][20];
int sdp[200011][20];
int d[200011];
void init(int v,int p, int w) {
    if(p != -1) {
        d[v] = d[p]+1;
        dp[v][0] = p;
        sdp[v][0] = w;
    }
    rep(i, tree[v].size()) {
        if(tree[v][i].f != p) {
            init(tree[v][i].f, v, edge[tree[v][i].s].c);
        }
    }
}
void build() {
    for(int j=1;j<20;j++) {
        for(int i=1; i<=n ;i++) {
            dp[i][j] = dp[dp[i][j-1]][j-1];
            sdp[i][j] = max(sdp[i][j-1], sdp[dp[i][j-1]][j-1]);
        }
    }
}
int LCA(int u, int v) {
    if(d[u] < d[v]) swap(u, v);
    int diff = d[u] - d[v];
    rep(i, 20) {
        if(diff&(1<<i)) u = dp[u][i];
    }
    if(u != v) {
        for(int i=19;i>=0;i--) {
            if(dp[u][i] != dp[v][i]) {
                u = dp[u][i];
                v = dp[v][i];
            }
        }
        u = dp[u][0];
    }
    return u;
}
int maxValue(int L, int u) {
    int diff = d[u] - d[L];
    int ans = 0;
    rep(i,20) {
        if(diff&(1<<i)) {
            ans = max(ans, sdp[u][i]);
            u = dp[u][i];
        }
    }
    return ans;
}
int maxPathValue(int u, int v) {
    int L = LCA(u, v);
    return max(maxValue(L, u), maxValue(L, v));
}
void calcForNonMSTEdges() {
    rep(i, m) {
        if(!MSTEdge[i]) {
            ans[i] = maxPathValue(edge[i].u, edge[i].v)-1;
        }
    }
}
bool del[200011];
int tot = 0;
std::vector<int> V;
void dfs(int v, int p) {
    sz[v] = 1;
    tot++;
    V.pb(v);
    rep(i, tree[v].size()) {
        if(!del[tree[v][i].f] and tree[v][i].f != p) {
            dfs(tree[v][i].f, v);
            sz[v] += sz[tree[v][i].f];
        }
    }
}
int centroid(int v, int p) {
    int mx = -1;
    rep(i, tree[v].size()) {
        if(tree[v][i].f != p and !del[tree[v][i].f]) {
            if(mx == -1) {
                mx = tree[v][i].f;
            }
            else{
                if(sz[mx] < sz[tree[v][i].f]) mx = tree[v][i].f;
            }
        }
    }
    if(mx ==-1 or sz[mx] <= tot/2) return v;
    return centroid(mx, v);
}
bool mark[200011];
void calcForMSTEdges(int v, int id) {

    tot = 0;
    V.clear();
    dfs(v, -1);

    if(id != -1) {
        rep(i, V.size()) {
            mark[V[i]] = 1;
        }
        rep(i, V.size()) {
            int x = V[i];
            rep(j, badGraph[x].size()) {
                if(!mark[badGraph[x][j].f]) {
                    ans[id] = min(ans[id],edge[badGraph[x][j].s].c-1);
                }
            }
        }
        rep(i, V.size()) {
            mark[V[i]] = 0;
        }

    }
    int root = centroid(v,-1);
    del[root] = 1;
    rep(i, tree[root].size()) {
        if(!del[tree[root][i].f]) {
            calcForMSTEdges(tree[root][i].f, tree[root][i].s);
        }
    }
}
int main() {

    cin >> n >> m;
    int u, v, c;
    rep(i, m) {
        cin >> edge[i].u >>  edge[i].v >> edge[i].c;
        edge[i].ind = i;
    }
    makeSpanningTree();
    init(1, -1, -1);
    build();
    calcForNonMSTEdges();
    rep(i, m) {
        if(MSTEdge[i]) ans[i] = INT_MAX;
    }
    calcForMSTEdges(1,-1);
    rep(i, m) {
        if(ans[i] == INT_MAX) ans[i] = -1;
        cout << ans[i] << " ";
    }
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define pi pair<int,int>
#define f first
#define s second
#define fr freopen("in.txt","r",stdin)
#define ll long long
#define pb push_back
struct Edge
{
    int u, v, c, ind;
    Edge(){}
    Edge(int _u, int _v, int _c, int _ind) {
        u = _u;
        v = _v;
        c = _c;
        ind = _ind;
    }
};

bool MSTEdge[200011];
int pa[200011];
int sz[200011];
Edge edge[200011];
int n, m;
int ans[200011];
bool func(Edge a, Edge b) {
    return a.c < b.c;
}
int f(int x) {
    if(x == pa[x]) return x;
    return pa[x] = f(pa[x]);
}
bool join(int u, int v) {
    u = f(u);
    v = f(v);
    if(u != v) {
        if(sz[u] < sz[v]) swap(u, v);
        sz[u] += sz[v];
        pa[v] = u;
        return 1;
    }
    return 0;
}

vector<pi> tree[200011];
vector<pi> badGraph[200011];

void makeSpanningTree() {
    vector<Edge> E;
    rep(i, m) {
        E.push_back(edge[i]);
    }
    for(int i=1;i<=n;i++) {
        pa[i] = i;
        sz[i] = 1;
    }
    sort(E.begin(), E.end(), func);
    rep(i, m) {
        if(join(E[i].u, E[i].v)) {
            MSTEdge[E[i].ind] = 1;
            tree[E[i].u].push_back(make_pair(E[i].v, E[i].ind));
            tree[E[i].v].push_back(make_pair(E[i].u, E[i].ind));
        }
        else{
            MSTEdge[E[i].ind] = 0;
            badGraph[E[i].u].push_back(make_pair(E[i].v, E[i].ind));
            badGraph[E[i].v].push_back(make_pair(E[i].u, E[i].ind));
        }
    }
}

int dp[200011][20];
int sdp[200011][20];
int d[200011];

void init(int v,int p, int w) {
    if(p != -1) {
        d[v] = d[p]+1;
        dp[v][0] = p;
        sdp[v][0] = w;
    }
    rep(i, tree[v].size()) {
        if(tree[v][i].f != p) {
            init(tree[v][i].f, v, edge[tree[v][i].s].c);
        }
    }
}
void build() {
    for(int j=1;j<20;j++) {
        for(int i=1; i<=n ;i++) {
            dp[i][j] = dp[dp[i][j-1]][j-1];
            sdp[i][j] = max(sdp[i][j-1], sdp[dp[i][j-1]][j-1]);
        }
    }
}
int LCA(int u, int v) {
    if(d[u] < d[v]) swap(u, v);
    int diff = d[u] - d[v];
    rep(i, 20) {
        if(diff&(1<<i)) u = dp[u][i];
    }
    if(u != v) {
        for(int i=19;i>=0;i--) {
            if(dp[u][i] != dp[v][i]) {
                u = dp[u][i];
                v = dp[v][i];
            }
        }
        u = dp[u][0];
    }
    return u;
}
int maxValue(int L, int u) {
    int diff = d[u] - d[L];
    int ans = 0;
    rep(i,20) {
        if(diff&(1<<i)) {
            ans = max(ans, sdp[u][i]);
            u = dp[u][i];
        }
    }
    return ans;
}
int maxPathValue(int u, int v) {
    int L = LCA(u, v);
    return max(maxValue(L, u), maxValue(L, v));
}
void calcForNonMSTEdges() {
    rep(i, m) {
        if(!MSTEdge[i]) {
            ans[i] = maxPathValue(edge[i].u, edge[i].v)-1;
        }
    }
}

int pa_edge[200011];
bool del[200011];
int curval[200011];
int tot = 0;
std::vector<int> V;
void dfs(int v, int p) {
    sz[v] = 1;
    tot++;
    V.pb(v);
    rep(i, tree[v].size()) {
        if(!del[tree[v][i].f] and tree[v][i].f != p) {
            dfs(tree[v][i].f, v);
            sz[v] += sz[tree[v][i].f];
        }
    }
}

int centroid(int v, int p) {
    int mx = -1;
    rep(i, tree[v].size()) {
        if(tree[v][i].f != p and !del[tree[v][i].f]) {
            if(mx == -1) {
                mx = tree[v][i].f;
            }
            else{
                if(sz[mx] < sz[tree[v][i].f]) mx = tree[v][i].f;
            }
        }
    }
    if(mx ==-1 or sz[mx] <= tot/2) return v;
    return centroid(mx, v);
}
int mark[200011];
void marknodes(int v, int p, int s) {
    mark[v] = s;
    curval[v] = INT_MAX;
    rep(i, tree[v].size()) {
        if(tree[v][i].f != p and !del[tree[v][i].f]) {
            marknodes(tree[v][i].f, v, s);
        }
    }
}
void dfscalc(int v,int p, int id) {
    rep(i, tree[v].size()) {
        if(tree[v][i].f != p and !del[tree[v][i].f]) {
            dfscalc(tree[v][i].f, v, tree[v][i].s);
            curval[v] = min(curval[v], curval[tree[v][i].f]);
        }
    }
    if(id!=-1) {
        ans[id] = min(ans[id], curval[v]-1);
    }
}
void calcForMSTEdges(int v, int id) {

    tot = 0;
    V.clear();
    dfs(v, -1);

    int root = centroid(v,-1);
    del[root] = 1;

    int tmp = 1;
    rep(i, tree[root].size()) {
        if(!del[tree[root][i].f]) {
            marknodes(tree[root][i].f,root,tmp++);
        }
    }
    curval[root] = INT_MAX;
    mark[root] = tmp++;

    int x, y;
    rep(i, V.size()) {
        x = V[i];
        rep(j, badGraph[x].size()) {
            y = badGraph[x][j].f;
            if(mark[x]!=0 and mark[y]!=0 and mark[x] != mark[y]) {
                curval[x] = min(curval[x], edge[badGraph[x][j].s].c);
                curval[y] = min(curval[y], edge[badGraph[x][j].s].c);
            }
        }
    }

    rep(i, V.size()) mark[V[i]] = 0;
    mark[root] = 0;

    dfscalc(root,-1,-1);

    rep(i, tree[root].size()) {
        if(!del[tree[root][i].f]) {
            calcForMSTEdges(tree[root][i].f, tree[root][i].s);
        }
    }
}
int main() {

    cin >> n >> m;
    rep(i, m) {
        cin >> edge[i].u >>  edge[i].v >> edge[i].c;
        edge[i].ind = i;
    }
    makeSpanningTree();
    init(1, -1, -1);
    build();
    rep(i, m) {
        ans[i] = INT_MAX;
    }
    calcForNonMSTEdges();
    calcForMSTEdges(1,-1);
    rep(i, m) {
        if(ans[i] == INT_MAX) ans[i] = -1;
        if(ans[i] == -1) assert(MSTEdge[i]);
        cout << ans[i] << " ";
    }
}


#include <bits/stdc++.h>

using namespace std;

struct Edge {
  int i, u, v, w;
  Edge() {}
  Edge(int _i, int _u, int _v, int _w) : i(_i), u(_u), v(_v), w(_w) {}
  bool operator<(const Edge& rhs) const {
    if (w != rhs.w) return w < rhs.w;
    return i < rhs.i;
  }
};

static const int N = 1 << 18;
static const int M = N;

int n, e, p[N];
vector<Edge> edges;
int m, st[N], nxt[M << 1], to[M << 1], id[M];
vector<pair<int, int> > adj[N];
bool cut[M];
int cnt[N], sub[N];
bool on_mst[M];
int col[N], max_w[N], ans[M];
vector<int> nodes;

int getp(int x) {
  return x == p[x] ? x : (p[x] = getp(p[x]));
}

void add(int u, int v, int i) {
  id[m / 2] = i;
  to[m] = v;
  nxt[m] = st[u];
  st[u] = m++;
  to[m] = u;
  nxt[m] = st[v];
  st[v] = m++;
}

void find(int u, int p, int total, int& root) {
  cnt[u] = 1;
  sub[u] = 0;
  for (int e = st[u]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2] || v == p) {
      continue;
    }
    find(v, u, total, root);
    cnt[u] += cnt[v];
    sub[u] = max(sub[u], cnt[v]);
  }
  sub[u] = max(sub[u], total - cnt[u]);
  if (root == -1 || sub[u] < sub[root]) {
    root = u;
  }
}

void upd_cnt(int u, int p) {
  cnt[u] = 1;
  for (int e = st[u]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2] || v == p) {
      continue;
    }
    cnt[u] += cnt[v];
  }
}

void flood(int u, int p, int c, int w) {
  nodes.push_back(u);
  col[u] = c;
  max_w[u] = w;
  for (int e = st[u]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2] || v == p) {
      continue;
    }
    flood(v, u, c, max(w, edges[e / 2].w));
  }
}

void upd(int& a, int b) {
  if (a == -1) a = b;
  else a = min(a, b);
}

void solve(int u, int total) {
  // Find root.
  int root = -1;
  find(u, -1, total, root);
  upd_cnt(root, -1);

  // Solve path across root.
  nodes.clear();
  nodes.push_back(root);
  col[root] = -1;
  max_w[root] = 0;
  for (int e = st[root]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2]) {
      continue;
    }
    flood(v, root, id[e / 2], edges[id[e / 2]].w);
  }
  /*
    for (int i = 0; i < (int)nodes.size(); ++i) {
      cout << nodes[i] << ": max_w->" << max_w[nodes[i]] << ", col->" << col[nodes[i]] << endl;
    }
    cout << endl;
  */
  for (int i = 0; i < (int)nodes.size(); ++i) {
    int u = nodes[i];
    for (int j = 0; j < (int)adj[u].size(); ++j) {
      int v = adj[u][j].first;
      int e_id = adj[u][j].second;
      // cout << u << "-" << v << "," << e_id << endl;
      if (col[u] == col[v]) continue;
      if (on_mst[e_id]) continue;
      // cout << e_id << " " << ans[e_id] << " " << max(max_w[u], max_w[v]) << endl;
      upd(ans[e_id], max(max_w[u], max_w[v]));
      if (col[u] != -1) upd(ans[col[u]], edges[e_id].w);
      // if (col[u] != -1) cout << col[u] << " " << ans[col[u]] << " " << edges[e_id].w << endl;
      if (col[v] != -1) upd(ans[col[v]], edges[e_id].w);
      // if (col[v] != -1) cout << col[v] << " " << ans[col[v]] << " " << edges[e_id].w << endl;
    }
  }

  // Solve subtree.
  for (int e = st[root]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2]) {
      continue;
    }
    cut[e / 2] = true;
    solve(v, cnt[v]);
  }
}

int main() {
  cin >> n >> e;
  for (int i = 0, u, v, w; i < e; ++i) {
    cin >> u >> v >> w;
    --u;
    --v;
    adj[u].push_back(make_pair(v, i));
    adj[v].push_back(make_pair(u, i));
    edges.push_back(Edge(i, u, v, w));
  }
  sort(edges.begin(), edges.end());
  for (int i = 0; i < n; ++i) p[i] = i;
  m = 0;
  memset(st, 0xff, sizeof(st));
  memset(on_mst, 0x00, sizeof(on_mst));
  for (int i = 0; i < e; ++i) {
    int pu = getp(edges[i].u);
    int pv = getp(edges[i].v);
    if (pu == pv) continue;
    on_mst[edges[i].i] =  true;
    add(edges[i].u, edges[i].v, edges[i].i);
    p[pu] = pv;
  }
  memset(ans, 0xff, sizeof(ans));
  solve(0, n);
  for (int i = 0; i < e; ++i) {
    cout << (ans[i] == -1 ? ans[i] : ans[i] - 1) << (i == e - 1 ? '\n' : ' ');
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct Edge {
  int u, v, w;
  Edge() {}
  Edge(int _u, int _v, int _w) : u(_u), v(_v), w(_w) {}
};

vector<Edge> edges;

bool ind_comp(int lhs, int rhs) {
  return edges[lhs].w < edges[rhs].w;
}

static const int N = 1 << 18;
static const int M = N;

int n, E, p[N];
int ind[M];
int m, st[N], nxt[M << 1], to[M << 1], id[M];
vector<pair<int, int> > adj[N];
bool cut[M];
int cnt[N], sub[N];
bool on_mst[M];
int col[N], max_w[N], ans[M], cur_max[N];
bool on_tree[N];
vector<int> nodes;

int getp(int x) {
  return x == p[x] ? x : (p[x] = getp(p[x]));
}

void add(int u, int v, int i) {
  id[m / 2] = i;
  to[m] = v;
  nxt[m] = st[u];
  st[u] = m++;
  to[m] = u;
  nxt[m] = st[v];
  st[v] = m++;
}

void find(int u, int p, int total, int& root) {
  cnt[u] = 1;
  sub[u] = 0;
  for (int e = st[u]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2] || v == p) {
      continue;
    }
    find(v, u, total, root);
    cnt[u] += cnt[v];
    sub[u] = max(sub[u], cnt[v]);
  }
  sub[u] = max(sub[u], total - cnt[u]);
  if (root == -1 || sub[u] < sub[root]) {
    root = u;
  }
}

void upd_cnt(int u, int p) {
  cnt[u] = 1;
  for (int e = st[u]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2] || v == p) {
      continue;
    }
    cnt[u] += cnt[v];
  }
}

void flood(int u, int p, int c, int w) {
  nodes.push_back(u);
  col[u] = c;
  max_w[u] = w;
  for (int e = st[u]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2] || v == p) {
      continue;
    }
    flood(v, u, c, max(w, edges[id[e / 2]].w));
  }
}

void upd_edge_on_mst(int u, int p, int e_id) {
  for (int e = st[u]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2] || v == p) {
      continue;
    }
    upd_edge_on_mst(v, u, id[e / 2]);
    cur_max[u] = min(cur_max[u], cur_max[v]);
  }
  if (e_id != -1 && cur_max[u] != 2e9) {
    if (ans[e_id] == -1) ans[e_id] = cur_max[u] - 1;
    else ans[e_id] = min(ans[e_id], cur_max[u] - 1);
  }
}

void solve(int u, int total) {
  // Find root.
  int root = -1;
  find(u, -1, total, root);
  upd_cnt(root, -1);

  // Solve path across root.
  nodes.clear();
  nodes.push_back(root);
  col[root] = -1;
  max_w[root] = 0;
  for (int e = st[root]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2]) {
      continue;
    }
    flood(v, root, id[e / 2], edges[id[e / 2]].w);
  }
  /*
    for (int i = 0; i < (int)nodes.size(); ++i) {
      cout << nodes[i] << ": max_w->" << max_w[nodes[i]] << ", col->" << col[nodes[i]] << endl;
    }
    cout << endl;
  */
  for (int i = 0; i < (int)nodes.size(); ++i) {
    on_tree[nodes[i]] = true;
    cur_max[nodes[i]] = 2e9;
  }
  for (int i = 0; i < (int)nodes.size(); ++i) {
    int u = nodes[i];
    for (int j = 0; j < (int)adj[u].size(); ++j) {
      int v = adj[u][j].first;
      int e_id = adj[u][j].second;
      // cout << u << "-" << v << "," << e_id << endl;
      if (!on_tree[v]) continue;
      if (col[u] == col[v]) continue;
      if (on_mst[e_id]) continue;
      // cout << e_id << " " << ans[e_id] << " " << max(max_w[u], max_w[v]) << endl;
      ans[e_id] = max(max_w[u], max_w[v]) - 1;
      cur_max[u] = min(cur_max[u], edges[e_id].w);
      cur_max[v] = min(cur_max[v], edges[e_id].w);
    }
  }
  for (int i = 0; i < (int)nodes.size(); ++i) {
    on_tree[nodes[i]] = false;
  }
  upd_edge_on_mst(root, -1, -1);

  // Solve subtree.
  for (int e = st[root]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2]) {
      continue;
    }
    cut[e / 2] = true;
    solve(v, cnt[v]);
  }
}

int main() {
  cin >> n >> E;
  for (int i = 0, u, v, w; i < E; ++i) {
    cin >> u >> v >> w;
    --u;
    --v;
    adj[u].push_back(make_pair(v, i));
    adj[v].push_back(make_pair(u, i));
    edges.push_back(Edge(u, v, w));
    ind[i] = i;
  }
  sort(ind, ind + E, ind_comp);
  for (int i = 0; i < n; ++i) p[i] = i;
  m = 0;
  memset(st, 0xff, sizeof(st));
  memset(on_mst, 0x00, sizeof(on_mst));
  for (int i = 0; i < E; ++i) {
    int _i = ind[i];
    int pu = getp(edges[_i].u);
    int pv = getp(edges[_i].v);
    if (pu == pv) continue;
    //  cout << _i << "," << edges[_i].u << "->" << edges[_i].v << ":" << edges[_i].w << endl;
    on_mst[_i] =  true;
    add(edges[_i].u, edges[_i].v, _i);
    p[pu] = pv;
  }
  memset(on_tree, 0x00, sizeof(on_tree));
  memset(ans, 0xff, sizeof(ans));
  solve(0, n);
  for (int i = 0; i < E; ++i) {
    cout << ans[i] << (i == E - 1 ? '\n' : ' ');
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct Edge {
  int u, v, w;
  Edge() {}
  Edge(int _u, int _v, int _w) : u(_u), v(_v), w(_w) {}
};

vector<Edge> edges;

bool ind_comp(int lhs, int rhs) {
  return edges[lhs].w < edges[rhs].w;
}

static const int N = 1 << 18;
static const int M = N;

int n, E, p[N];
int ind[M];
int m, st[N], nxt[M << 1], to[M << 1], id[M];
vector<pair<int, int> > adj[N];
bool cut[M];
int cnt[N], sub[N];
bool on_mst[M];
int col[N], max_w[N], ans[M], cur_max[N];
vector<int> nodes;

int getp(int x) {
  return x == p[x] ? x : (p[x] = getp(p[x]));
}

void add(int u, int v, int i) {
  id[m / 2] = i;
  to[m] = v;
  nxt[m] = st[u];
  st[u] = m++;
  to[m] = u;
  nxt[m] = st[v];
  st[v] = m++;
}

void find(int u, int p, int total, int& root) {
  cnt[u] = 1;
  sub[u] = 0;
  for (int e = st[u]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2] || v == p) {
      continue;
    }
    find(v, u, total, root);
    cnt[u] += cnt[v];
    sub[u] = max(sub[u], cnt[v]);
  }
  sub[u] = max(sub[u], total - cnt[u]);
  if (root == -1 || sub[u] < sub[root]) {
    root = u;
  }
}

void upd_cnt(int u, int p) {
  cnt[u] = 1;
  for (int e = st[u]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2] || v == p) {
      continue;
    }
    cnt[u] += cnt[v];
  }
}

void flood(int u, int p, int c, int w) {
  nodes.push_back(u);
  col[u] = c;
  max_w[u] = w;
  for (int e = st[u]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2] || v == p) {
      continue;
    }
    flood(v, u, c, max(w, edges[id[e / 2]].w));
  }
}

void upd_edge_on_mst(int u, int p, int e_id) {
  for (int e = st[u]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2] || v == p) {
      continue;
    }
    upd_edge_on_mst(v, u, id[e / 2]);
    cur_max[u] = min(cur_max[u], cur_max[v]);
  }
  if (e_id != -1 && cur_max[u] != 2e9) {
    if (ans[e_id] == -1) ans[e_id] = cur_max[u] - 1;
    else ans[e_id] = min(ans[e_id], cur_max[u] - 1);
  }
}

void solve(int u, int total) {
  // Find root.
  int root = -1;
  find(u, -1, total, root);
  upd_cnt(root, -1);

  // Solve path across root.
  nodes.clear();
  nodes.push_back(root);
  col[root] = -1;
  max_w[root] = 0;
  for (int e = st[root]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2]) {
      continue;
    }
    flood(v, root, id[e / 2], edges[id[e / 2]].w);
  }
  /*
    for (int i = 0; i < (int)nodes.size(); ++i) {
      cout << nodes[i] << ": max_w->" << max_w[nodes[i]] << ", col->" << col[nodes[i]] << endl;
    }
    cout << endl;
  */
  set<int> nodes_set;
  for (int i = 0; i < (int)nodes.size(); ++i) {
    nodes_set.insert(nodes[i]);
    cur_max[nodes[i]] = 2e9;
  }
  for (int i = 0; i < (int)nodes.size(); ++i) {
    int u = nodes[i];
    for (int j = 0; j < (int)adj[u].size(); ++j) {
      int v = adj[u][j].first;
      int e_id = adj[u][j].second;
      // cout << u << "-" << v << "," << e_id << endl;
      if (!nodes_set.count(v)) continue;
      if (col[u] == col[v]) continue;
      if (on_mst[e_id]) continue;
      // cout << e_id << " " << ans[e_id] << " " << max(max_w[u], max_w[v]) << endl;
      ans[e_id] = max(max_w[u], max_w[v]) - 1;
      cur_max[u] = min(cur_max[u], edges[e_id].w);
      cur_max[v] = min(cur_max[v], edges[e_id].w);
    }
  }

  upd_edge_on_mst(root, -1, -1);

  // Solve subtree.
  for (int e = st[root]; e != -1; e = nxt[e]) {
    int v = to[e];
    if (cut[e / 2]) {
      continue;
    }
    cut[e / 2] = true;
    solve(v, cnt[v]);
  }
}

int main() {
  cin >> n >> E;
  for (int i = 0, u, v, w; i < E; ++i) {
    cin >> u >> v >> w;
    --u;
    --v;
    adj[u].push_back(make_pair(v, i));
    adj[v].push_back(make_pair(u, i));
    edges.push_back(Edge(u, v, w));
    ind[i] = i;
  }
  sort(ind, ind + E, ind_comp);
  for (int i = 0; i < n; ++i) p[i] = i;
  m = 0;
  memset(st, 0xff, sizeof(st));
  memset(on_mst, 0x00, sizeof(on_mst));
  for (int i = 0; i < E; ++i) {
    int _i = ind[i];
    int pu = getp(edges[_i].u);
    int pv = getp(edges[_i].v);
    if (pu == pv) continue;
    //  cout << _i << "," << edges[_i].u << "->" << edges[_i].v << ":" << edges[_i].w << endl;
    on_mst[_i] =  true;
    add(edges[_i].u, edges[_i].v, _i);
    p[pu] = pv;
  }
  memset(ans, 0xff, sizeof(ans));
  solve(0, n);
  for (int i = 0; i < E; ++i) {
    cout << ans[i] << (i == E - 1 ? '\n' : ' ');
  }
  return 0;
}


//themis
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int, int> ii;

const int mod = 1e9 + 7;

const int inf = 2e9;

int add(int x, int y) {
    return (1ll * x + 1ll * y) % mod;
}

int del(int x, int y) {
    return ((1ll * x - 1ll * y) % mod + mod) % mod;
}

int mul(int x, int y) {
    return (1ll * x * 1ll * y) % mod;
}

const int N = 2e5 + 5;

int n, m, pSet[N], f[N], ans[N];

ii par[N][25];

bool used[N];

vector<pair<int, ii>> graph[N];

multiset<int> mst[N];
multiset<int>::iterator it;

vector<int> vec[N];

int findSet(int u) {
    if(u == pSet[u]) return u;
    else return pSet[u] = findSet(pSet[u]);
}

void unionSet(int u, int v) {
    u = findSet(u), v = findSet(v);
    if(u == v) return ;
    pSet[u] = v;
}

vector<pair<ii, ii>> edge;

void dfs(int u, int p) {
    for(int i = 0; i < graph[u].size(); i++) {
        int v = graph[u][i].fi, c = graph[u][i].se.fi, id = graph[u][i].se.se;
        if(v == p) continue;
        f[v] = f[u] + 1;
        par[v][0] = {u, c};
        dfs(v, u);
    }
}

void redfs(int u, int p, int id) {
    for(int i = 0; i < graph[u].size(); i++) {
        int v = graph[u][i].fi, id = graph[u][i].se.se;
        if(v == p) continue;
        redfs(v, u, id);
        if(mst[v].size())
            ans[id] = *mst[v].begin();
        else
            ans[id] = inf;
    }
    for(int i = 0; i < graph[u].size(); i++) {
        int v = graph[u][i].fi, id = graph[u][i].se.se;
        if(v == p) continue;
        if(mst[v].size() > mst[u].size())
            mst[u].swap(mst[v]);
        for(it = mst[v].begin(); it != mst[v].end(); it++)
            mst[u].insert(*it);
    }
    for(int i = 0; i < vec[u].size(); i++) {
        if(vec[u][i] > 0)
            mst[u].insert(vec[u][i]);
        else
            mst[u].erase(mst[u].find(-vec[u][i]));
    }
}

void preplca() {
    for(int j = 1; j <= 20; j++) {
        for(int i = 1; i <= n; i++) {
            par[i][j].fi = par[par[i][j - 1].fi][j - 1].fi;
            par[i][j].se = max(par[i][j - 1].se, par[par[i][j - 1].fi][j - 1].se);
        }
    }
}

ii findlca(int u, int v) {
    int mn = 0;
    if(f[u] < f[v]) swap(u, v);
    for(int i = 20; i >= 0; i--) {
        if(par[u][i].fi != 0 && f[par[u][i].fi] >= f[v]) {
            mn = max(mn, par[u][i].se);
            u = par[u][i].fi;
        }
    }
    if(u == v) return {u, mn};
    for(int i = 20; i >= 0; i--) {
        if(par[u][i].fi != 0 && par[v][i].fi != 0 && par[u][i].fi != par[v][i].fi) {
            mn = max(mn, par[u][i].se);
            mn = max(mn, par[v][i].se);
            u = par[u][i].fi;
            v = par[v][i].fi;
        }
    }
    mn = max(mn, par[u][0].se);
    mn = max(mn, par[v][0].se);
    return {par[u][0].fi, mn};
}

int main() {
    cin.tie(0), ios_base::sync_with_stdio(0);
    cin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int u, v, c;
        cin >> u >> v >> c;
        edge.pb({{c, i}, {u, v}});
    }
    for(int i = 1; i <= n; i++)
        pSet[i] = i;
    sort(edge.begin(), edge.end());
    for(int i = 0; i < edge.size(); i++) {
        int c = edge[i].fi.fi, u = edge[i].se.fi, v = edge[i].se.se, id = edge[i].fi.se;
        if(findSet(u) != findSet(v)) {
            used[id] = 1;
            unionSet(u, v);
            graph[u].pb({v, {c, id}});
            graph[v].pb({u, {c, id}});
        }
    }
    dfs(1, 1);
    preplca();
    for(int i = 0; i < edge.size(); i++) {
        int c = edge[i].fi.fi, u = edge[i].se.fi, v = edge[i].se.se, id = edge[i].fi.se;
        if(!used[id]) {
            int lca = findlca(u, v).fi;
            vec[lca].pb(-c);
            if(lca == u)
                vec[v].pb(c);
            else if(lca == v)
                vec[u].pb(c);
            else {
                vec[u].pb(c);
                vec[v].pb(c);
            }
            ans[id] = findlca(u, v).se;
        }
    }
    redfs(1, 1, 0);
    for(int i = 1; i <= m; i++) {
        if(ans[i] == inf) ans[i] = 0;
        cout << ans[i] - 1 << " ";
    }
}


//themis
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int, int> ii;

const int mod = 1e9 + 7;

const int inf = 2e9;

int add(int x, int y) {
    return (1ll * x + 1ll * y) % mod;
}

int del(int x, int y) {
    return ((1ll * x - 1ll * y) % mod + mod) % mod;
}

int mul(int x, int y) {
    return (1ll * x * 1ll * y) % mod;
}

const int N = 2e5 + 5;

int n, m, pSet[N], f[N], ans[N];

ii par[N][25];

bool used[N];

vector<pair<int, ii>> graph[N];

multiset<int> mst[N];
multiset<int>::iterator it;

vector<int> vec[N];

int findSet(int u) {
    if(u == pSet[u]) return u;
    else return pSet[u] = findSet(pSet[u]);
}

void unionSet(int u, int v) {
    u = findSet(u), v = findSet(v);
    if(u == v) return ;
    pSet[u] = v;
}

vector<pair<ii, ii>> edge;

void dfs(int u, int p) {
    for(int i = 0; i < graph[u].size(); i++) {
        int v = graph[u][i].fi, c = graph[u][i].se.fi, id = graph[u][i].se.se;
        if(v == p) continue;
        f[v] = f[u] + 1;
        par[v][0] = {u, c};
        dfs(v, u);
    }
}

void redfs(int u, int p, int id) {
    for(int i = 0; i < graph[u].size(); i++) {
        int v = graph[u][i].fi, id = graph[u][i].se.se;
        if(v == p) continue;
        redfs(v, u, id);
        if(mst[v].size())
            ans[id] = *mst[v].begin();
        else
            ans[id] = inf;
    }
    for(int i = 0; i < graph[u].size(); i++) {
        int v = graph[u][i].fi, id = graph[u][i].se.se;
        if(v == p) continue;
        if(mst[v].size() > mst[u].size())
            mst[u].swap(mst[v]);
        for(it = mst[v].begin(); it != mst[v].end(); it++)
            mst[u].insert(*it);
    }
    for(int i = 0; i < vec[u].size(); i++) {
        if(vec[u][i] > 0)
            mst[u].insert(vec[u][i]);
        else
            mst[u].erase(mst[u].find(-vec[u][i]));
    }
}

void preplca() {
    for(int j = 1; j <= 20; j++) {
        for(int i = 1; i <= n; i++) {
            par[i][j].fi = par[par[i][j - 1].fi][j - 1].fi;
            par[i][j].se = max(par[i][j - 1].se, par[par[i][j - 1].fi][j - 1].se);
        }
    }
}

ii findlca(int u, int v) {
    int mn = 0;
    if(f[u] < f[v]) swap(u, v);
    for(int i = 20; i >= 0; i--) {
        if(par[u][i].fi != 0 && f[par[u][i].fi] >= f[v]) {
            mn = max(mn, par[u][i].se);
            u = par[u][i].fi;
        }
    }
    if(u == v) return {u, mn};
    for(int i = 20; i >= 0; i--) {
        if(par[u][i].fi != 0 && par[v][i].fi != 0 && par[u][i].fi != par[v][i].fi) {
            mn = max(mn, par[u][i].se);
            mn = max(mn, par[v][i].se);
            u = par[u][i].fi;
            v = par[v][i].fi;
        }
    }
    mn = max(mn, par[u][0].se);
    mn = max(mn, par[v][0].se);
    return {par[u][0].fi, mn};
}

int main() {
    cin.tie(0), ios_base::sync_with_stdio(0);
    cin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int u, v, c;
        cin >> u >> v >> c;
        edge.pb({{c, i}, {u, v}});
    }
    for(int i = 1; i <= n; i++)
        pSet[i] = i;
    sort(edge.begin(), edge.end());
    for(int i = 0; i < edge.size(); i++) {
        int c = edge[i].fi.fi, u = edge[i].se.fi, v = edge[i].se.se, id = edge[i].fi.se;
        if(findSet(u) != findSet(v)) {
            used[id] = 1;
            unionSet(u, v);
            graph[u].pb({v, {c, id}});
            graph[v].pb({u, {c, id}});
        }
    }
    dfs(1, 1);
    preplca();
    for(int i = 0; i < edge.size(); i++) {
        int c = edge[i].fi.fi, u = edge[i].se.fi, v = edge[i].se.se, id = edge[i].fi.se;
        if(!used[id]) {
            int lca = findlca(u, v).fi;
            vec[lca].pb(-c);
            if(lca == u)
                vec[v].pb(c);
            else if(lca == v)
                vec[u].pb(c);
            else {
                vec[lca].pb(-c);
                vec[u].pb(c);
                vec[v].pb(c);
            }
            ans[id] = findlca(u, v).se;
        }
    }
    redfs(1, 1, 0);
    for(int i = 1; i <= m; i++) {
        if(ans[i] == inf) ans[i] = 0;
        cout << ans[i] - 1 << " ";
    }
}


/*input
4 4

1 2 2

2 3 2

3 4 2

4 1 3

*/
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
typedef pair<ii, ii> II;

#define x first
#define y second

const int N = 2e5 + 5;

int n, m, p[N], par[N][20], mx[N][20], h[N], ans[N], in[N];
vector<II> E;
vector<int> G[N], out;
multiset<ii> s[N], s2[N];

int find(int u) {
	if(u == p[u]) return u;
	return p[u] = find(p[u]);
}

void join(int u, int v) {
	u = find(u), v = find(v);
	if(u == v) return;
	p[u] = v;
}

void dfs(int u, int dad) {
	par[u][0] = dad;
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == dad) continue;
		mx[v][0] = w;
		h[v] = h[u] + 1;
		dfs(v, u);
	}
}

ii lca(int u, int v) {
	int res = 0;
	for(int i = 19; i >= 0; --i) if(h[par[u][i]] >= h[v]) {
		res = max(res, mx[u][i]);
		u = par[u][i];
	}
	for(int i = 19; i >= 0; --i) if(h[par[v][i]] >= h[u]) {
		res = max(res, mx[v][i]);
		v = par[v][i];
	}
	for(int i = 19; i >= 0; --i) if(par[u][i] != par[v][i]) {
		res = max(res, mx[u][i]);
		u = par[u][i];
		res = max(res, mx[v][i]);
		v = par[v][i];
	}
	while(u != v) {
		res = max(res, mx[u][0]);
		res = max(res, mx[v][0]);
		u = par[u][0]; v = par[v][0];
	}
	return ii(u, res);
}

void solve(int u, int p, int ID) {
	int pos = 0;
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		solve(v, u, E[id].x.y);
		if(s[pos].size() < s[v].size()) pos = v;
	}
	if(pos) swap(s[u], s[pos]), swap(s2[u], s2[pos]);
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		for(multiset<ii>::iterator it = s[v].begin(); it != s[v].end(); ++it) {
			s[u].insert(*it);
		}
	}
	while(1) {
		multiset<ii>::iterator it = s2[u].lower_bound(ii(u, 0));
		if(it == s2[u].end()) break;
		multiset<ii>::iterator it2 = s[u].find(ii((*it).y, (*it).x));
		s2[u].erase(it); s[u].erase(it2);
	}
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		s[u].insert(ii(w, lca(u, v).x));
		s2[u].insert(ii(lca(u, v).x, w));
	}
	if(s[u].size()) ans[ID] = max(ans[ID], (*s[u].begin()).x - 1);
}

int main(){
	iostream::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	for(int i = 0; i < m; ++i) {
		int u, v, c;
		cin >> u >> v >> c;
		E.push_back(II(ii(c, i), ii(u, v)));
	}
	for(int i = 1; i <= n; ++i) p[i] = i;
	sort(E.begin(), E.end());
	for(int i = 0; i < E.size(); ++i) {
		int u = E[i].y.x, v = E[i].y.y;
		G[u].push_back(i); G[v].push_back(i);
	}
	for(int i = 0; i < E.size(); ++i) {
		int u, v, id;
		id = E[i].x.y, u = E[i].y.x, v = E[i].y.y;
		if(find(u) != find(v)) join(u, v), in[id] = true;
		else out.push_back(i);
	}
	dfs(1, 1);
	for(int i = 1; i < 20; ++i) for(int j = 1; j <= n; ++j) {
		par[j][i] = par[par[j][i - 1]][i - 1];
		mx[j][i] = max(mx[j][i - 1], mx[par[j][i - 1]][i - 1]);
	}
	for(int i = 0; i < m; ++i) ans[i] = -1;
	for(int i = 0; i < out.size(); ++i) {
		int id = out[i];
		int u = E[id].y.x, v = E[id].y.y, ID = E[id].x.y;
		ans[ID] = max(ans[ID], lca(u, v).y - 1);
	}
	solve(1, 1, m);
	for(int i = 0; i < m; ++i) printf("%d ", ans[i]);
}


/*input
4 3
1 2 2
2 3 2
3 4 2
*/
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
typedef pair<int, ii> II;

#define x first
#define y second

const int N = 2e5 + 5;

int n, m, p[N], par[N][20], mx[N][20], h[N], ans[N], in[N];
vector<II> E;
vector<int> G[N], out;
multiset<ii> s[N], s2[N];

int find(int u) {
	if(u == p[u]) return u;
	return p[u] = find(p[u]);
}

void join(int u, int v) {
	u = find(u), v = find(v);
	if(u == v) return;
	p[u] = v;
}

void dfs(int u, int dad) {
	par[u][0] = dad;
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[id]) continue;
		int w = E[id].x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == dad) continue;
		mx[v][0] = w;
		h[v] = h[u] + 1;
		dfs(v, u);
	}
}

ii lca(int u, int v) {
	int res = 0;
	for(int i = 19; i >= 0; --i) if(h[par[u][i]] >= h[v]) {
		res = max(res, mx[u][i]);
		u = par[u][i];
	}
	for(int i = 19; i >= 0; --i) if(h[par[v][i]] >= h[u]) {
		res = max(res, mx[v][i]);
		v = par[v][i];
	}
	for(int i = 19; i >= 0; --i) if(par[u][i] != par[v][i]) {
		res = max(res, mx[u][i]);
		u = par[u][i];
		res = max(res, mx[v][i]);
		v = par[v][i];
	}
	while(u != v) {
		res = max(res, mx[u][0]);
		res = max(res, mx[v][0]);
		u = par[u][0]; v = par[v][0];
	}
	return ii(u, res);
}

void solve(int u, int p, int ID) {
	int pos = 0;
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[id]) continue;
		int w = E[id].x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		solve(v, u, id);
		if(s[pos].size() < s[v].size()) pos = v;
	}
	if(pos) swap(s[u], s[pos]), swap(s2[u], s2[pos]);
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[id]) continue;
		int w = E[id].x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		for(multiset<ii>::iterator it = s[v].begin(); it != s[v].end(); ++it) {
			s[u].insert(*it);
		}
	}
	while(1) {
		multiset<ii>::iterator it = s2[u].lower_bound(ii(u, 0));
		if(it == s2[u].end()) break;
		multiset<ii>::iterator it2 = s[u].find(ii((*it).y, (*it).x));
		s2[u].erase(it); s[u].erase(it2);
	}
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(in[id]) continue;
		int w = E[id].x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		s[u].insert(ii(w, lca(u, v).x));
		s2[u].insert(ii(lca(u, v).x, w));
	}
	if(s[u].size()) ans[ID] = max(ans[ID], (*s[u].begin()).x - 1);
}

int main(){
	iostream::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	for(int i = 0; i < m; ++i) {
		int u, v, c;
		cin >> u >> v >> c;
		E.push_back(II(c, ii(u, v)));
		G[u].push_back(i);
		G[v].push_back(i);
	}
	for(int i = 1; i <= n; ++i) p[i] = i;
	sort(E.begin(), E.end());
	for(int i = 0; i < E.size(); ++i) {
		int u, v, c;
		c = E[i].x, u = E[i].y.x, v = E[i].y.y;
		if(find(u) != find(v)) join(u, v), in[i] = true;
		else out.push_back(i);
	}
	dfs(1, 1);
	for(int i = 1; i < 20; ++i) for(int j = 1; j <= n; ++j) {
		par[j][i] = par[par[j][i - 1]][i - 1];
		mx[j][i] = max(mx[j][i - 1], mx[par[j][i - 1]][i - 1]);
	}
	for(int i = 0; i < m; ++i) ans[i] = -1;
	for(int i = 0; i < out.size(); ++i) {
		int id = out[i];
		int u = E[id].y.x, v = E[id].y.y;
		ans[id] = max(ans[id], lca(u, v).y - 1);
	}
	solve(1, 1, m);
	for(int i = 0; i < m; ++i) printf("%d ", ans[i]);
}


/*input
20 40

5 20 86

6 2 33

16 20 50

8 2 92

13 20 28

5 16 33

8 11 90

10 12 26

12 18 93

4 2 76

11 18 14

13 15 3

14 20 92

5 8 37

16 18 85

18 8 47

9 3 12

15 20 30

11 9 3

19 12 94

13 18 75

14 4 87

19 2 91

8 7 20

18 2 9

17 3 19

2 15 93

3 2 26

1 15 68

13 7 20

17 14 78

4 13 45

10 17 72

1 19 16

17 20 99

7 19 4

4 1 23

16 9 71

6 17 1

13 2 3

*/
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
typedef pair<ii, ii> II;

#define x first
#define y second

const int N = 2e5 + 5;

int n, m, p[N], par[N][20], mx[N][20], h[N], ans[N], in[N];
vector<II> E;
vector<int> G[N], out;
multiset<ii> s[N], s2[N];

int find(int u) {
	if(u == p[u]) return u;
	return p[u] = find(p[u]);
}

void join(int u, int v) {
	u = find(u), v = find(v);
	if(u == v) return;
	p[u] = v;
}

void dfs(int u, int dad) {
	par[u][0] = dad;
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == dad) continue;
		mx[v][0] = w;
		h[v] = h[u] + 1;
		dfs(v, u);
	}
}

ii lca(int u, int v) {
	int res = 0;
	for(int i = 19; i >= 0; --i) if(h[par[u][i]] >= h[v]) {
		res = max(res, mx[u][i]);
		u = par[u][i];
	}
	for(int i = 19; i >= 0; --i) if(h[par[v][i]] >= h[u]) {
		res = max(res, mx[v][i]);
		v = par[v][i];
	}
	for(int i = 19; i >= 0; --i) if(par[u][i] != par[v][i]) {
		res = max(res, mx[u][i]);
		u = par[u][i];
		res = max(res, mx[v][i]);
		v = par[v][i];
	}
	while(u != v) {
		res = max(res, mx[u][0]);
		res = max(res, mx[v][0]);
		u = par[u][0]; v = par[v][0];
	}
	return ii(u, res);
}

void solve(int u, int p, int ID) {
	int pos = 0;
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		solve(v, u, E[id].x.y);
		if(s[pos].size() < s[v].size()) pos = v;
	}
	if(pos) swap(s[u], s[pos]), swap(s2[u], s2[pos]);
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p || v == pos) continue;
		for(multiset<ii>::iterator it = s[v].begin(); it != s[v].end(); ++it) {
			s[u].insert(*it);
		}
	}
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		s[u].insert(ii(w, lca(u, v).x));
		s2[u].insert(ii(lca(u, v).x, w));
	}
	while(1) {
		multiset<ii>::iterator it = s2[u].lower_bound(ii(u, 0));
		if(it == s2[u].end()) break;
		if((*it).x != u) break;
		multiset<ii>::iterator it2 = s[u].find(ii((*it).y, (*it).x));
		s2[u].erase(it); s[u].erase(it2);
	}
	if(s[u].size()) ans[ID] = max(ans[ID], (*s[u].begin()).x - 1);
	//if(s[u].size()) cout << u << ' ' << (*s[u].begin()).x << endl;
}

int main(){
	iostream::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	for(int i = 0; i < m; ++i) {
		int u, v, c;
		cin >> u >> v >> c;
		E.push_back(II(ii(c, i), ii(u, v)));
	}
	for(int i = 1; i <= n; ++i) p[i] = i;
	sort(E.begin(), E.end());
	for(int i = 0; i < E.size(); ++i) {
		int u = E[i].y.x, v = E[i].y.y;
		G[u].push_back(i); G[v].push_back(i);
	}
	for(int i = 0; i < E.size(); ++i) {
		int u, v, id;
		id = E[i].x.y, u = E[i].y.x, v = E[i].y.y;
		if(find(u) != find(v)) join(u, v), in[id] = true;
		else out.push_back(i);
	}
	dfs(1, 1);
	for(int i = 1; i < 20; ++i) for(int j = 1; j <= n; ++j) {
		par[j][i] = par[par[j][i - 1]][i - 1];
		mx[j][i] = max(mx[j][i - 1], mx[par[j][i - 1]][i - 1]);
	}
	for(int i = 0; i < m; ++i) ans[i] = -1;
	for(int i = 0; i < out.size(); ++i) {
		int id = out[i];
		int u = E[id].y.x, v = E[id].y.y, ID = E[id].x.y;
		ans[ID] = max(ans[ID], lca(u, v).y - 1);
	}
	solve(1, 1, m);
	for(int i = 0; i < m; ++i) {
		cout << ans[i] << ' ';
	}
}


/*input
20 20

9 4 21

5 7 22

2 20 37

1 6 19

14 20 5

8 9 43

17 4 40

12 10 26

19 4 99

11 4 55

3 18 6

7 8 43

13 9 1

20 4 69

16 18 67

18 13 90

6 11 66

15 5 76

10 9 93

5 18 14

*/
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
typedef pair<ii, ii> II;

#define x first
#define y second

const int N = 2e5 + 5;

int n, m, p[N], par[N][20], mx[N][20], h[N], ans[N], in[N];
vector<II> E;
vector<int> G[N], out;
multiset<ii> s[N], s2[N];

int find(int u) {
	if(u == p[u]) return u;
	return p[u] = find(p[u]);
}

void join(int u, int v) {
	u = find(u), v = find(v);
	if(u == v) return;
	p[u] = v;
}

void dfs(int u, int dad) {
	par[u][0] = dad;
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == dad) continue;
		mx[v][0] = w;
		h[v] = h[u] + 1;
		dfs(v, u);
	}
}

ii lca(int u, int v) {
	int res = 0;
	for(int i = 19; i >= 0; --i) if(h[par[u][i]] >= h[v]) {
		res = max(res, mx[u][i]);
		u = par[u][i];
	}
	for(int i = 19; i >= 0; --i) if(h[par[v][i]] >= h[u]) {
		res = max(res, mx[v][i]);
		v = par[v][i];
	}
	for(int i = 19; i >= 0; --i) if(par[u][i] != par[v][i]) {
		res = max(res, mx[u][i]);
		u = par[u][i];
		res = max(res, mx[v][i]);
		v = par[v][i];
	}
	while(u != v) {
		res = max(res, mx[u][0]);
		res = max(res, mx[v][0]);
		u = par[u][0]; v = par[v][0];
	}
	return ii(u, res);
}

void solve(int u, int p, int ID) {
	int pos = 0;
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		solve(v, u, E[id].x.y);
		if(s[pos].size() < s[v].size()) pos = v;
	}
	if(pos) swap(s[u], s[pos]), swap(s2[u], s2[pos]);
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p || v != pos) continue;
		for(multiset<ii>::iterator it = s[v].begin(); it != s[v].end(); ++it) {
			s[u].insert(*it);
		}
	}
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		s[u].insert(ii(w, lca(u, v).x));
		s2[u].insert(ii(lca(u, v).x, w));
	}
	while(1) {
		multiset<ii>::iterator it = s2[u].lower_bound(ii(u, 0));
		if(it == s2[u].end()) break;
		if((*it).x != u) break;
		multiset<ii>::iterator it2 = s[u].find(ii((*it).y, (*it).x));
		s2[u].erase(it); s[u].erase(it2);
	}
	if(s[u].size()) ans[ID] = max(ans[ID], (*s[u].begin()).x - 1);
}

int main(){
	iostream::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	for(int i = 0; i < m; ++i) {
		int u, v, c;
		cin >> u >> v >> c;
		E.push_back(II(ii(c, i), ii(u, v)));
	}
	for(int i = 1; i <= n; ++i) p[i] = i;
	sort(E.begin(), E.end());
	for(int i = 0; i < E.size(); ++i) {
		int u = E[i].y.x, v = E[i].y.y;
		G[u].push_back(i); G[v].push_back(i);
	}
	for(int i = 0; i < E.size(); ++i) {
		int u, v, id;
		id = E[i].x.y, u = E[i].y.x, v = E[i].y.y;
		if(find(u) != find(v)) join(u, v), in[id] = true;
		else out.push_back(i);
	}
	dfs(1, 1);
	for(int i = 1; i < 20; ++i) for(int j = 1; j <= n; ++j) {
		par[j][i] = par[par[j][i - 1]][i - 1];
		mx[j][i] = max(mx[j][i - 1], mx[par[j][i - 1]][i - 1]);
	}
	for(int i = 0; i < m; ++i) ans[i] = -1;
	for(int i = 0; i < out.size(); ++i) {
		int id = out[i];
		int u = E[id].y.x, v = E[id].y.y, ID = E[id].x.y;
		ans[ID] = max(ans[ID], lca(u, v).y - 1);
	}
	solve(1, 1, m);
	for(int i = 0; i < m; ++i) printf("%d ", ans[i]);
}


/*input
20 20

9 4 21

5 7 22

2 20 37

1 6 19

14 20 5

8 9 43

17 4 40

12 10 26

19 4 99

11 4 55

3 18 6

7 8 43

13 9 1

20 4 69

16 18 67

18 13 90

6 11 66

15 5 76

10 9 93

5 18 14
*/
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
typedef pair<ii, ii> II;

#define x first
#define y second

const int N = 2e5 + 5;

int n, m, p[N], par[N][20], mx[N][20], h[N], ans[N], in[N];
vector<II> E;
vector<int> G[N], out;
multiset<ii> s[N], s2[N];

int find(int u) {
	if(u == p[u]) return u;
	return p[u] = find(p[u]);
}

void join(int u, int v) {
	u = find(u), v = find(v);
	if(u == v) return;
	p[u] = v;
}

void dfs(int u, int dad) {
	par[u][0] = dad;
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == dad) continue;
		mx[v][0] = w;
		h[v] = h[u] + 1;
		dfs(v, u);
	}
}

ii lca(int u, int v) {
	int res = 0;
	for(int i = 19; i >= 0; --i) if(h[par[u][i]] >= h[v]) {
		res = max(res, mx[u][i]);
		u = par[u][i];
	}
	for(int i = 19; i >= 0; --i) if(h[par[v][i]] >= h[u]) {
		res = max(res, mx[v][i]);
		v = par[v][i];
	}
	for(int i = 19; i >= 0; --i) if(par[u][i] != par[v][i]) {
		res = max(res, mx[u][i]);
		u = par[u][i];
		res = max(res, mx[v][i]);
		v = par[v][i];
	}
	while(u != v) {
		res = max(res, mx[u][0]);
		res = max(res, mx[v][0]);
		u = par[u][0]; v = par[v][0];
	}
	return ii(u, res);
}

void solve(int u, int p, int ID) {
	int pos = 0;
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		solve(v, u, E[id].x.y);
		if(s[pos].size() < s[v].size()) pos = v;
	}
	if(pos) swap(s[u], s[pos]), swap(s2[u], s2[pos]);
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(!in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p || v == pos) continue;
		for(multiset<ii>::iterator it = s[v].begin(); it != s[v].end(); ++it) {
			s[u].insert(*it);
		}
		for(multiset<ii>::iterator it2 = s2[v].begin(); it2 != s2[v].end(); ++it2) {
			s2[u].insert(*it2);
		}
	}
	for(int i = 0; i < G[u].size(); ++i) {
		int id = G[u][i];
		if(in[E[id].x.y]) continue;
		int w = E[id].x.x;
		int v = E[id].y.x; if(v == u) v = E[id].y.y;
		if(v == p) continue;
		s[u].insert(ii(w, lca(u, v).x));
		s2[u].insert(ii(lca(u, v).x, w));
	}
	while(1) {
		multiset<ii>::iterator it = s2[u].lower_bound(ii(u, 0));
		if(it == s2[u].end()) break;
		if((*it).x != u) break;
		multiset<ii>::iterator it2 = s[u].find(ii((*it).y, (*it).x));
		s2[u].erase(it); s[u].erase(it2);
	}
	if(s[u].size()) ans[ID] = max(ans[ID], (*s[u].begin()).x - 1);
	//if(s[u].size()) cout << u << ' ' << (*s2[u].begin()).y << endl;
}

int main(){
	iostream::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	for(int i = 0; i < m; ++i) {
		int u, v, c;
		cin >> u >> v >> c;
		E.push_back(II(ii(c, i), ii(u, v)));
	}
	for(int i = 1; i <= n; ++i) p[i] = i;
	sort(E.begin(), E.end());
	for(int i = 0; i < E.size(); ++i) {
		int u = E[i].y.x, v = E[i].y.y;
		G[u].push_back(i); G[v].push_back(i);
	}
	for(int i = 0; i < E.size(); ++i) {
		int u, v, id;
		id = E[i].x.y, u = E[i].y.x, v = E[i].y.y;
		if(find(u) != find(v)) join(u, v), in[id] = true;
		else out.push_back(i);
	}
	dfs(1, 1);
	for(int i = 1; i < 20; ++i) for(int j = 1; j <= n; ++j) {
		par[j][i] = par[par[j][i - 1]][i - 1];
		mx[j][i] = max(mx[j][i - 1], mx[par[j][i - 1]][i - 1]);
	}
	for(int i = 0; i < m; ++i) ans[i] = -1;
	for(int i = 0; i < out.size(); ++i) {
		int id = out[i];
		int u = E[id].y.x, v = E[id].y.y, ID = E[id].x.y;
		ans[ID] = max(ans[ID], lca(u, v).y - 1);
	}
	solve(1, 1, m);
	for(int i = 0; i < m; ++i) {
		cout << ans[i] << ' ';
	}
}


/*input
4 4
1 2 2
2 3 2
3 4 2
4 1 3
*/
#pragma GCC optimize("O3")

#include <bits/stdc++.h>
using namespace std;

const int N = 200005;
int Ans[N], cost[N], height[N], par[18][N], mx[18][N], n, m;
multiset<int> st[N];
vector<pair<int, int> > child[N], events[N];

struct Dsu {
	int Arr[N], Size[N];
	Dsu() {
		for(int i = 0; i < N; ++i)
			Size[i] = 1, Arr[i] = i;
	}

	int root(int a) {
		if(a == Arr[a]) return a;
		return Arr[a] = root(Arr[a]);
	}

	void Union(int a, int b) {
		a = root(a), b = root(b);
		if(Size[a] < Size[b]) swap(a, b);
		if(a != b) Arr[b] = a, Size[a] += Size[b];
	}
} G;

struct Edge {
	int u, v, index;
	Edge() {
		u = v = index = 0;
	}
	Edge(int _u, int _v, int _index) :
		u(_u), v(_v), index(_index) {}
	bool operator<(const Edge &x) const {
		return cost[index] < cost[x.index];
	}
};

void dfs(int u) {
	for(int i = 1; i < 18; ++i) {
		par[i][u] = par[i - 1][par[i - 1][u]];
		mx[i][u] = max(mx[i - 1][u], mx[i - 1][par[i - 1][u]]);
	}

	for(auto v : child[u]) {
		if(v.first == par[0][u]) continue;
		height[v.first] = height[u] + 1;
		mx[0][v.first] = cost[v.second];
		par[0][v.first] = u;
		dfs(v.first);
	}
}

pair<int, int> lca(int u, int v) {
	if(height[u] < height[v]) swap(u, v);

	int Mx = 0;
	for(int i = 17; i >= 0; --i)
		if(height[par[i][u]] >= height[v])
			Mx = max(Mx, mx[i][u]), u = par[i][u];

	for(int i = 17; i >= 0; --i)
		if(par[i][u] != par[i][v])
			Mx = max({Mx, mx[i][u], mx[i][v]}),
			u = par[i][u], v = par[i][v];

	while(u != v) {
		Mx = max({Mx, mx[0][u], mx[0][v]});
		u = par[0][u], v = par[0][v];
	}
	return make_pair(u, Mx);
}

void dfs2(int u, int index) {
	for(auto v : child[u]) {
		if(v.first == par[0][u]) continue;
		dfs2(v.first, v.second);
		if(st[u].size() < st[v.first].size())
			st[u].swap(st[v.first]);

		for(auto cur : st[v.first])
			st[u].insert(cur);
	}

	for(auto cur : events[u])
		if(cur.first == 0) st[u].insert(cur.second);
			else st[u].erase(st[u].find(cur.second));

	if(st[u].size()) Ans[index] = max(*st[u].begin() - 1, Ans[index]);
		else Ans[index] = -1;
}

int main(){
	scanf("%d%d", &n, &m);

	vector<Edge> E(m);
	for(int i = 0; i < m; ++i) {
		int u, v; scanf("%d%d%d", &u, &v, &cost[i]);
		E[i] = Edge(u, v, i);
	}
	sort(E.begin(), E.end());

	vector<bool> mark(m);
	for(int i = 0; i < m; ++i) {
		if(G.root(E[i].u) == G.root(E[i].v)) continue;
		G.Union(E[i].u, E[i].v), mark[i] = 1;
		child[E[i].u].push_back(make_pair(E[i].v, E[i].index));
		child[E[i].v].push_back(make_pair(E[i].u, E[i].index));
	}
	height[1] = 1, dfs(1);

	for(int i = 0; i < m; ++i) if(!mark[i]) {
		if(height[E[i].u] > height[E[i].v]) swap(E[i].u, E[i].v);

		pair<int, int> k = lca(E[i].u, E[i].v);
		Ans[E[i].index] = k.second - 1;

		if(k.first == E[i].u) {
			events[E[i].v].push_back(make_pair(0, cost[E[i].index]));
			events[E[i].u].push_back(make_pair(1, cost[E[i].index]));
		}
		else {
			events[E[i].v].push_back(make_pair(0, cost[E[i].index]));
			events[E[i].u].push_back(make_pair(0, cost[E[i].index]));
			events[k.first].push_back(make_pair(1, cost[E[i].index]));
			events[k.first].push_back(make_pair(1, cost[E[i].index]));
		}
	}
	dfs2(1, m);

	for(int i = 0; i < m; ++i)
		printf("%s%d", i ? " " : "", Ans[i]);
	printf("\n");
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MAXN=2e5+10,MAXLOGN=20,INF=0x7FFFFFF;
int n,logn,m,dep[MAXN],ans[MAXN];
pii fa[MAXN],anc[MAXLOGN][MAXN];
int lim[MAXLOGN][MAXN];
bool in_mst[MAXN];
struct Edge
{
    int u,v,c,id;
};
Edge e[MAXN];
namespace MST
{
    namespace DSU
    {
        int fa[MAXN];
        int find(int x)
        {
            return fa[x]==x?x:fa[x]=find(fa[x]);
        }
        bool merge(int a,int b)
        {
            a=find(a);
            b=find(b);
            if(a!=b)
                return fa[a]=b,true;
            else
                return false;
        }
        void init()
        {
            for(int i=1;i<=n;i++)
                fa[i]=i;
        }
    }
    Edge e_bak[MAXN];
    vector<pii> e_mst[MAXN];
    bool cmp(Edge a,Edge b)
    {
        return a.c<b.c;
    }
    void dfs(int x)
    {
        for(vector<pii>::iterator i=e_mst[x].begin();i<e_mst[x].end();i++)
        {
            if(i->first!=fa[x].first)
            {
                dep[i->first]=dep[x]+1;
                fa[i->first]=make_pair(x,i->second);
                dfs(i->first);
            }
        }
    }
    void solve()
    {
        for(int i=1;i<=n;i++)
            e_bak[i]=e[i];
        sort(e_bak+1,e_bak+n+1,cmp);
        DSU::init();
        for(int i=1;i<=n;i++)
        {
            if(DSU::merge(e_bak[i].u,e_bak[i].v))
            {
                in_mst[e_bak[i].id]=true;
                e_mst[e_bak[i].u].push_back(make_pair(e_bak[i].v,e_bak[i].c));
                e_mst[e_bak[i].v].push_back(make_pair(e_bak[i].u,e_bak[i].c));
            }
        }
        dep[1]=1;
        dfs(1);
    }
}
void init()
{
    scanf("%d%d",&n,&m);
    logn=0;
    while(1<<logn<n)
        logn++;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);
        e[i].id=i;
    }
    MST::solve();
    for(int i=1;i<=n;i++)
    {
        anc[0][i]=fa[i];
        lim[0][i]=INF;
    }
    for(int i=1;i<=logn;i++)
        for(int j=1;j<=n;j++)
        {
            anc[i][j]=anc[i-1][anc[i-1][j].first];
            anc[i][j].second+=anc[i-1][j].second;
            lim[i][j]=INF;
        }
}
pii LCA(int a,int b,int c)
{
    int mi=INF;
    if(dep[a]>dep[b])
        swap(a,b);
    int offset=b-a;
    for(int i=logn;i>=0;i--)
    {
        if((offset>>i)&1)
        {
            mi=min(mi,anc[i][b].second);
            lim[i][b]=min(lim[i][b],c);
            b=anc[i][b].first;
        }
    }
    if(a==b)
        return make_pair(a,mi);
    for(int i=logn;i>=0;i--)
    {
        if(anc[i][a].first!=anc[i][b].first)
        {
            mi=min(mi,min(anc[i][a].second,anc[i][b].second));
            lim[i][a]=min(lim[i][a],c);
            lim[i][b]=min(lim[i][b],c);
            a=anc[i][a].first;
            b=anc[i][b].first;
        }
    }
    mi=min(mi,min(fa[a].second,fa[b].second));
    return make_pair(fa[a].first,mi);
}
void solve()
{
    for(int i=1;i<=m;i++)
    {
        if(!in_mst[i])
        {
            ans[i]=LCA(e[i].u,e[i].v,e[i].c).second-1;
        }
    }
    for(int i=logn;i;i--)
        for(int j=1;j<=n;j++)
        {
            lim[i-1][j]=min(lim[i-1][j],lim[i][j]);
            lim[i-1][anc[i-1][j].first]=min(lim[i-1][anc[i-1][j].first],lim[i][j]);
        }
    for(int i=1;i<=m;i++)
    {
        if(in_mst[i])
        {
            if(fa[e[i].u].first==e[i].v)
                ans[i]=lim[0][e[i].u];
            else
                ans[i]=lim[0][e[i].v];
            if(ans[i]==INF)
                ans[i]=0;
            ans[i]--;
        }
    }
    for(int i=1;i<=m;i++)
        printf("%d ",ans[i]);
    printf("\n");
}
int main()
{
    init();
    solve();
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MAXN=2e5+10,MAXLOGN=20,INF=0x7FFFFFFF;
int n,logn,m,dep[MAXN],ans[MAXN];
pii fa[MAXN],anc[MAXLOGN][MAXN];
int lim[MAXLOGN][MAXN];
bool in_mst[MAXN];
struct Edge
{
    int u,v,c,id;
};
Edge e[MAXN];
namespace MST
{
    namespace DSU
    {
        int fa[MAXN];
        int find(int x)
        {
            return fa[x]==x?x:fa[x]=find(fa[x]);
        }
        bool merge(int a,int b)
        {
            a=find(a);
            b=find(b);
            if(a!=b)
                return fa[a]=b,true;
            else
                return false;
        }
        void init()
        {
            for(int i=1;i<=n;i++)
                fa[i]=i;
        }
    }
    Edge e_bak[MAXN];
    vector<pii> e_mst[MAXN];
    bool cmp(Edge a,Edge b)
    {
        return a.c<b.c;
    }
    void dfs(int x)
    {
        for(vector<pii>::iterator i=e_mst[x].begin();i<e_mst[x].end();i++)
        {
            if(i->first!=fa[x].first)
            {
                dep[i->first]=dep[x]+1;
                fa[i->first]=make_pair(x,i->second);
                dfs(i->first);
            }
        }
    }
    void solve()
    {
        for(int i=1;i<=m;i++)
            e_bak[i]=e[i];
        sort(e_bak+1,e_bak+m+1,cmp);
        DSU::init();
        for(int i=1;i<=n;i++)
        {
            if(DSU::merge(e_bak[i].u,e_bak[i].v))
            {
                in_mst[e_bak[i].id]=true;
                e_mst[e_bak[i].u].push_back(make_pair(e_bak[i].v,e_bak[i].c));
                e_mst[e_bak[i].v].push_back(make_pair(e_bak[i].u,e_bak[i].c));
            }
        }
        dep[1]=1;
        dfs(1);
    }
}
void init()
{
    scanf("%d%d",&n,&m);
    logn=0;
    while(1<<logn<n)
        logn++;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);
        e[i].id=i;
    }
    MST::solve();
    for(int i=1;i<=n;i++)
    {
        anc[0][i]=fa[i];
    }
    for(int i=1;i<=logn;i++)
        for(int j=1;j<=n;j++)
        {
            anc[i][j]=anc[i-1][anc[i-1][j].first];
            anc[i][j].second=max(anc[i][j].second,anc[i-1][j].second);
        }
    for(int i=0;i<=logn;i++)
        for(int j=1;j<=n;j++)
            lim[i][j]=INF;
}
pii LCA(int a,int b,int c)
{
    int mx=0;
    if(dep[a]>dep[b])
        swap(a,b);
    int offset=dep[b]-dep[a];
    for(int i=logn;i>=0;i--)
    {
        if((offset>>i)&1)
        {
            mx=max(mx,anc[i][b].second);
            lim[i][b]=min(lim[i][b],c);
            b=anc[i][b].first;
        }
    }
    if(a==b)
        return make_pair(a,mx);
    for(int i=logn;i>=0;i--)
    {
        if(anc[i][a].first!=anc[i][b].first)
        {
            mx=max(mx,max(anc[i][a].second,anc[i][b].second));
            lim[i][a]=min(lim[i][a],c);
            lim[i][b]=min(lim[i][b],c);
            a=anc[i][a].first;
            b=anc[i][b].first;
        }
    }
    mx=max(mx,max(fa[a].second,fa[b].second));
    lim[0][a]=min(lim[0][a],c);
    lim[0][b]=min(lim[0][b],c);
    return make_pair(fa[a].first,mx);
}
void solve()
{
    for(int i=1;i<=m;i++)
    {
        if(!in_mst[i])
        {
            ans[i]=LCA(e[i].u,e[i].v,e[i].c).second-1;
        }
    }
    for(int i=logn;i;i--)
        for(int j=1;j<=n;j++)
        {
            lim[i-1][j]=min(lim[i-1][j],lim[i][j]);
            lim[i-1][anc[i-1][j].first]=min(lim[i-1][anc[i-1][j].first],lim[i][j]);
        }
    for(int i=1;i<=m;i++)
    {
        if(in_mst[i])
        {
            if(fa[e[i].u].first==e[i].v)
                ans[i]=lim[0][e[i].u];
            else
                ans[i]=lim[0][e[i].v];
            if(ans[i]==INF)
                ans[i]=0;
            ans[i]--;
        }
    }
    for(int i=1;i<=m;i++)
        printf("%d ",ans[i]);
    printf("\n");
}
int main()
{
    init();
    solve();
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MAXN=2e5+10,MAXLOGN=20,INF=0x7FFFFFF;
int n,logn,m,dep[MAXN],ans[MAXN];
pii fa[MAXN],anc[MAXLOGN][MAXN];
int lim[MAXLOGN][MAXN];
bool in_mst[MAXN];
struct Edge
{
    int u,v,c,id;
};
Edge e[MAXN];
namespace MST
{
    namespace DSU
    {
        int fa[MAXN];
        int find(int x)
        {
            return fa[x]==x?x:fa[x]=find(fa[x]);
        }
        bool merge(int a,int b)
        {
            a=find(a);
            b=find(b);
            if(a!=b)
                return fa[a]=b,true;
            else
                return false;
        }
        void init()
        {
            for(int i=1;i<=n;i++)
                fa[i]=i;
        }
    }
    Edge e_bak[MAXN];
    vector<pii> e_mst[MAXN];
    bool cmp(Edge a,Edge b)
    {
        return a.c<b.c;
    }
    void dfs(int x)
    {
        for(vector<pii>::iterator i=e_mst[x].begin();i<e_mst[x].end();i++)
        {
            if(i->first!=fa[x].first)
            {
                dep[i->first]=dep[x]+1;
                fa[i->first]=make_pair(x,i->second);
                dfs(i->first);
            }
        }
    }
    void solve()
    {
        for(int i=1;i<=m;i++)
            e_bak[i]=e[i];
        sort(e_bak+1,e_bak+m+1,cmp);
        DSU::init();
        for(int i=1;i<=n;i++)
        {
            if(DSU::merge(e_bak[i].u,e_bak[i].v))
            {
                in_mst[e_bak[i].id]=true;
                e_mst[e_bak[i].u].push_back(make_pair(e_bak[i].v,e_bak[i].c));
                e_mst[e_bak[i].v].push_back(make_pair(e_bak[i].u,e_bak[i].c));
            }
        }
        dep[1]=1;
        dfs(1);
    }
}
void init()
{
    scanf("%d%d",&n,&m);
    logn=0;
    while(1<<logn<n)
        logn++;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);
        e[i].id=i;
    }
    MST::solve();
    for(int i=1;i<=n;i++)
    {
        anc[0][i]=fa[i];
        lim[0][i]=INF;
    }
    for(int i=1;i<=logn;i++)
        for(int j=1;j<=n;j++)
        {
            anc[i][j]=anc[i-1][anc[i-1][j].first];
            anc[i][j].second=max(anc[i][j].second,anc[i-1][j].second);
            lim[i][j]=INF;
        }
}
pii LCA(int a,int b,int c)
{
    int mi=INF;
    if(dep[a]>dep[b])
        swap(a,b);
    int offset=dep[b]-dep[a];
    for(int i=logn;i>=0;i--)
    {
        if((offset>>i)&1)
        {
            mi=min(mi,anc[i][b].second);
            lim[i][b]=min(lim[i][b],c);
            b=anc[i][b].first;
        }
    }
    if(a==b)
        return make_pair(a,mi);
    for(int i=logn;i>=0;i--)
    {
        if(anc[i][a].first!=anc[i][b].first)
        {
            mi=min(mi,min(anc[i][a].second,anc[i][b].second));
            lim[i][a]=min(lim[i][a],c);
            lim[i][b]=min(lim[i][b],c);
            a=anc[i][a].first;
            b=anc[i][b].first;
        }
    }
    mi=min(mi,min(fa[a].second,fa[b].second));
    lim[0][a]=min(lim[0][a],c);
    lim[0][b]=min(lim[0][b],c);
    return make_pair(fa[a].first,mi);
}
void solve()
{
    for(int i=1;i<=m;i++)
    {
        if(!in_mst[i])
        {
            ans[i]=LCA(e[i].u,e[i].v,e[i].c).second-1;
        }
    }
    for(int i=logn;i;i--)
        for(int j=1;j<=n;j++)
        {
            lim[i-1][j]=min(lim[i-1][j],lim[i][j]);
            lim[i-1][anc[i-1][j].first]=min(lim[i-1][anc[i-1][j].first],lim[i][j]);
        }
    for(int i=1;i<=m;i++)
    {
        if(in_mst[i])
        {
            if(fa[e[i].u].first==e[i].v)
                ans[i]=lim[0][e[i].u];
            else
                ans[i]=lim[0][e[i].v];
            if(ans[i]==INF)
                ans[i]=0;
            ans[i]--;
        }
    }
    for(int i=1;i<=m;i++)
        printf("%d ",ans[i]);
    printf("\n");
}
int main()
{
    init();
    solve();
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MAXN=2e5+10,MAXLOGN=20,INF=0x7FFFFFF;
int n,logn,m,dep[MAXN],ans[MAXN];
pii fa[MAXN],anc[MAXLOGN][MAXN];
int lim[MAXLOGN][MAXN];
bool in_mst[MAXN];
struct Edge
{
    int u,v,c,id;
};
Edge e[MAXN];
namespace MST
{
    namespace DSU
    {
        int fa[MAXN];
        int find(int x)
        {
            return fa[x]==x?x:fa[x]=find(fa[x]);
        }
        bool merge(int a,int b)
        {
            a=find(a);
            b=find(b);
            if(a!=b)
                return fa[a]=b,true;
            else
                return false;
        }
        void init()
        {
            for(int i=1;i<=n;i++)
                fa[i]=i;
        }
    }
    Edge e_bak[MAXN];
    vector<pii> e_mst[MAXN];
    bool cmp(Edge a,Edge b)
    {
        return a.c<b.c;
    }
    void dfs(int x)
    {
        for(vector<pii>::iterator i=e_mst[x].begin();i<e_mst[x].end();i++)
        {
            if(i->first!=fa[x].first)
            {
                dep[i->first]=dep[x]+1;
                fa[i->first]=make_pair(x,i->second);
                dfs(i->first);
            }
        }
    }
    void solve()
    {
        for(int i=1;i<=n;i++)
            e_bak[i]=e[i];
        sort(e_bak+1,e_bak+n+1,cmp);
        DSU::init();
        for(int i=1;i<=n;i++)
        {
            if(DSU::merge(e_bak[i].u,e_bak[i].v))
            {
                in_mst[e_bak[i].id]=true;
                e_mst[e_bak[i].u].push_back(make_pair(e_bak[i].v,e_bak[i].c));
                e_mst[e_bak[i].v].push_back(make_pair(e_bak[i].u,e_bak[i].c));
            }
        }
        dep[1]=1;
        dfs(1);
    }
}
void init()
{
    scanf("%d%d",&n,&m);
    logn=0;
    while(1<<logn<n)
        logn++;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);
        e[i].id=i;
    }
    MST::solve();
    for(int i=1;i<=n;i++)
    {
        anc[0][i]=fa[i];
        lim[0][i]=INF;
    }
    for(int i=1;i<=logn;i++)
        for(int j=1;j<=n;j++)
        {
            anc[i][j]=anc[i-1][anc[i-1][j].first];
            anc[i][j].second=max(anc[i][j].second,anc[i-1][j].second);
            lim[i][j]=INF;
        }
}
pii LCA(int a,int b,int c)
{
    int mi=INF;
    if(dep[a]>dep[b])
        swap(a,b);
    int offset=dep[b]-dep[a];
    for(int i=logn;i>=0;i--)
    {
        if((offset>>i)&1)
        {
            mi=min(mi,anc[i][b].second);
            lim[i][b]=min(lim[i][b],c);
            b=anc[i][b].first;
        }
    }
    if(a==b)
        return make_pair(a,mi);
    for(int i=logn;i>=0;i--)
    {
        if(anc[i][a].first!=anc[i][b].first)
        {
            mi=min(mi,min(anc[i][a].second,anc[i][b].second));
            lim[i][a]=min(lim[i][a],c);
            lim[i][b]=min(lim[i][b],c);
            a=anc[i][a].first;
            b=anc[i][b].first;
        }
    }
    mi=min(mi,min(fa[a].second,fa[b].second));
    lim[0][a]=min(lim[0][a],c);
    lim[0][b]=min(lim[0][b],c);
    return make_pair(fa[a].first,mi);
}
void solve()
{
    for(int i=1;i<=m;i++)
    {
        if(!in_mst[i])
        {
            ans[i]=LCA(e[i].u,e[i].v,e[i].c).second-1;
        }
    }
    for(int i=logn;i;i--)
        for(int j=1;j<=n;j++)
        {
            lim[i-1][j]=min(lim[i-1][j],lim[i][j]);
            lim[i-1][anc[i-1][j].first]=min(lim[i-1][anc[i-1][j].first],lim[i][j]);
        }
    for(int i=1;i<=m;i++)
    {
        if(in_mst[i])
        {
            if(fa[e[i].u].first==e[i].v)
                ans[i]=lim[0][e[i].u];
            else
                ans[i]=lim[0][e[i].v];
            if(ans[i]==INF)
                ans[i]=0;
            ans[i]--;
        }
    }
    for(int i=1;i<=m;i++)
        printf("%d ",ans[i]);
    printf("\n");
}
int main()
{
    init();
    solve();
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MAXN=2e5+10,MAXLOGN=20,INF=0x7FFFFFF;
int n,logn,m,dep[MAXN],ans[MAXN];
pii fa[MAXN],anc[MAXLOGN][MAXN];
int lim[MAXLOGN][MAXN];
bool in_mst[MAXN];
struct Edge
{
    int u,v,c,id;
};
Edge e[MAXN];
namespace MST
{
    namespace DSU
    {
        int fa[MAXN];
        int find(int x)
        {
            return fa[x]==x?x:fa[x]=find(fa[x]);
        }
        bool merge(int a,int b)
        {
            a=find(a);
            b=find(b);
            if(a!=b)
                return fa[a]=b,true;
            else
                return false;
        }
        void init()
        {
            for(int i=1;i<=n;i++)
                fa[i]=i;
        }
    }
    Edge e_bak[MAXN];
    vector<pii> e_mst[MAXN];
    bool cmp(Edge a,Edge b)
    {
        return a.c<b.c;
    }
    void dfs(int x)
    {
        for(vector<pii>::iterator i=e_mst[x].begin();i<e_mst[x].end();i++)
        {
            if(i->first!=fa[x].first)
            {
                dep[i->first]=dep[x]+1;
                fa[i->first]=make_pair(x,i->second);
                dfs(i->first);
            }
        }
    }
    void solve()
    {
        for(int i=1;i<=n;i++)
            e_bak[i]=e[i];
        sort(e_bak+1,e_bak+n+1,cmp);
        DSU::init();
        for(int i=1;i<=n;i++)
        {
            if(DSU::merge(e_bak[i].u,e_bak[i].v))
            {
                in_mst[e_bak[i].id]=true;
                e_mst[e_bak[i].u].push_back(make_pair(e_bak[i].v,e_bak[i].c));
                e_mst[e_bak[i].v].push_back(make_pair(e_bak[i].u,e_bak[i].c));
            }
        }
        dep[1]=1;
        dfs(1);
    }
}
void init()
{
    scanf("%d%d",&n,&m);
    logn=0;
    while(1<<logn<n)
        logn++;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);
        e[i].id=i;
    }
    MST::solve();
    for(int i=1;i<=n;i++)
    {
        anc[0][i]=fa[i];
        lim[0][i]=INF;
    }
    for(int i=1;i<=logn;i++)
        for(int j=1;j<=n;j++)
        {
            anc[i][j]=anc[i-1][anc[i-1][j].first];
            anc[i][j].second=max(anc[i][j].second,anc[i-1][j].second);
            lim[i][j]=INF;
        }
}
pii LCA(int a,int b,int c)
{
    int mi=INF;
    if(dep[a]>dep[b])
        swap(a,b);
    int offset=b-a;
    for(int i=logn;i>=0;i--)
    {
        if((offset>>i)&1)
        {
            mi=min(mi,anc[i][b].second);
            lim[i][b]=min(lim[i][b],c);
            b=anc[i][b].first;
        }
    }
    if(a==b)
        return make_pair(a,mi);
    for(int i=logn;i>=0;i--)
    {
        if(anc[i][a].first!=anc[i][b].first)
        {
            mi=min(mi,min(anc[i][a].second,anc[i][b].second));
            lim[i][a]=min(lim[i][a],c);
            lim[i][b]=min(lim[i][b],c);
            a=anc[i][a].first;
            b=anc[i][b].first;
        }
    }
    mi=min(mi,min(fa[a].second,fa[b].second));
    return make_pair(fa[a].first,mi);
}
void solve()
{
    for(int i=1;i<=m;i++)
    {
        if(!in_mst[i])
        {
            ans[i]=LCA(e[i].u,e[i].v,e[i].c).second-1;
        }
    }
    for(int i=logn;i;i--)
        for(int j=1;j<=n;j++)
        {
            lim[i-1][j]=min(lim[i-1][j],lim[i][j]);
            lim[i-1][anc[i-1][j].first]=min(lim[i-1][anc[i-1][j].first],lim[i][j]);
        }
    for(int i=1;i<=m;i++)
    {
        if(in_mst[i])
        {
            if(fa[e[i].u].first==e[i].v)
                ans[i]=lim[0][e[i].u];
            else
                ans[i]=lim[0][e[i].v];
            if(ans[i]==INF)
                ans[i]=0;
            ans[i]--;
        }
    }
    for(int i=1;i<=m;i++)
        printf("%d ",ans[i]);
    printf("\n");
}
int main()
{
    init();
    solve();
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MAXN=2e5+10,MAXLOGN=20,INF=0x7FFFFFFF;
int n,logn,m,dep[MAXN],ans[MAXN];
pii fa[MAXN],anc[MAXLOGN][MAXN];
int lim[MAXLOGN][MAXN];
bool in_mst[MAXN];
struct Edge
{
    int u,v,c,id;
};
Edge e[MAXN];
namespace MST
{
    namespace DSU
    {
        int fa[MAXN];
        int find(int x)
        {
            return fa[x]==x?x:fa[x]=find(fa[x]);
        }
        bool merge(int a,int b)
        {
            a=find(a);
            b=find(b);
            if(a!=b)
                return fa[a]=b,true;
            else
                return false;
        }
        void init()
        {
            for(int i=1;i<=n;i++)
                fa[i]=i;
        }
    }
    Edge e_bak[MAXN];
    vector<pii> e_mst[MAXN];
    bool cmp(Edge a,Edge b)
    {
        return a.c<b.c;
    }
    void dfs(int x)
    {
        for(vector<pii>::iterator i=e_mst[x].begin();i<e_mst[x].end();i++)
        {
            if(i->first!=fa[x].first)
            {
                dep[i->first]=dep[x]+1;
                fa[i->first]=make_pair(x,i->second);
                dfs(i->first);
            }
        }
    }
    void solve()
    {
        for(int i=1;i<=m;i++)
            e_bak[i]=e[i];
        sort(e_bak+1,e_bak+m+1,cmp);
        DSU::init();
        for(int i=1;i<=m;i++)
        {
            if(DSU::merge(e_bak[i].u,e_bak[i].v))
            {
                in_mst[e_bak[i].id]=true;
                e_mst[e_bak[i].u].push_back(make_pair(e_bak[i].v,e_bak[i].c));
                e_mst[e_bak[i].v].push_back(make_pair(e_bak[i].u,e_bak[i].c));
            }
        }
        dep[1]=1;
        dfs(1);
    }
}
void init()
{
    scanf("%d%d",&n,&m);
    logn=0;
    while(1<<logn<n)
        logn++;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);
        e[i].id=i;
    }
    MST::solve();
    for(int i=1;i<=n;i++)
    {
        anc[0][i]=fa[i];
    }
    for(int i=1;i<=logn;i++)
        for(int j=1;j<=n;j++)
        {
            anc[i][j]=anc[i-1][anc[i-1][j].first];
            anc[i][j].second=max(anc[i][j].second,anc[i-1][j].second);
        }
    for(int i=0;i<=logn;i++)
        for(int j=1;j<=n;j++)
            lim[i][j]=INF;
}
pii LCA(int a,int b,int c)
{
    int mx=0;
    if(dep[a]>dep[b])
        swap(a,b);
    int offset=dep[b]-dep[a];
    for(int i=logn;i>=0;i--)
    {
        if((offset>>i)&1)
        {
            mx=max(mx,anc[i][b].second);
            lim[i][b]=min(lim[i][b],c);
            b=anc[i][b].first;
        }
    }
    if(a==b)
        return make_pair(a,mx);
    for(int i=logn;i>=0;i--)
    {
        if(anc[i][a].first!=anc[i][b].first)
        {
            mx=max(mx,max(anc[i][a].second,anc[i][b].second));
            lim[i][a]=min(lim[i][a],c);
            lim[i][b]=min(lim[i][b],c);
            a=anc[i][a].first;
            b=anc[i][b].first;
        }
    }
    mx=max(mx,max(fa[a].second,fa[b].second));
    lim[0][a]=min(lim[0][a],c);
    lim[0][b]=min(lim[0][b],c);
    return make_pair(fa[a].first,mx);
}
void solve()
{
    for(int i=1;i<=m;i++)
    {
        if(!in_mst[i])
        {
            ans[i]=LCA(e[i].u,e[i].v,e[i].c).second-1;
        }
    }
    for(int i=logn;i;i--)
        for(int j=1;j<=n;j++)
        {
            lim[i-1][j]=min(lim[i-1][j],lim[i][j]);
            lim[i-1][anc[i-1][j].first]=min(lim[i-1][anc[i-1][j].first],lim[i][j]);
        }
    for(int i=1;i<=m;i++)
    {
        if(in_mst[i])
        {
            if(fa[e[i].u].first==e[i].v)
                ans[i]=lim[0][e[i].u];
            else
                ans[i]=lim[0][e[i].v];
            if(ans[i]==INF)
                ans[i]=0;
            ans[i]--;
        }
    }
    for(int i=1;i<=m;i++)
        printf("%d ",ans[i]);
    printf("\n");
}
int main()
{
    init();
    solve();
}


#include <iostream>
#include <queue>
#include <unordered_set>
#include <algorithm>
using namespace std;
priority_queue <pair<int,pair<int,int> > > pq;
unordered_set<int> us;
queue <int> q;
vector<pair<int,int >  > /*vv[200005],*/graf[200005],legit[200005];
int tata[200005],sz[200005],n,m,nivel[1000005],euclid[1000005],cnt,fr[200005],rmq[1000005][22],loga[1000005],put[1000005],nex[200005][22],str[200005][22],tata_next[200005];
long long costuri[200005];
long long solutie[1000005];
pair<int,pair<int,int> >to_upd[1000005];

long long cost;
pair<int,pair<int,int> > v[2000005];
void unite(int a,int b,int c){
    int cp=a;
    while(cp!=tata[cp]){
        cp=tata[cp];
    }
    int cp1=a;
    while(cp1!=cp){
        int aux=tata[cp1];
        tata[cp1]=cp;
        cp1=aux;
    }
    cp=b;
    cp1=b;
    while(cp!=tata[cp]){
        cp=tata[cp];
    }
    while(cp1!=cp){
        int aux=tata[cp1];
        tata[cp1]=cp;
        cp1=aux;
    }
    if(tata[a]==tata[b]){
        return;
    }
    else
    if(sz[tata[a]]>sz[tata[b]]){
        tata[tata[b]]=tata[a];
        sz[tata[a]]+=sz[tata[b]];
    }
    else{
        tata[tata[a]]=tata[b];
        sz[tata[b]]+=sz[tata[a]];
    }
    cost+=c;
    graf[a].push_back(make_pair(b,c));
    graf[b].push_back(make_pair(a,c));
}
void dfs(int nod,int niv){
    nivel[++cnt]=niv;
    euclid[cnt]=nod;
    if(fr[nod]==0){
        fr[nod]=cnt;
    }
    for(int i=0;i<legit[nod].size();i++){
        dfs(legit[nod][i].first,niv+1);
        nivel[++cnt]=niv;
        euclid[cnt]=nod;
    }
}
int find_lca(int a,int b){
    int st=fr[a],dr=fr[b];
    if(st>dr)
        swap(st,dr);
    int dist=dr-st+1;
    if(nivel[rmq[st][loga[dist]]]<nivel[rmq[dr-put[loga[dist]]+1][loga[dist]]]){
        return euclid[rmq[st][loga[dist]]];
    }
    return euclid[rmq[dr-put[loga[dist]]+1][loga[dist]]];
}
long long mi(long long a,long long b){
    if(a>b)
        return b;
    return a;
}
long long ma(long long a,long long b){
    if(a>b)
        return a;
    return b;
}
long long find_max(int down,int up){
    if(nivel[fr[down]]<nivel[fr[up]])
        swap(up,down);
    int dist=nivel[fr[down]]-nivel[fr[up]],maxim=0;
    while(dist!=0){
        int e=loga[dist];
        maxim=ma(str[down][e],maxim);
        down=nex[down][e];
        dist-=put[e];
    }
    return maxim;
}
bool exista(int ind){
    int a=v[ind].first,b=v[ind].second.first;
    for(int i=0;i<legit[a].size();i++)
        if(legit[a][i].first==b)
            return true;
    swap(a,b);
    for(int i=0;i<legit[a].size();i++)
        if(legit[a][i].first==b)
            return true;
    return false;
}
bool mysort(pair<int,pair<int,int> > a, pair<int,pair<int,int> > b){
    return a.first<b.first;
}
void update(int down,int up,int val){
    int cop=down;
    //cout<<"-="<<down<<" "<<up<<"\n";
    while(cop!=up){
        int aux=tata_next[cop];
        if(aux==tata[cop] and costuri[cop]==10000000000){
            costuri[cop]=val;
        }
        cop=aux;
    }
    cop=down;
    while(cop!=up){
        int aux=tata_next[cop];
        tata_next[cop]=up;
        cop=aux;
    }
}
int find_lca_inef(int up, int down){
    while(up!=down){
        if(nivel[fr[up]]>nivel[fr[down]]){
            up=tata_next[up];
        }
        else
            down=tata_next[down];
    }
    return up;
}
int main()
{

    int a,b,c,contt=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        costuri[i]=10000000000;
    for(int i=1;i<=m;i++)
        solutie[i]=-1;
    for(int i=1;i<=m;i++){
        cin>>v[i].first>>v[i].second.first>>v[i].second.second;
        a=v[i].first;
        b=v[i].second.first;
        c=v[i].second.second;
        pq.push(make_pair(-c,make_pair(a,b)));
        //vv[a].push_back(make_pair(b,c));
        //vv[b].push_back(make_pair(a,c));
    }
    for(int i=1;i<=n;i++)
        tata[i]=i;
    for(int i=1;i<=n;i++)
        sz[i]=1;
    while(pq.empty()==false){
        pair<int,pair<int,int> >x=pq.top();
        pq.pop();
        unite(x.second.first,x.second.second,-x.first);
    }
    q.push(1);
    us.insert(1);
    while(q.empty()==false){
        int nod=q.front();
        q.pop();
        for(int i=0;i<graf[nod].size();i++){
            if(us.count(graf[nod][i].first)==0){
                legit[nod].push_back(graf[nod][i]);
                us.insert(graf[nod][i].first);
                q.push(graf[nod][i].first);
            }
        }
        graf[nod].clear();
    }
    //for(int i=1;i<=n;i++)
      //  graf[i].clear();
    dfs(1,1);
    for(int i=1;i<=cnt;i++){
        rmq[i][0]=i;
    }
    put[0]=1;
    //loga[1]=1;
    for(int i=1;i<=25;i++){
        put[i]=put[i-1]*2;
        if(put[i]<=cnt)
        loga[put[i]]=1;
    }
    for(int i=1;i<=cnt;i++){
        loga[i]+=loga[i-1];
    }
    for(int j=1;j<=20;j++){
        for(int i=1;i<=cnt;i++){
            if(i+put[j]<=cnt)
            if(nivel[rmq[i][j-1]]<nivel[rmq[i+put[j-1]][j-1]]){
                rmq[i][j]=rmq[i][j-1];
            }
            else
                rmq[i][j]=rmq[i+put[j-1]][j-1];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<legit[i].size();j++){
            nex[legit[i][j].first][0]=i;
            str[legit[i][j].first][0]=legit[i][j].second;
        }
    }

    nex[1][0]=1;
    for(int j=1;j<=20;j++){
        for(int i=1;i<=n;i++){
            nex[i][j]=nex[nex[i][j-1]][j-1];
            str[i][j]=ma(str[i][j-1],str[nex[i][j-1]][j-1]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<legit[i].size();j++){
            tata_next[legit[i][j].first]=i;
            tata[legit[i][j].first]=i;
        }
    }
    tata[1]=1;
    tata_next[1]=1;
    for(int i=1;i<=m;i++){
        int a=v[i].first,b=v[i].second.first;
        int lca=find_lca(a,b);
        /*if(nivel[fr[lca]]>nivel[fr[tata_next[a]]] or nivel[fr[lca]]>nivel[fr[tata_next[b]]]){
            if(nivel[fr[tata_next[a]]]>nivel[fr[tata_next[b]]]){
                lca=tata_next[b];
            }
            else
                lca=tata_next[a];
        }
        */
        long long maxim=ma(find_max(a,lca),find_max(b,lca));
        if(exista(i)==false){
            //cout<<maxim<<"\n";
            solutie[i]=maxim;
            to_upd[++contt]=make_pair(v[i].second.second,make_pair(a,b));
        }
    }
    sort(to_upd+1,to_upd+contt+1,mysort);
    //for(int i=1;i<=n;i++)
      //  cout<<tata[i]<<" ";
    //cout<<"\n\n";
    //cout<<contt<<"\n";
    //for(int i=1;i<=contt;i++)
      //  cout<<to_upd[i].first<<" "<<to_upd[i].second.first<<" "<<to_upd[i].second.second<<"\n";
    //cout<<"\n\n";
    //cout<<contt<<" "<<"\n";
    for(int i=1;i<=contt;i++){
        int a=to_upd[i].second.second;
        int b=to_upd[i].second.first;
        int lca=find_lca_inef(a,b);
        //cout<<"GIGEL\n";
        update(a,lca,to_upd[i].first);
        update(b,lca,to_upd[i].first);
    }
    for(int i=1;i<=m;i++){
        if(solutie[i]==-1){
            int a=v[i].first;
            int b=v[i].second.first;
            if(nivel[fr[a]]>nivel[fr[b]]){
                swap(a,b);
            }
            solutie[i]=costuri[b];
        }
    }
    for(int i=1;i<=m;i++){
        if(solutie[i]==10000000000)
            cout<<-1<<" ";
        else
            cout<<solutie[i]-1<<" ";
    }

    return 0;
}
/**
3 3
1 2 8
2 3 3
3 1 4

5 5
1 2 5
1 3 5
1 4 5
1 5 5
2 3 5
HEEEEEELU
*/


#include <iostream>
#include <queue>
#include <unordered_set>
#include <algorithm>
using namespace std;
priority_queue <pair<int,pair<int,int> > > pq;
unordered_set<int> us;
queue <int> q;
vector<pair<int,int >  > /*vv[100005],*/graf[100005],legit[100005];
int tata[100005],sz[100005],n,m,nivel[1000005],euclid[1000005],cnt,fr[100005],rmq[1000005][22],loga[1000005],put[1000005],nex[100005][22],str[100005][22],tata_next[100005],costuri[100005];
long long solutie[1000005];
pair<int,pair<int,int> >to_upd[1000005];

long long cost;
pair<int,pair<int,int> > v[1000005];
void unite(int a,int b,int c){
    int cp=a;
    while(cp!=tata[cp]){
        cp=tata[cp];
    }
    int cp1=a;
    while(cp1!=cp){
        int aux=tata[cp1];
        tata[cp1]=cp;
        cp1=aux;
    }
    cp=b;
    cp1=b;
    while(cp!=tata[cp]){
        cp=tata[cp];
    }
    while(cp1!=cp){
        int aux=tata[cp1];
        tata[cp1]=cp;
        cp1=aux;
    }
    if(tata[a]==tata[b]){
        return;
    }
    else
    if(sz[tata[a]]>sz[tata[b]]){
        tata[tata[b]]=tata[a];
        sz[tata[a]]+=sz[tata[b]];
    }
    else{
        tata[tata[a]]=tata[b];
        sz[tata[b]]+=sz[tata[a]];
    }
    cost+=c;
    graf[a].push_back(make_pair(b,c));
    graf[b].push_back(make_pair(a,c));
}
void dfs(int nod,int niv){
    nivel[++cnt]=niv;
    euclid[cnt]=nod;
    if(fr[nod]==0){
        fr[nod]=cnt;
    }
    for(int i=0;i<legit[nod].size();i++){
        dfs(legit[nod][i].first,niv+1);
        nivel[++cnt]=niv;
        euclid[cnt]=nod;
    }
}
int find_lca(int a,int b){
    int st=fr[a],dr=fr[b];
    if(st>dr)
        swap(st,dr);
    int dist=dr-st+1;
    if(nivel[rmq[st][loga[dist]]]<nivel[rmq[dr-put[loga[dist]]+1][loga[dist]]]){
        return euclid[rmq[st][loga[dist]]];
    }
    return euclid[rmq[dr-put[loga[dist]]+1][loga[dist]]];
}
long long mi(long long a,long long b){
    if(a>b)
        return b;
    return a;
}
long long ma(long long a,long long b){
    if(a>b)
        return a;
    return b;
}
long long find_max(int down,int up){
    if(nivel[fr[down]]<nivel[fr[up]])
        swap(up,down);
    int dist=nivel[fr[down]]-nivel[fr[up]],maxim=0;
    while(dist!=0){
        int e=loga[dist];
        maxim=ma(str[down][e],maxim);
        down=nex[down][e];
        dist-=put[e];
    }
    return maxim;
}
bool exista(int ind){
    int a=v[ind].first,b=v[ind].second.first;
    for(int i=0;i<legit[a].size();i++)
        if(legit[a][i].first==b)
            return true;
    swap(a,b);
    for(int i=0;i<legit[a].size();i++)
        if(legit[a][i].first==b)
            return true;
    return false;
}
bool mysort(pair<int,pair<int,int> > a, pair<int,pair<int,int> > b){
    return a.first<b.first;
}
void update(int down,int up,int val){
    int cop=down;
    //cout<<"-="<<down<<" "<<up<<"\n";
    while(cop!=up){
        int aux=tata_next[cop];
        if(aux==tata[cop] and costuri[cop]==10000000000){
            costuri[cop]=val;
        }
        cop=aux;
    }
    cop=down;
    while(cop!=up){
        int aux=tata_next[cop];
        tata_next[cop]=up;
        cop=aux;
    }
}
int find_lca_inef(int up, int down){
    while(up!=down){
        if(nivel[fr[up]]>nivel[fr[down]]){
            up=tata_next[up];
        }
        else
            down=tata_next[down];
    }
    return up;
}
int main()
{

    int a,b,c,contt=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        costuri[i]=10000000000;
    for(int i=1;i<=m;i++)
        solutie[i]=-1;
    for(int i=1;i<=m;i++){
        cin>>v[i].first>>v[i].second.first>>v[i].second.second;
        a=v[i].first;
        b=v[i].second.first;
        c=v[i].second.second;
        pq.push(make_pair(-c,make_pair(a,b)));
        //vv[a].push_back(make_pair(b,c));
        //vv[b].push_back(make_pair(a,c));
    }
    for(int i=1;i<=n;i++)
        tata[i]=i;
    for(int i=1;i<=n;i++)
        sz[i]=1;
    while(pq.empty()==false){
        pair<int,pair<int,int> >x=pq.top();
        pq.pop();
        unite(x.second.first,x.second.second,-x.first);
    }
    q.push(1);
    us.insert(1);
    while(q.empty()==false){
        int nod=q.front();
        q.pop();
        for(int i=0;i<graf[nod].size();i++){
            if(us.count(graf[nod][i].first)==0){
                legit[nod].push_back(graf[nod][i]);
                us.insert(graf[nod][i].first);
                q.push(graf[nod][i].first);
            }
        }
        graf[nod].clear();
    }
    //for(int i=1;i<=n;i++)
      //  graf[i].clear();
    dfs(1,1);
    for(int i=1;i<=cnt;i++){
        rmq[i][0]=i;
    }
    put[0]=1;
    //loga[1]=1;
    for(int i=1;i<=25;i++){
        put[i]=put[i-1]*2;
        if(put[i]<=cnt)
        loga[put[i]]=1;
    }
    for(int i=1;i<=cnt;i++){
        loga[i]+=loga[i-1];
    }
    for(int j=1;j<=20;j++){
        for(int i=1;i<=cnt;i++){
            if(i+put[j]<=cnt)
            if(nivel[rmq[i][j-1]]<nivel[rmq[i+put[j-1]][j-1]]){
                rmq[i][j]=rmq[i][j-1];
            }
            else
                rmq[i][j]=rmq[i+put[j-1]][j-1];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<legit[i].size();j++){
            nex[legit[i][j].first][0]=i;
            str[legit[i][j].first][0]=legit[i][j].second;
        }
    }

    nex[1][0]=1;
    for(int j=1;j<=20;j++){
        for(int i=1;i<=n;i++){
            nex[i][j]=nex[nex[i][j-1]][j-1];
            str[i][j]=ma(str[i][j-1],str[nex[i][j-1]][j-1]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<legit[i].size();j++){
            tata_next[legit[i][j].first]=i;
            tata[legit[i][j].first]=i;
        }
    }
    tata[1]=1;
    tata_next[1]=1;
    for(int i=1;i<=m;i++){
        int a=v[i].first,b=v[i].second.first;
        int lca=find_lca(a,b);
        /*if(nivel[fr[lca]]>nivel[fr[tata_next[a]]] or nivel[fr[lca]]>nivel[fr[tata_next[b]]]){
            if(nivel[fr[tata_next[a]]]>nivel[fr[tata_next[b]]]){
                lca=tata_next[b];
            }
            else
                lca=tata_next[a];
        }
        */
        long long maxim=ma(find_max(a,lca),find_max(b,lca));
        if(exista(i)==false){
            //cout<<maxim<<"\n";
            solutie[i]=maxim;
            to_upd[++contt]=make_pair(v[i].second.second,make_pair(a,b));
        }
    }
    sort(to_upd+1,to_upd+contt+1,mysort);
    //for(int i=1;i<=n;i++)
      //  cout<<tata[i]<<" ";
    //cout<<"\n\n";
    //cout<<contt<<"\n";
    //for(int i=1;i<=contt;i++)
      //  cout<<to_upd[i].first<<" "<<to_upd[i].second.first<<" "<<to_upd[i].second.second<<"\n";
    //cout<<"\n\n";
    //cout<<contt<<" "<<"\n";
    for(int i=1;i<=contt;i++){
        int a=to_upd[i].second.second;
        int b=to_upd[i].second.first;
        int lca=find_lca_inef(a,b);
        //cout<<"GIGEL\n";
        update(a,lca,to_upd[i].first);
        update(b,lca,to_upd[i].first);
    }
    for(int i=1;i<=m;i++){
        if(solutie[i]==-1){
            int a=v[i].first;
            int b=v[i].second.first;
            if(nivel[fr[a]]>nivel[fr[b]]){
                swap(a,b);
            }
            solutie[i]=costuri[b];
        }
    }
    for(int i=1;i<=m;i++){
        if(solutie[i]==10000000000)
            cout<<-1<<" ";
        else
            cout<<solutie[i]-1<<" ";
    }

    return 0;
}
/**
3 3
1 2 8
2 3 3
3 1 4

5 5
1 2 5
1 3 5
1 4 5
1 5 5
2 3 5
HEEEEEELU
*/


#include <iostream>
#include <queue>
#include <unordered_set>
#include <algorithm>
using namespace std;
priority_queue <pair<int,pair<int,int> > > pq;
unordered_set<int> us;
queue <int> q;
vector<pair<int,int >  > /*vv[100005],*/graf[100005],legit[100005];
int tata[100005],sz[100005],n,m,nivel[1000005],euclid[1000005],cnt,fr[100005],rmq[1000005][22],loga[1000005],put[1000005],nex[100005][22],str[100005][22],tata_next[100005];
long long costuri[100005];
long long solutie[1000005];
pair<int,pair<int,int> >to_upd[1000005];

long long cost;
pair<int,pair<int,int> > v[1000005];
void unite(int a,int b,int c){
    int cp=a;
    while(cp!=tata[cp]){
        cp=tata[cp];
    }
    int cp1=a;
    while(cp1!=cp){
        int aux=tata[cp1];
        tata[cp1]=cp;
        cp1=aux;
    }
    cp=b;
    cp1=b;
    while(cp!=tata[cp]){
        cp=tata[cp];
    }
    while(cp1!=cp){
        int aux=tata[cp1];
        tata[cp1]=cp;
        cp1=aux;
    }
    if(tata[a]==tata[b]){
        return;
    }
    else
    if(sz[tata[a]]>sz[tata[b]]){
        tata[tata[b]]=tata[a];
        sz[tata[a]]+=sz[tata[b]];
    }
    else{
        tata[tata[a]]=tata[b];
        sz[tata[b]]+=sz[tata[a]];
    }
    cost+=c;
    graf[a].push_back(make_pair(b,c));
    graf[b].push_back(make_pair(a,c));
}
void dfs(int nod,int niv){
    nivel[++cnt]=niv;
    euclid[cnt]=nod;
    if(fr[nod]==0){
        fr[nod]=cnt;
    }
    for(int i=0;i<legit[nod].size();i++){
        dfs(legit[nod][i].first,niv+1);
        nivel[++cnt]=niv;
        euclid[cnt]=nod;
    }
}
int find_lca(int a,int b){
    int st=fr[a],dr=fr[b];
    if(st>dr)
        swap(st,dr);
    int dist=dr-st+1;
    if(nivel[rmq[st][loga[dist]]]<nivel[rmq[dr-put[loga[dist]]+1][loga[dist]]]){
        return euclid[rmq[st][loga[dist]]];
    }
    return euclid[rmq[dr-put[loga[dist]]+1][loga[dist]]];
}
long long mi(long long a,long long b){
    if(a>b)
        return b;
    return a;
}
long long ma(long long a,long long b){
    if(a>b)
        return a;
    return b;
}
long long find_max(int down,int up){
    if(nivel[fr[down]]<nivel[fr[up]])
        swap(up,down);
    int dist=nivel[fr[down]]-nivel[fr[up]],maxim=0;
    while(dist!=0){
        int e=loga[dist];
        maxim=ma(str[down][e],maxim);
        down=nex[down][e];
        dist-=put[e];
    }
    return maxim;
}
bool exista(int ind){
    int a=v[ind].first,b=v[ind].second.first;
    for(int i=0;i<legit[a].size();i++)
        if(legit[a][i].first==b)
            return true;
    swap(a,b);
    for(int i=0;i<legit[a].size();i++)
        if(legit[a][i].first==b)
            return true;
    return false;
}
bool mysort(pair<int,pair<int,int> > a, pair<int,pair<int,int> > b){
    return a.first<b.first;
}
void update(int down,int up,int val){
    int cop=down;
    //cout<<"-="<<down<<" "<<up<<"\n";
    while(cop!=up){
        int aux=tata_next[cop];
        if(aux==tata[cop] and costuri[cop]==10000000000){
            costuri[cop]=val;
        }
        cop=aux;
    }
    cop=down;
    while(cop!=up){
        int aux=tata_next[cop];
        tata_next[cop]=up;
        cop=aux;
    }
}
int find_lca_inef(int up, int down){
    while(up!=down){
        if(nivel[fr[up]]>nivel[fr[down]]){
            up=tata_next[up];
        }
        else
            down=tata_next[down];
    }
    return up;
}
int main()
{

    int a,b,c,contt=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        costuri[i]=10000000000;
    for(int i=1;i<=m;i++)
        solutie[i]=-1;
    for(int i=1;i<=m;i++){
        cin>>v[i].first>>v[i].second.first>>v[i].second.second;
        a=v[i].first;
        b=v[i].second.first;
        c=v[i].second.second;
        pq.push(make_pair(-c,make_pair(a,b)));
        //vv[a].push_back(make_pair(b,c));
        //vv[b].push_back(make_pair(a,c));
    }
    for(int i=1;i<=n;i++)
        tata[i]=i;
    for(int i=1;i<=n;i++)
        sz[i]=1;
    while(pq.empty()==false){
        pair<int,pair<int,int> >x=pq.top();
        pq.pop();
        unite(x.second.first,x.second.second,-x.first);
    }
    q.push(1);
    us.insert(1);
    while(q.empty()==false){
        int nod=q.front();
        q.pop();
        for(int i=0;i<graf[nod].size();i++){
            if(us.count(graf[nod][i].first)==0){
                legit[nod].push_back(graf[nod][i]);
                us.insert(graf[nod][i].first);
                q.push(graf[nod][i].first);
            }
        }
        graf[nod].clear();
    }
    //for(int i=1;i<=n;i++)
      //  graf[i].clear();
    dfs(1,1);
    for(int i=1;i<=cnt;i++){
        rmq[i][0]=i;
    }
    put[0]=1;
    //loga[1]=1;
    for(int i=1;i<=25;i++){
        put[i]=put[i-1]*2;
        if(put[i]<=cnt)
        loga[put[i]]=1;
    }
    for(int i=1;i<=cnt;i++){
        loga[i]+=loga[i-1];
    }
    for(int j=1;j<=20;j++){
        for(int i=1;i<=cnt;i++){
            if(i+put[j]<=cnt)
            if(nivel[rmq[i][j-1]]<nivel[rmq[i+put[j-1]][j-1]]){
                rmq[i][j]=rmq[i][j-1];
            }
            else
                rmq[i][j]=rmq[i+put[j-1]][j-1];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<legit[i].size();j++){
            nex[legit[i][j].first][0]=i;
            str[legit[i][j].first][0]=legit[i][j].second;
        }
    }

    nex[1][0]=1;
    for(int j=1;j<=20;j++){
        for(int i=1;i<=n;i++){
            nex[i][j]=nex[nex[i][j-1]][j-1];
            str[i][j]=ma(str[i][j-1],str[nex[i][j-1]][j-1]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<legit[i].size();j++){
            tata_next[legit[i][j].first]=i;
            tata[legit[i][j].first]=i;
        }
    }
    tata[1]=1;
    tata_next[1]=1;
    for(int i=1;i<=m;i++){
        int a=v[i].first,b=v[i].second.first;
        int lca=find_lca(a,b);
        /*if(nivel[fr[lca]]>nivel[fr[tata_next[a]]] or nivel[fr[lca]]>nivel[fr[tata_next[b]]]){
            if(nivel[fr[tata_next[a]]]>nivel[fr[tata_next[b]]]){
                lca=tata_next[b];
            }
            else
                lca=tata_next[a];
        }
        */
        long long maxim=ma(find_max(a,lca),find_max(b,lca));
        if(exista(i)==false){
            //cout<<maxim<<"\n";
            solutie[i]=maxim;
            to_upd[++contt]=make_pair(v[i].second.second,make_pair(a,b));
        }
    }
    sort(to_upd+1,to_upd+contt+1,mysort);
    //for(int i=1;i<=n;i++)
      //  cout<<tata[i]<<" ";
    //cout<<"\n\n";
    //cout<<contt<<"\n";
    //for(int i=1;i<=contt;i++)
      //  cout<<to_upd[i].first<<" "<<to_upd[i].second.first<<" "<<to_upd[i].second.second<<"\n";
    //cout<<"\n\n";
    //cout<<contt<<" "<<"\n";
    for(int i=1;i<=contt;i++){
        int a=to_upd[i].second.second;
        int b=to_upd[i].second.first;
        int lca=find_lca_inef(a,b);
        //cout<<"GIGEL\n";
        update(a,lca,to_upd[i].first);
        update(b,lca,to_upd[i].first);
    }
    for(int i=1;i<=m;i++){
        if(solutie[i]==-1){
            int a=v[i].first;
            int b=v[i].second.first;
            if(nivel[fr[a]]>nivel[fr[b]]){
                swap(a,b);
            }
            solutie[i]=costuri[b];
        }
    }
    for(int i=1;i<=m;i++){
        if(solutie[i]==10000000000)
            cout<<-1<<" ";
        else
            cout<<solutie[i]-1<<" ";
    }

    return 0;
}
/**
3 3
1 2 8
2 3 3
3 1 4

5 5
1 2 5
1 3 5
1 4 5
1 5 5
2 3 5
HEEEEEELU
*/