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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return up[x] = (x == up[x] ? x : find(up[x]));
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  return {par[0][v], max(res, max(emax[0][v], emax[0][u]))};
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  if(sz[v] > sz[nv]) swap(v, nv);
  up[v] = nv;
  v = nv;
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	//cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      //cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	//cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      //cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	//cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        if(!is_father(e.v, e.u)) goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	//cerr << val[i] << " ";
  }
  //cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      //cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      //cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int label[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  return {par[0][v], max(res, max(emax[0][v], emax[0][u]))};
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  if(sz[v] > sz[nv]) swap(v, nv);
  up[v] = nv;
  sz[nv] += sz[v];
  label[nv] = (depth[v] < depth[nv] ? v : nv);
  v = label[nv];
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
    label[i] = i;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	//cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      //cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	//cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      //cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	//cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        if(!is_father(e.v, e.u)) goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	//cerr << val[i] << " ";
  }
  //cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      //cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      //cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  for(int i = LOGN-1; i >= 0; i--) {
  	int tu = (is_father(u, v) ? u : par[i][u]);
    if(tu != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  return {par[0][v], max(res, emax[0][u])};
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  // if(sz[e.u] > sz[neu]) swap(e.u, neu); !!!
  up[v] = nv;
  v = nv;
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        if(!is_father(e.v, e.u)) goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	cerr << val[i] << " ";
  }
  cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u] + is_father(u, v) ) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  else {
    return {par[0][v], max(res, max(emax[0][u], emax[0][v]))};
  }
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  // if(sz[e.u] > sz[neu]) swap(e.u, neu); !!!
  up[v] = nv;
  v = nv;
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	//cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      //cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	//cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      //cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(e.u != e.v) {
      	//cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        if(!is_father(e.v, e.u)) goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	//cerr << val[i] << " ";
  }
  //cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      //cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      //cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


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

const int N = 200123;
const int LOGN = 20;
const int inf = (1<<30);

struct edge {
  int u, v, c, id;

  int get(int x) {
    assert(x == u || x == v);
    return (x == u ? v : u);
  }
};

bool cmp(edge e1, edge e2) {
  return e1.c < e2.c;
}

vector<edge> es, sortedes;

int z, n, m;
int up[N];
int sz[N];
int inmst[N];
int depth[N];
int ans[N];
int val[N];
int eparent[N];
int parent[N];
int pre[N];
int post[N];
int label[N];
int T;

vector<edge> mst[N];
int par[LOGN][N];
int emax[LOGN][N];

int find(int x) {
  return x == up[x] ? x : find(up[x]);
}

void join(int a, int b) {
  a = find(a);
  b = find(b);
  if(sz[a] < sz[b]) swap(a, b);
  up[b] = a;
  sz[a] += sz[b];
}

void dfs(int v, int p, int d = 0) {
  par[0][v] = p;
  depth[v] = d;
  parent[v] = p;
  pre[v] = T++;

  for(edge e : mst[v]) {
    int w = e.get(v);
    if(w == p) continue;
    emax[0][w] = max(emax[0][w], e.c);
    eparent[w] = e.id;
    dfs(w, v, d+1);
  }
  post[v] = T++;
}

bool is_father(int v, int w) {
	return pre[v] <= pre[w] && post[v] >= post[w];
}

void build_lca() {
  for(int i = 1; i < LOGN; i++) {
    for(int v = 1; v <= n; v++) {
      par[i][v] = par[i-1][par[i-1][v]];
      emax[i][v] = max(emax[i-1][v], emax[i-1][par[i-1][v]]);
    }
  }
}

pair<int, int> lca(int u, int v) {
  int res = 0;
  if(depth[u] > depth[v]) swap(u, v);
  for(int i = LOGN-1; i >= 0; i--) {
    if(depth[v] - (1<<i) >= depth[u]) {
      res = max(res, emax[i][v]);
      v = par[i][v];
    }
  }
  if(u == v)
    return {u, res};
  for(int i = LOGN-1; i >= 0; i--) {
    if(par[i][u] != par[i][v]) {
      res = max(res, emax[i][u]);
      res = max(res, emax[i][v]);
      u = par[i][u];
      v = par[i][v];
    }
  }
  return {par[0][v], max(res, max(emax[0][v], emax[0][u]))};
}

void goup(int& v, int c) {
	val[v] = min(val[v], c - 1);
  int nv = find(parent[v]);
  if(sz[v] > sz[nv]) swap(v, nv);
  up[v] = nv;
  sz[nv] += sz[v];
  label[nv] = (depth[v] < depth[nv] ? v : nv);
  v = label[nv];
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    up[i] = i;
    sz[i] = 1;
    label[i] = i;
  }
  for(int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    es.push_back({u, v, c, i});
    //nbs[u].push_back(i);
    //nbs[v].push_back(i);
  }
  sortedes = es;
  sort(sortedes.begin(), sortedes.end(), cmp);

  for(edge e : sortedes) {
    if(find(e.u) != find(e.v)) {
      join(e.u, e.v);
      mst[e.u].push_back(e);
      mst[e.v].push_back(e);
      inmst[e.id] = 1;
    }
  }

  dfs(1, 1);
  build_lca();

  for(int i = 0; i < m; i++) {
  	//cerr << "inmst " << i << ": " << inmst[i] << endl;
  	ans[i] = inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
      auto r = lca(e.u, e.v);
      //cerr << e.id << " r: " << r.first << " " << r.second -1 << endl;
      ans[e.id] = r.second - 1;
    }
  }

  for(int i = 1; i <= n; i++) {
    up[i] = i;
  	//cerr << i << ": " << eparent[i] << endl;
    sz[i] = 1;
    val[i] =  inf;
  }

  for(edge e : sortedes) {
    if(!inmst[e.id]) {
    	e.u = find(e.u);
      e.v = find(e.v);
      //cerr << e.u << " " << e.v << " id: " << e.id << " c: " << e.c << endl;
      while(find(e.u) != find(e.v)) {
      	//cerr << e.u << " " << e.v << endl;
        if(!is_father(e.u, e.v)) goup(e.u, e.c);
        if(!is_father(e.v, e.u)) goup(e.v, e.c);
      }
      if(e.u == 1) val[e.u] = min(val[e.u], e.c - 1);
    }
  }
  for(int i = 1; i <= n; i++) {
  	//cerr << val[i] << " ";
  }
  //cerr << endl;

  for(edge e : es) {
    if(inmst[e.id]) {
      int v = (depth[e.u] > depth[e.v] ? e.u : e.v);
      //cerr << e.id << " :: " << v << " " << val[v] << endl;
      assert(ans[e.id] == inf);
      ans[e.id] = val[v];
      //cerr << e.id << ": " << ans[e.id] << endl;
    }
  }
  for(int i = 0 ; i < m; i++) {
    cout << (ans[i] != inf ? ans[i] : -1) << " ";
  }
  cout << endl;

  return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
const int N=200005,L=20;
int head[N],v[N*2],w[N*2],nxt[N*2],id[N*2],e,dep[N],f[N][L],mx[N][L];
vector<int> delta[N];
inline void add_edge(int u,int v,int w,int id)
{
	::v[++e]=v;
	::w[e]=w;
	::id[e]=id;
	nxt[e]=head[u];
	head[u]=e;
}
struct edge
{
	int u,v,w,id;
	bool operator<(const edge& rhs)const
	{
		return w<rhs.w;
	}
}E[N];
int p[N],ans[N];
int getf(int x)
{
	return p[x]==x?x:p[x]=getf(p[x]);
}
void dfs(int u,int fat)
{
	for(int i=head[u];i;i=nxt[i])
		if(v[i]!=fat)
		{
			dep[v[i]]=dep[u]+1;
			f[v[i]][0]=u;
			mx[v[i]][0]=w[i];
			dfs(v[i],u);
		}
}
pair<int,int> lca(int u,int v)
{
	if(dep[u]<dep[v])
		swap(u,v);
	int delta=dep[u]-dep[v],val=0;
	for(int i=0;delta;i++,delta/=2)
		if(delta&1)
		{
			val=max(val,mx[u][i]);
			u=f[u][i];
		}
	if(u==v)
		return make_pair(u,val);
	for(int i=L-1;i>=0;i--)
		if(f[u][i]!=f[v][i])
		{
			val=max(val,mx[u][i]);
			u=f[u][i];
			val=max(val,mx[v][i]);
			v=f[v][i];
		}
	val=max(val,mx[u][0]);
	val=max(val,mx[v][0]);
	return make_pair(f[u][0],val);
}
bool tree[N];
multiset<int> dfs2(int u,int fat)
{
	multiset<int> S;
	for(int i=head[u];i;i=nxt[i])
		if(v[i]!=fat)
		{
			multiset<int> now=dfs2(v[i],u);
			ans[id[i]]=*now.begin()-1;
			if(now.size()>S.size())
				S.swap(now);
			for(int x:now)
				S.insert(x);
		}
	for(int x:delta[u])
		if(x>0)
			S.insert(x);
		else
			S.erase(S.find(-x));
	return S;
}
int main()
{
	ios::sync_with_stdio(false);
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>E[i].u>>E[i].v>>E[i].w;
		E[i].id=i;
	}
	sort(E+1,E+m+1);
	for(int i=1;i<=n;i++)
		p[i]=i;
	int cnt=0;
	for(int i=1;i<=m;i++)
	{
		int ru=getf(E[i].u),rv=getf(E[i].v);
		if(ru!=rv)
		{
			add_edge(E[i].u,E[i].v,E[i].w,E[i].id);
			add_edge(E[i].v,E[i].u,E[i].w,E[i].id);
			p[ru]=rv;
			tree[i]=true;
			if(++cnt==n-1)
				break;
		}
	}
	dfs(1,0);
	for(int j=1;j<L;j++)
		for(int i=1;i<=n;i++)
		{
			f[i][j]=f[f[i][j-1]][j-1];
			mx[i][j]=max(mx[i][j-1],mx[f[i][j-1]][j-1]);
		}
	fill(ans+1,ans+m+1,-1);
	for(int i=1;i<=m;i++)
		if(!tree[i])
		{
			pair<int,int> a=lca(E[i].u,E[i].v);
			ans[E[i].id]=a.second-1;
			if(a.first==E[i].u)
			{
				delta[E[i].v].push_back(E[i].w);
				delta[E[i].u].push_back(-E[i].w);
			}
			else if(a.first==E[i].v)
			{
				delta[E[i].u].push_back(E[i].w);
				delta[E[i].v].push_back(-E[i].w);
			}
			else
			{
				delta[E[i].u].push_back(E[i].w);
				delta[E[i].v].push_back(E[i].w);
				delta[a.first].push_back(-E[i].w);
			}
		}
	dfs2(1,0);
	for(int i=1;i<=m;i++)
		cout<<ans[i]<<' ';
	cout<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
const int N=200005,L=20;
int head[N],v[N*2],w[N*2],nxt[N*2],id[N*2],e,dep[N],f[N][L],mx[N][L];
vector<int> delta[N];
inline void add_edge(int u,int v,int w,int id)
{
	::v[++e]=v;
	::w[e]=w;
	::id[e]=id;
	nxt[e]=head[u];
	head[u]=e;
}
struct edge
{
	int u,v,w,id;
	bool operator<(const edge& rhs)const
	{
		return w<rhs.w;
	}
}E[N];
int p[N],ans[N];
int getf(int x)
{
	return p[x]==x?x:p[x]=getf(p[x]);
}
void dfs(int u,int fat)
{
	for(int i=head[u];i;i=nxt[i])
		if(v[i]!=fat)
		{
			dep[v[i]]=dep[u]+1;
			f[v[i]][0]=u;
			mx[v[i]][0]=w[i];
			dfs(v[i],u);
		}
}
pair<int,int> lca(int u,int v)
{
	if(dep[u]<dep[v])
		swap(u,v);
	int delta=dep[u]-dep[v],val=0;
	for(int i=0;delta;i++,delta/=2)
		if(delta&1)
		{
			val=max(val,mx[u][i]);
			u=f[u][i];
		}
	if(u==v)
		return make_pair(u,val);
	for(int i=L-1;i>=0;i--)
		if(f[u][i]!=f[v][i])
		{
			val=max(val,mx[u][i]);
			u=f[u][i];
			val=max(val,mx[v][i]);
			v=f[v][i];
		}
	val=max(val,mx[u][0]);
	val=max(val,mx[v][0]);
	return make_pair(f[u][0],val);
}
bool tree[N];
multiset<int> dfs2(int u,int fat)
{
	multiset<int> S;
	for(int i=head[u];i;i=nxt[i])
		if(v[i]!=fat)
		{
			multiset<int> now=dfs2(v[i],u);
			if(now.size())
				ans[id[i]]=*now.begin()-1;
			if(now.size()>S.size())
				S.swap(now);
			for(int x:now)
				S.insert(x);
		}
	for(int x:delta[u])
		if(x>0)
			S.insert(x);
		else
			S.erase(S.find(-x));
	return S;
}
int main()
{
	ios::sync_with_stdio(false);
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>E[i].u>>E[i].v>>E[i].w;
		E[i].id=i;
	}
	sort(E+1,E+m+1);
	for(int i=1;i<=n;i++)
		p[i]=i;
	int cnt=0;
	for(int i=1;i<=m;i++)
	{
		int ru=getf(E[i].u),rv=getf(E[i].v);
		if(ru!=rv)
		{
			add_edge(E[i].u,E[i].v,E[i].w,E[i].id);
			add_edge(E[i].v,E[i].u,E[i].w,E[i].id);
			p[ru]=rv;
			tree[i]=true;
			if(++cnt==n-1)
				break;
		}
	}
	dfs(1,0);
	for(int j=1;j<L;j++)
		for(int i=1;i<=n;i++)
		{
			f[i][j]=f[f[i][j-1]][j-1];
			mx[i][j]=max(mx[i][j-1],mx[f[i][j-1]][j-1]);
		}
	fill(ans+1,ans+m+1,-1);
	for(int i=1;i<=m;i++)
		if(!tree[i])
		{
			pair<int,int> a=lca(E[i].u,E[i].v);
			ans[E[i].id]=a.second-1;
			if(a.first==E[i].u)
			{
				delta[E[i].v].push_back(E[i].w);
				delta[E[i].u].push_back(-E[i].w);
			}
			else if(a.first==E[i].v)
			{
				delta[E[i].u].push_back(E[i].w);
				delta[E[i].v].push_back(-E[i].w);
			}
			else
			{
				delta[E[i].u].push_back(E[i].w);
				delta[E[i].v].push_back(E[i].w);
				delta[a.first].push_back(-E[i].w);
				delta[a.first].push_back(-E[i].w);
			}
		}
	dfs2(1,0);
	for(int i=1;i<=m;i++)
		cout<<ans[i]<<' ';
	cout<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
const int N=200005,L=20;
int head[N],v[N*2],w[N*2],nxt[N*2],id[N*2],e,dep[N],f[N][L],mx[N][L];
vector<int> delta[N];
inline void add_edge(int u,int v,int w,int id)
{
	::v[++e]=v;
	::w[e]=w;
	::id[e]=id;
	nxt[e]=head[u];
	head[u]=e;
}
struct edge
{
	int u,v,w,id;
	bool operator<(const edge& rhs)const
	{
		return w<rhs.w;
	}
}E[N];
int p[N],ans[N];
int getf(int x)
{
	return p[x]==x?x:p[x]=getf(p[x]);
}
void dfs(int u,int fat)
{
	for(int i=head[u];i;i=nxt[i])
		if(v[i]!=fat)
		{
			dep[v[i]]=dep[u]+1;
			f[v[i]][0]=u;
			mx[v[i]][0]=w[i];
			dfs(v[i],u);
		}
}
pair<int,int> lca(int u,int v)
{
	if(dep[u]<dep[v])
		swap(u,v);
	int delta=dep[u]-dep[v],val=0;
	for(int i=0;delta;i++,delta/=2)
		if(delta&1)
		{
			val=max(val,mx[u][i]);
			u=f[u][i];
		}
	if(u==v)
		return make_pair(u,val);
	for(int i=L-1;i>=0;i--)
		if(f[u][i]!=f[v][i])
		{
			val=max(val,mx[u][i]);
			u=f[u][i];
			val=max(val,mx[v][i]);
			v=f[v][i];
		}
	val=max(val,mx[u][0]);
	val=max(val,mx[v][0]);
	return make_pair(f[u][0],val);
}
bool tree[N];
multiset<int> dfs2(int u,int fat)
{
	multiset<int> S;
	for(int i=head[u];i;i=nxt[i])
		if(v[i]!=fat)
		{
			multiset<int> now=dfs2(v[i],u);
			ans[id[i]]=*now.begin()-1;
			if(now.size()>S.size())
				S.swap(now);
			for(int x:now)
				S.insert(x);
		}
	for(int x:delta[u])
		if(x>0)
			S.insert(x);
		else
			S.erase(S.find(-x));
	return S;
}
int main()
{
	ios::sync_with_stdio(false);
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>E[i].u>>E[i].v>>E[i].w;
		E[i].id=i;
	}
	sort(E+1,E+m+1);
	for(int i=1;i<=n;i++)
		p[i]=i;
	int cnt=0;
	for(int i=1;i<=m;i++)
	{
		int ru=getf(E[i].u),rv=getf(E[i].v);
		if(ru!=rv)
		{
			add_edge(E[i].u,E[i].v,E[i].w,i);
			add_edge(E[i].v,E[i].u,E[i].w,i);
			p[ru]=rv;
			tree[i]=true;
			if(++cnt==n-1)
				break;
		}
	}
	dfs(1,0);
	for(int j=1;j<L;j++)
		for(int i=1;i<=n;i++)
		{
			f[i][j]=f[f[i][j-1]][j-1];
			mx[i][j]=max(mx[i][j-1],mx[f[i][j-1]][j-1]);
		}
	fill(ans+1,ans+m+1,-1);
	for(int i=1;i<=m;i++)
		if(!tree[i])
		{
			pair<int,int> a=lca(E[i].u,E[i].v);
			ans[E[i].id]=a.second-1;
			if(a.first==E[i].u)
			{
				delta[E[i].v].push_back(E[i].w);
				delta[E[i].u].push_back(-E[i].w);
			}
			else if(a.first==E[i].v)
			{
				delta[E[i].u].push_back(E[i].w);
				delta[E[i].v].push_back(-E[i].w);
			}
			else
			{
				delta[E[i].u].push_back(E[i].w);
				delta[E[i].v].push_back(E[i].w);
				delta[a.first].push_back(-E[i].w);
			}
		}
	dfs2(1,0);
	for(int i=1;i<=m;i++)
		cout<<ans[i]<<' ';
	cout<<endl;
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long LL;
typedef unsigned int u32;
#define pb push_back
const int maxn = 200005;

int n,m,par[maxn][20],mx[maxn][20],dep[maxn],ot[maxn];
vector<int> adj[maxn];bool mst[maxn];
struct edge {
	int u,v,w,id;
}edg[maxn];
bool comp(edge a,edge b) {return a.w<b.w;}
void add(int u,int v) {
	adj[u].pb(v);adj[v].pb(u);
}
int p[maxn];
int findp(int u) {
	return p[u]?p[u]=findp(p[u]):u;
}

void dfs1(int u)
{
	int v;
	for (u32 j=0;j<adj[u].size();j++)
	if ((v=adj[u][j])!=par[u][0])
		par[v][0]=u,dep[v]=dep[u]+1,dfs1(v);
}
void sparse_table()
{
	for (int j=1;j<20;j++)
	for (int i=1;i<=n;i++)
		par[i][j]=par[par[i][j-1]][j-1],
		mx[i][j]=max(mx[i][j-1],mx[par[i][j-1]][j-1]);
}
int lca(int u,int v)
{
	if (dep[u]<dep[v]) swap(u,v);
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) u=par[u][j];
	if (u==v) return u;
	for (int j=19;j>=0;j--)
	if (par[u][j]!=par[v][j])
		u=par[u][j],v=par[v][j];
	return par[u][0];
}
int query(int u,int v)
{
	int res=0;
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) res=max(res,mx[u][j]),u=par[u][j];
	return res;
}
void merge(int u,int v) {
	u=findp(u);v=findp(v);
	if (dep[u]>dep[v]) swap(u,v);
	if (u!=v) p[v]=u;
}
void work(int u,int v,int w)
{
	for (int x=ot[u]?par[findp(u)][0]:u;dep[x]>dep[v];) {
		ot[x]=w;merge(u,x);x=par[x][0];
		if (ot[x]) x=par[findp(x)][0];
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("weight.in","r",stdin);
		//freopen("weight.out","w",stdout);
	#endif
	scanf("%d %d",&n,&m);
	for (int u,v,c,i=1;i<=m;i++)
		scanf("%d %d %d",&u,&v,&c),edg[i]=(edge){u,v,c,i};
	sort(edg+1,edg+m+1,comp);
	for (int i=1;i<=m;i++)
	{
		int u=edg[i].u,v=edg[i].v;
		if (findp(u)!=findp(v)) {
			p[findp(u)]=findp(v);
			mst[edg[i].id]=true;
			add(u,v);
		}
	}

	dfs1(1);memset(p,0,n+1<<2);
	for (int i=1;i<=m;i++)
	if (mst[i]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		mx[u][0]=edg[i].w;
	}
	sparse_table();

	for (int i=m;i>=1;i--)
	if (!mst[i]) {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		work(u,t,edg[i].w);
		work(v,t,edg[i].w);
	}

	for (int i=1;i<=m;i++)
	if (mst[i]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		if (!ot[u]) printf("-1 ");
		else printf("%d ",ot[u]-1);
	}
	else {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		int res=max(query(u,t),query(v,t));
		printf("%d ",res-1);
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long LL;
typedef unsigned int u32;
#define pb push_back
const int maxn = 400005;

int n,m,par[maxn][20],mx[maxn][20],dep[maxn],ot[maxn];
vector<int> adj[maxn];bool mst[maxn];
struct edge {
	int u,v,w,id;
}edg[maxn];
bool comp(edge a,edge b) {return a.w<b.w;}
bool compid(edge a,edge b) {return a.id<b.id;}
void add(int u,int v) {
	adj[u].pb(v);adj[v].pb(u);
}
int p[maxn];
int findp(int u) {
	return p[u]?p[u]=findp(p[u]):u;
}

void dfs1(int u)
{
	int v;
	for (u32 j=0;j<adj[u].size();j++)
	if ((v=adj[u][j])!=par[u][0])
		par[v][0]=u,dep[v]=dep[u]+1,dfs1(v);
}
void sparse_table()
{
	for (int j=1;j<20;j++)
	for (int i=1;i<=n;i++)
		par[i][j]=par[par[i][j-1]][j-1],
		mx[i][j]=max(mx[i][j-1],mx[par[i][j-1]][j-1]);
}
int lca(int u,int v)
{
	if (dep[u]<dep[v]) swap(u,v);
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) u=par[u][j];
	if (u==v) return u;
	for (int j=19;j>=0;j--)
	if (par[u][j]!=par[v][j])
		u=par[u][j],v=par[v][j];
	return par[u][0];
}
int query(int u,int v)
{
	int res=0;
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) res=max(res,mx[u][j]),u=par[u][j];
	return res;
}
void merge(int u,int v) {
	u=findp(u);v=findp(v);
	if (dep[u]>dep[v]) swap(u,v);
	if (u!=v) p[v]=u;
}
void work(int u,int v,int w)
{
	for (int x=ot[u]?par[findp(u)][0]:u;dep[x]>dep[v];) {
		if (!ot[x]) ot[x]=w;merge(x,u);x=par[x][0];
		if (ot[x]) {merge(x,u);x=par[findp(x)][0];}
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("weight.in","r",stdin);
		//freopen("weight.out","w",stdout);
	#endif
	scanf("%d %d",&n,&m);
	for (int u,v,c,i=1;i<=m;i++)
		scanf("%d %d %d",&u,&v,&c),edg[i]=(edge){u,v,c,i};
	sort(edg+1,edg+m+1,comp);
	for (int i=1;i<=m;i++)
	{
		int u=edg[i].u,v=edg[i].v;
		if (findp(u)!=findp(v)) {
			p[findp(u)]=findp(v);
			mst[edg[i].id]=true;
			add(u,v);
		}
	}
	dfs1(1);memset(p,0,n+1<<2);
	for (int i=1;i<=m;i++)
	if (mst[edg[i].id]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		mx[u][0]=edg[i].w;
	}
	sparse_table();

	for (int i=1;i<=m;i++)
	if (!mst[edg[i].id]) {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		work(u,t,edg[i].w);
		work(v,t,edg[i].w);
	}

	sort(edg+1,edg+m+1,compid);
	for (int i=1;i<=m;i++)
	if (mst[i]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		if (!ot[u]) printf("-1 ");
		else printf("%d ",ot[u]-1);
	}
	else {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		int res=max(query(u,t),query(v,t));
		printf("%d ",res-1);
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long LL;
typedef unsigned int u32;
#define pb push_back
const int maxn = 200005;

int n,m,par[maxn][20],mx[maxn][20],dep[maxn],ot[maxn];
vector<int> adj[maxn];bool mst[maxn];
struct edge {
	int u,v,w,id;
}edg[maxn];
bool comp(edge a,edge b) {return a.w<b.w;}
bool compid(edge a,edge b) {return a.id<b.id;}
void add(int u,int v) {
	adj[u].pb(v);adj[v].pb(u);
}
int p[maxn];
int findp(int u) {
	return p[u]?p[u]=findp(p[u]):u;
}

void dfs1(int u)
{
	int v;
	for (u32 j=0;j<adj[u].size();j++)
	if ((v=adj[u][j])!=par[u][0])
		par[v][0]=u,dep[v]=dep[u]+1,dfs1(v);
}
void sparse_table()
{
	for (int j=1;j<20;j++)
	for (int i=1;i<=n;i++)
		par[i][j]=par[par[i][j-1]][j-1],
		mx[i][j]=max(mx[i][j-1],mx[par[i][j-1]][j-1]);
}
int lca(int u,int v)
{
	if (dep[u]<dep[v]) swap(u,v);
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) u=par[u][j];
	if (u==v) return u;
	for (int j=19;j>=0;j--)
	if (par[u][j]!=par[v][j])
		u=par[u][j],v=par[v][j];
	return par[u][0];
}
int query(int u,int v)
{
	int res=0;
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) res=max(res,mx[u][j]),u=par[u][j];
	return res;
}
void merge(int u,int v) {
	u=findp(u);v=findp(v);
	if (dep[u]>dep[v]) swap(u,v);
	if (u!=v) p[v]=u;
}
void work(int u,int v,int w)
{
	for (int x=ot[u]?par[findp(u)][0]:u;dep[x]>dep[v];) {
		ot[x]=w;merge(u,x);x=par[x][0];
		if (ot[x]) x=par[findp(x)][0];
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("weight.in","r",stdin);
		//freopen("weight.out","w",stdout);
	#endif
	scanf("%d %d",&n,&m);
	for (int u,v,c,i=1;i<=m;i++)
		scanf("%d %d %d",&u,&v,&c),edg[i]=(edge){u,v,c,i};
	sort(edg+1,edg+m+1,comp);
	for (int i=1;i<=m;i++)
	{
		int u=edg[i].u,v=edg[i].v;
		if (findp(u)!=findp(v)) {
			p[findp(u)]=findp(v);
			mst[edg[i].id]=true;
			add(u,v);
		}
	}

	dfs1(1);memset(p,0,n+1<<2);
	for (int i=1;i<=m;i++)
	if (mst[edg[i].id]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		mx[u][0]=edg[i].w;
	}
	sparse_table();

	for (int i=m;i>=1;i--)
	if (!mst[edg[i].id]) {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		work(u,t,edg[i].w);
		work(v,t,edg[i].w);
	}

	sort(edg+1,edg+m+1,compid);
	for (int i=1;i<=m;i++)
	if (mst[i]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		if (!ot[u]) printf("-1 ");
		else printf("%d ",ot[u]-1);
	}
	else {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		int res=max(query(u,t),query(v,t));
		printf("%d ",res-1);
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long LL;
typedef unsigned int u32;
#define pb push_back
const int maxn = 200005;

int n,m,par[maxn][20],mx[maxn][20],dep[maxn],ot[maxn];
vector<int> adj[maxn];bool mst[maxn];
struct edge {
	int u,v,w,id;
}edg[maxn];
bool comp(edge a,edge b) {return a.w<b.w;}
bool compid(edge a,edge b) {return a.id<b.id;}
void add(int u,int v) {
	adj[u].pb(v);adj[v].pb(u);
}
int p[maxn];
int findp(int u) {
	return p[u]?p[u]=findp(p[u]):u;
}

void dfs1(int u)
{
	int v;
	for (u32 j=0;j<adj[u].size();j++)
	if ((v=adj[u][j])!=par[u][0])
		par[v][0]=u,dep[v]=dep[u]+1,dfs1(v);
}
void sparse_table()
{
	for (int j=1;j<20;j++)
	for (int i=1;i<=n;i++)
		par[i][j]=par[par[i][j-1]][j-1],
		mx[i][j]=max(mx[i][j-1],mx[par[i][j-1]][j-1]);
}
int lca(int u,int v)
{
	if (dep[u]<dep[v]) swap(u,v);
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) u=par[u][j];
	if (u==v) return u;
	for (int j=19;j>=0;j--)
	if (par[u][j]!=par[v][j])
		u=par[u][j],v=par[v][j];
	return par[u][0];
}
int query(int u,int v)
{
	int res=0;
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) res=max(res,mx[u][j]),u=par[u][j];
	return res;
}
void merge(int u,int v) {
	u=findp(u);v=findp(v);
	if (dep[u]>dep[v]) swap(u,v);
	if (u!=v) p[v]=u;
}
void work(int u,int v,int w)
{
	for (int x=ot[u]?par[findp(u)][0]:u;dep[x]>dep[v];) {
		ot[x]=w;merge(u,x);x=par[x][0];
		if (ot[x]) x=par[findp(x)][0];
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("weight.in","r",stdin);
		//freopen("weight.out","w",stdout);
	#endif
	scanf("%d %d",&n,&m);
	for (int u,v,c,i=1;i<=m;i++)
		scanf("%d %d %d",&u,&v,&c),edg[i]=(edge){u,v,c,i};
	sort(edg+1,edg+m+1,comp);
	for (int i=1;i<=m;i++)
	{
		int u=edg[i].u,v=edg[i].v;
		if (findp(u)!=findp(v)) {
			p[findp(u)]=findp(v);
			mst[edg[i].id]=true;
			add(u,v);
		}
	}

	dfs1(1);memset(p,0,n+1<<2);
	for (int i=1;i<=m;i++)
	if (mst[edg[i].id]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		mx[u][0]=edg[i].w;
	}
	sparse_table();

	for (int i=1;i<=m;i++)
	if (!mst[edg[i].id]) {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		work(u,t,edg[i].w);
		work(v,t,edg[i].w);
	}

	sort(edg+1,edg+m+1,compid);
	for (int i=1;i<=m;i++)
	if (mst[i]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		if (!ot[u]) printf("-1 ");
		else printf("%d ",ot[u]-1);
	}
	else {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		int res=max(query(u,t),query(v,t));
		printf("%d ",res-1);
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long LL;
typedef unsigned int u32;
#define pb push_back
const int maxn = 200005;

int n,m,par[maxn][20],mx[maxn][20],dep[maxn],ot[maxn];
vector<int> adj[maxn];bool mst[maxn];
struct edge {
	int u,v,w,id;
}edg[maxn];
bool comp(edge a,edge b) {return a.w<b.w;}
bool compid(edge a,edge b) {return a.id<b.id;}
void add(int u,int v) {
	adj[u].pb(v);adj[v].pb(u);
}
int p[maxn];
int findp(int u) {
	return p[u]?p[u]=findp(p[u]):u;
}

void dfs1(int u)
{
	int v;
	for (u32 j=0;j<adj[u].size();j++)
	if ((v=adj[u][j])!=par[u][0])
		par[v][0]=u,dep[v]=dep[u]+1,dfs1(v);
}
void sparse_table()
{
	for (int j=1;j<20;j++)
	for (int i=1;i<=n;i++)
		par[i][j]=par[par[i][j-1]][j-1],
		mx[i][j]=max(mx[i][j-1],mx[par[i][j-1]][j-1]);
}
int lca(int u,int v)
{
	if (dep[u]<dep[v]) swap(u,v);
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) u=par[u][j];
	if (u==v) return u;
	for (int j=19;j>=0;j--)
	if (par[u][j]!=par[v][j])
		u=par[u][j],v=par[v][j];
	return par[u][0];
}
int query(int u,int v)
{
	int res=0;
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) res=max(res,mx[u][j]),u=par[u][j];
	return res;
}
void merge(int u,int v) {
	u=findp(u);v=findp(v);
	if (dep[u]>dep[v]) swap(u,v);
	if (u!=v) p[v]=u;
}
void work(int u,int v,int w)
{
	for (int x=ot[u]?par[findp(u)][0]:u;dep[x]>dep[v];) {
		ot[x]=w;merge(u,x);x=par[x][0];
		if (ot[x]) x=par[findp(x)][0];
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("weight.in","r",stdin);
		//freopen("weight.out","w",stdout);
	#endif
	scanf("%d %d",&n,&m);
	for (int u,v,c,i=1;i<=m;i++)
		scanf("%d %d %d",&u,&v,&c),edg[i]=(edge){u,v,c,i};
	sort(edg+1,edg+m+1,comp);
	for (int i=1;i<=m;i++)
	{
		int u=edg[i].u,v=edg[i].v;
		if (findp(u)!=findp(v)) {
			p[findp(u)]=findp(v);
			mst[edg[i].id]=true;
			add(u,v);
		}
	}

	dfs1(1);memset(p,0,n+1<<2);
	for (int i=1;i<=m;i++)
	if (mst[i]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		mx[u][0]=edg[i].w;
	}
	sparse_table();

	for (int i=m;i>=1;i--)
	if (!mst[i]) {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		work(u,t,edg[i].w);
		work(v,t,edg[i].w);
	}

	sort(edg+1,edg+m+1,compid);
	for (int i=1;i<=m;i++)
	if (mst[i]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		if (!ot[u]) printf("-1 ");
		else printf("%d ",ot[u]-1);
	}
	else {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		int res=max(query(u,t),query(v,t));
		printf("%d ",res-1);
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long LL;
typedef unsigned int u32;
#define pb push_back
const int maxn = 400005;

int n,m,par[maxn][20],mx[maxn][20],dep[maxn],ot[maxn];
vector<int> adj[maxn];bool mst[maxn];
struct edge {
	int u,v,w,id;
}edg[maxn];
bool comp(edge a,edge b) {return a.w<b.w;}
bool compid(edge a,edge b) {return a.id<b.id;}
void add(int u,int v) {
	adj[u].pb(v);adj[v].pb(u);
}
int p[maxn];
int findp(int u) {
	return p[u]?p[u]=findp(p[u]):u;
}

void dfs1(int u)
{
	int v;
	for (u32 j=0;j<adj[u].size();j++)
	if ((v=adj[u][j])!=par[u][0])
		par[v][0]=u,dep[v]=dep[u]+1,dfs1(v);
}
void sparse_table()
{
	for (int j=1;j<20;j++)
	for (int i=1;i<=n;i++)
		par[i][j]=par[par[i][j-1]][j-1],
		mx[i][j]=max(mx[i][j-1],mx[par[i][j-1]][j-1]);
}
int lca(int u,int v)
{
	if (dep[u]<dep[v]) swap(u,v);
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) u=par[u][j];
	if (u==v) return u;
	for (int j=19;j>=0;j--)
	if (par[u][j]!=par[v][j])
		u=par[u][j],v=par[v][j];
	return par[u][0];
}
int query(int u,int v)
{
	int res=0;
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) res=max(res,mx[u][j]),u=par[u][j];
	return res;
}
void merge(int u,int v) {
	u=findp(u);v=findp(v);
	if (dep[u]>dep[v]) swap(u,v);
	if (u!=v) p[v]=u;
}
void work(int u,int v,int w)
{
	for (int x=ot[u]?par[findp(u)][0]:u;dep[x]>dep[v];) {
		ot[x]=w;merge(x,u);x=par[x][0];
		if (ot[x]) {merge(x,u);x=par[findp(x)][0];}
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("weight.in","r",stdin);
		//freopen("weight.out","w",stdout);
	#endif
	scanf("%d %d",&n,&m);
	for (int u,v,c,i=1;i<=m;i++)
		scanf("%d %d %d",&u,&v,&c),edg[i]=(edge){u,v,c,i};
	sort(edg+1,edg+m+1,comp);
	for (int i=1;i<=m;i++)
	{
		int u=edg[i].u,v=edg[i].v;
		if (findp(u)!=findp(v)) {
			p[findp(u)]=findp(v);
			mst[edg[i].id]=true;
			add(u,v);
		}
	}
	dfs1(1);memset(p,0,n+1<<2);
	for (int i=1;i<=m;i++)
	if (mst[edg[i].id]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		mx[u][0]=edg[i].w;
	}
	sparse_table();

	for (int i=1;i<=m;i++)
	if (!mst[edg[i].id]) {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		work(u,t,edg[i].w);
		work(v,t,edg[i].w);
	}

	sort(edg+1,edg+m+1,compid);
	for (int i=1;i<=m;i++)
	if (mst[i]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		if (!ot[u]) printf("-1 ");
		else printf("%d ",ot[u]-1);
	}
	else {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		int res=max(query(u,t),query(v,t));
		printf("%d ",res-1);
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long LL;
typedef unsigned int u32;
#define pb push_back
const int maxn = 400005;

int n,m,par[maxn][20],mx[maxn][20],dep[maxn],ot[maxn];
vector<int> adj[maxn];bool mst[maxn];
struct edge {
	int u,v,w,id;
}edg[maxn];
bool comp(edge a,edge b) {return a.w<b.w;}
bool compid(edge a,edge b) {return a.id<b.id;}
void add(int u,int v) {
	adj[u].pb(v);adj[v].pb(u);
}
int p[maxn];
int findp(int u) {
	return p[u]?p[u]=findp(p[u]):u;
}

void dfs1(int u)
{
	int v;
	for (u32 j=0;j<adj[u].size();j++)
	if ((v=adj[u][j])!=par[u][0])
		par[v][0]=u,dep[v]=dep[u]+1,dfs1(v);
}
void sparse_table()
{
	for (int j=1;j<20;j++)
	for (int i=1;i<=n;i++)
		par[i][j]=par[par[i][j-1]][j-1],
		mx[i][j]=max(mx[i][j-1],mx[par[i][j-1]][j-1]);
}
int lca(int u,int v)
{
	if (dep[u]<dep[v]) swap(u,v);
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) u=par[u][j];
	if (u==v) return u;
	for (int j=19;j>=0;j--)
	if (par[u][j]!=par[v][j])
		u=par[u][j],v=par[v][j];
	return par[u][0];
}
int query(int u,int v)
{
	int res=0;
	for (int d=dep[u]-dep[v],j=0;d;d>>=1,++j)
		if (d&1) res=max(res,mx[u][j]),u=par[u][j];
	return res;
}
void merge(int u,int v) {
	u=findp(u);v=findp(v);
	if (dep[u]>dep[v]) swap(u,v);
	if (u!=v) p[v]=u;
}
void work(int u,int v,int w)
{
	for (int x=ot[u]?par[findp(u)][0]:u;dep[x]>dep[v];) {
		ot[x]=w;x=par[x][0];merge(x,u);
		if (ot[x]) x=par[findp(x)][0];
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("weight.in","r",stdin);
		//freopen("weight.out","w",stdout);
	#endif
	scanf("%d %d",&n,&m);
	for (int u,v,c,i=1;i<=m;i++)
		scanf("%d %d %d",&u,&v,&c),edg[i]=(edge){u,v,c,i};
	sort(edg+1,edg+m+1,comp);
	for (int i=1;i<=m;i++)
	{
		int u=edg[i].u,v=edg[i].v;
		if (findp(u)!=findp(v)) {
			p[findp(u)]=findp(v);
			mst[edg[i].id]=true;
			add(u,v);
		}
	}
	dfs1(1);memset(p,0,n+1<<2);
	for (int i=1;i<=m;i++)
	if (mst[edg[i].id]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		mx[u][0]=edg[i].w;
	}
	sparse_table();

	for (int i=1;i<=m;i++)
	if (!mst[edg[i].id]) {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		work(u,t,edg[i].w);
		work(v,t,edg[i].w);
	}

	sort(edg+1,edg+m+1,compid);
	for (int i=1;i<=m;i++)
	if (mst[i]) {
		int u=edg[i].u,v=edg[i].v;
		if (dep[u]<dep[v]) swap(u,v);
		if (!ot[u]) printf("-1 ");
		else printf("%d ",ot[u]-1);
	}
	else {
		int u=edg[i].u,v=edg[i].v;
		int t=lca(u,v);
		int res=max(query(u,t),query(v,t));
		printf("%d ",res-1);
	}
	return 0;
}


/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 200005
#define maxm 200005
#define pii pair <int,int>
#define Task "827D"
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,m,ans[maxn],root[maxn],h[maxn];
bool dd[maxm];
pii par[maxn][18];
multiset <int> val;
multiset <pii> s;
struct edge
{
    int u,v,w,id;
    bool operator < (edge &x) const
    {
        return w < x.w;
    }
}ed[maxm];
vector <pair <pii,int> > a[maxn];
vector <pii> q[maxn];
int Get_root(int u)
{
    return root[u] ? root[u] = Get_root(root[u]) : u;
}
void Dfs(int u)
{
    for (auto ww : a[u])
    {
        int v = ww.F.F,
            w = ww.F.S,
            id = ww.S;
        if (v == par[u][0].F) continue;
        h[v] = h[u] + 1;
        par[v][0] = mp(u,w);
        FOR(i,1,17)
        {
            par[v][i].F = par[par[v][i-1].F][i-1].F;
            par[v][i].S = max(par[v][i-1].S,par[par[v][i-1].F][i-1].S);
        }
        Dfs(v);
    }
}
pii Lca(int u,int v)
{
    int res = 0;
    if (h[u] < h[v]) swap(u,v);
    int del = h[u] - h[v];
    FOR(i,0,17)
        if ((del >> i) & 1)
        {
            res = max(res,par[u][i].S);
            u = par[u][i].F;
        }
    if (u == v) return mp(u,res);
    FORD(i,17,0)
        if (par[u][i].F != par[v][i].F)
        {
            res = max(res,par[u][i].S);
            res = max(res,par[v][i].S);
            u = par[u][i].F;
            v = par[v][i].S;
        }
    return mp(par[u][0].F,res);
}
void Dfs_Get_ans(int u,int id)
{
    for (auto ww : a[u])
    {
        int v = ww.F.F,
            idd = ww.S;
        if (v == par[u][0].F) continue;
        Dfs_Get_ans(v,idd);
    }
    for (auto v : q[u])
    {
        s.insert(v);
        val.insert(v.S);
    }
    while (s.size())
    {
        auto it = s.end();
        --it;
        pii cur = *it;
        if (cur.F == h[u])
        {
            auto it1 = val.lower_bound(cur.S);
            val.erase(it1);
            s.erase(it);
        }
        else break;
    }
    if (val.size())
    {
        auto it = val.end();
        --it;
        ans[id] = (*it) - 1;
    }
    else ans[id] = -1;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen(Task".inp", "r",stdin);
    //freopen(Task".out", "w",stdout);
    cin >> n >> m;
    FOR(i,1,m)
    {
        int u,v,w;
        cin >> u >> v >> w;
        ed[i] = {u,v,w,i};
    }
    sort(ed + 1,ed + m + 1);
    FOR(i,1,m)
    {
        int u = ed[i].u,
            v = ed[i].v,
            w = ed[i].w,
            id = ed[i].id;
        u = Get_root(u),
        v = Get_root(v);
        if (u == v) continue;
        root[u] = v;
        dd[id] = 1;
        a[u].pb(mp(mp(v,w),id));
        a[v].pb(mp(mp(u,w),id));
    }
    Dfs(1);
    FOR(i,1,m)
    {
        int u = ed[i].u,
            v = ed[i].v,
            w = ed[i].w,
            id = ed[i].id;
        if (dd[id]) continue;
        pii ww = Lca(u,v);
        ans[id] = ww.S - 1;
        q[u].pb(mp(h[ww.F],w));
        q[v].pb(mp(h[ww.F],w));
    }
    Dfs_Get_ans(1,0);
    FOR(i,1,m) cout << ans[i] << ' ';
    return 0;
}


/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 200005
#define maxm 200005
#define pii pair <int,int>
#define Task "827D"
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,m,ans[maxn],root[maxn],h[maxn];
bool dd[maxm];
pii par[maxn][18];
multiset <int> val;
multiset <pii> s;
struct edge
{
    int u,v,w,id;
    bool operator < (edge &x) const
    {
        return w < x.w;
    }
}ed[maxm];
vector <pair <pii,int> > a[maxn];
vector <pii> q[maxn];
int Get_root(int u)
{
    return root[u] ? root[u] = Get_root(root[u]) : u;
}
void Dfs(int u)
{
    for (auto ww : a[u])
    {
        int v = ww.F.F,
            w = ww.F.S,
            id = ww.S;
        if (v == par[u][0].F) continue;
        h[v] = h[u] + 1;
        par[v][0] = mp(u,w);
        FOR(i,1,17)
        {
            par[v][i].F = par[par[v][i-1].F][i-1].F;
            par[v][i].S = max(par[v][i-1].S,par[par[v][i-1].F][i-1].S);
        }
        Dfs(v);
    }
}
pii Lca(int u,int v)
{
    int res = 0;
    if (h[u] < h[v]) swap(u,v);
    int del = h[u] - h[v];
    FOR(i,0,17)
        if ((del >> i) & 1)
        {
            res = max(res,par[u][i].S);
            u = par[u][i].F;
        }
    if (u == v) return mp(u,res);
    FORD(i,17,0)
        if (par[u][i].F != par[v][i].F)
        {
            res = max(res,par[u][i].S);
            res = max(res,par[v][i].S);
            u = par[u][i].F;
            v = par[v][i].S;
        }
    res = max(res,par[u][0].S);
    res = max(res,par[v][0].S);
    return mp(par[u][0].F,res);
}
void Dfs_Get_ans(int u,int id)
{
    for (auto ww : a[u])
    {
        int v = ww.F.F,
            idd = ww.S;
        if (v == par[u][0].F) continue;
        Dfs_Get_ans(v,idd);
    }
    for (auto v : q[u])
    {
        s.insert(v);
        val.insert(v.S);
    }
    while (s.size())
    {
        auto it = s.end();
        --it;
        pii cur = *it;
        if (cur.F == h[u])
        {
            auto it1 = val.lower_bound(cur.S);
            val.erase(it1);
            s.erase(it);
        }
        else break;
    }
    if (val.size())
    {
        auto it = val.begin();
        ans[id] = (*it) - 1;
    }
    else ans[id] = -1;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen(Task".inp", "r",stdin);
    //freopen(Task".out", "w",stdout);
    cin >> n >> m;
    FOR(i,1,m)
    {
        int u,v,w;
        cin >> u >> v >> w;
        ed[i] = {u,v,w,i};
    }
    sort(ed + 1,ed + m + 1);
    FOR(i,1,m)
    {
        int u = ed[i].u,
            v = ed[i].v,
            w = ed[i].w,
            id = ed[i].id;
        int saveu = u,savev = v;
        u = Get_root(u),
        v = Get_root(v);
        if (u == v) continue;
        root[u] = v;
        dd[id] = 1;
        a[saveu].pb(mp(mp(savev,w),id));
        a[savev].pb(mp(mp(saveu,w),id));
    }
    Dfs(1);
    FOR(i,1,m)
    {
        int u = ed[i].u,
            v = ed[i].v,
            w = ed[i].w,
            id = ed[i].id;
        if (dd[id]) continue;
        pii ww = Lca(u,v);
        ans[id] = ww.S - 1;
        q[u].pb(mp(h[ww.F],w));
        q[v].pb(mp(h[ww.F],w));
    }
    Dfs_Get_ans(1,0);
    FOR(i,1,m) cout << ans[i] << ' ';
    return 0;
}


/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 200005
#define maxm 200005
#define pii pair <int,int>
#define Task "827D"
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,m,ans[maxn],root[maxn],h[maxn];
bool dd[maxm];
pii par[maxn][18];
multiset <int> val[maxn];
multiset <pii> s[maxn];
struct edge
{
    int u,v,w,id;
    bool operator < (edge &x) const
    {
        return w < x.w;
    }
}ed[maxm];
vector <pair <pii,int> > a[maxn];
vector <pii> q[maxn];
int Get_root(int u)
{
    return root[u] ? root[u] = Get_root(root[u]) : u;
}
void Dfs(int u)
{
    for (auto ww : a[u])
    {
        int v = ww.F.F,
            w = ww.F.S,
            id = ww.S;
        if (v == par[u][0].F) continue;
        h[v] = h[u] + 1;
        par[v][0] = mp(u,w);
        FOR(i,1,17)
        {
            par[v][i].F = par[par[v][i-1].F][i-1].F;
            par[v][i].S = max(par[v][i-1].S,par[par[v][i-1].F][i-1].S);
        }
        Dfs(v);
    }
}
pii Lca(int u,int v)
{
    int res = 0;
    if (h[u] < h[v]) swap(u,v);
    int del = h[u] - h[v];
    FOR(i,0,17)
        if ((del >> i) & 1)
        {
            res = max(res,par[u][i].S);
            u = par[u][i].F;
        }
    if (u == v) return mp(u,res);
    FORD(i,17,0)
        if (par[u][i].F != par[v][i].F)
        {
            res = max(res,par[u][i].S);
            res = max(res,par[v][i].S);
            u = par[u][i].F;
            v = par[v][i].F;
        }
    res = max(res,par[u][0].S);
    res = max(res,par[v][0].S);
    return mp(par[u][0].F,res);
}
void Dfs_Get_ans(int u,int id)
{
    for (auto v : q[u])
    {
        s[u].insert(v);
        val[u].insert(v.S);
    }
    for (auto ww : a[u])
    {
        int v = ww.F.F,
            idd = ww.S;
        if (v == par[u][0].F) continue;
        Dfs_Get_ans(v,idd);
        if (s[u].size() < s[v].size())
        {
            swap(s[u],s[v]);
            swap(val[u],val[v]);
        }
        while (s[v].size())
        {
            auto it = s[v].begin();
            s[u].insert(*it);
            auto it1 = val[v].begin();
            val[u].insert(*it1);
            s[v].erase(it);
            val[v].erase(it1);
        }
    }
    while (s[u].size())
    {
        auto it = s[u].end();
        --it;
        pii cur = *it;
        if (cur.F == h[u])
        {
            auto it1 = val[u].lower_bound(cur.S);
            s[u].erase(it);
            val[u].erase(it1);
        }
        else break;
    }
    if (s[u].size())
    {
        auto it = val[u].begin();
        ans[id] = (*it) - 1;
    }
    else ans[id] = -1;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen(Task".inp", "r",stdin);
    //freopen(Task".out", "w",stdout);
    cin >> n >> m;
    FOR(i,1,m)
    {
        int u,v,w;
        cin >> u >> v >> w;
        ed[i] = {u,v,w,i};
    }
    sort(ed + 1,ed + m + 1);
    FOR(i,1,m)
    {
        int u = ed[i].u,
            v = ed[i].v,
            w = ed[i].w,
            id = ed[i].id;
        int saveu = u,savev = v;
        u = Get_root(u),
        v = Get_root(v);
        if (u == v) continue;
        root[u] = v;
        dd[id] = 1;
        a[saveu].pb(mp(mp(savev,w),id));
        a[savev].pb(mp(mp(saveu,w),id));
    }
    Dfs(1);
    FOR(i,1,m)
    {
        int u = ed[i].u,
            v = ed[i].v,
            w = ed[i].w,
            id = ed[i].id;
        if (dd[id]) continue;
        pii ww = Lca(u,v);
        ans[id] = ww.S - 1;
        q[u].pb(mp(h[ww.F],w));
        q[v].pb(mp(h[ww.F],w));
    }
    Dfs_Get_ans(1,0);
    FOR(i,1,m) cout << ans[i] << ' ';
    return 0;
}


/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 200005
#define maxm 200005
#define pii pair <int,int>
#define Task "827D"
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,m,ans[maxn],root[maxn],h[maxn];
bool dd[maxm];
pii par[maxn][18];
multiset <int> val[maxn];
multiset <pii> s[maxn];
struct edge
{
    int u,v,w,id;
    bool operator < (edge &x) const
    {
        return w < x.w;
    }
}ed[maxm];
vector <pair <pii,int> > a[maxn];
vector <pii> q[maxn];
int Get_root(int u)
{
    return root[u] ? root[u] = Get_root(root[u]) : u;
}
void Dfs(int u)
{
    for (auto ww : a[u])
    {
        int v = ww.F.F,
            w = ww.F.S,
            id = ww.S;
        if (v == par[u][0].F) continue;
        h[v] = h[u] + 1;
        par[v][0] = mp(u,w);
        FOR(i,1,17)
        {
            par[v][i].F = par[par[v][i-1].F][i-1].F;
            par[v][i].S = max(par[v][i-1].S,par[par[v][i-1].F][i-1].S);
        }
        Dfs(v);
    }
}
pii Lca(int u,int v)
{
    int res = 0;
    if (h[u] < h[v]) swap(u,v);
    int del = h[u] - h[v];
    FOR(i,0,17)
        if ((del >> i) & 1)
        {
            res = max(res,par[u][i].S);
            u = par[u][i].F;
        }
    if (u == v) return mp(u,res);
    FORD(i,17,0)
        if (par[u][i].F != par[v][i].F)
        {
            res = max(res,par[u][i].S);
            res = max(res,par[v][i].S);
            u = par[u][i].F;
            v = par[v][i].S;
        }
    res = max(res,par[u][0].S);
    res = max(res,par[v][0].S);
    return mp(par[u][0].F,res);
}
void Dfs_Get_ans(int u,int id)
{
    for (auto v : q[u])
    {
        s[u].insert(v);
        val[u].insert(v.S);
    }
    for (auto ww : a[u])
    {
        int v = ww.F.F,
            idd = ww.S;
        if (v == par[u][0].F) continue;
        Dfs_Get_ans(v,idd);
        if (s[u].size() < s[v].size())
        {
            swap(s[u],s[v]);
            swap(val[u],val[v]);
        }
        while (s[v].size())
        {
            auto it = s[v].begin();
            s[u].insert(*it);
            auto it1 = val[v].begin();
            val[u].insert(*it1);
            s[v].erase(it);
            val[v].erase(it1);
        }
    }
    while (s[u].size())
    {
        auto it = s[u].end();
        --it;
        pii cur = *it;
        if (cur.F == h[u])
        {
            auto it1 = val[u].lower_bound(cur.S);
            s[u].erase(it);
            val[u].erase(it1);
        }
        else break;
    }
    if (s[u].size())
    {
        auto it = val[u].begin();
        ans[id] = (*it) - 1;
    }
    else ans[id] = -1;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen(Task".inp", "r",stdin);
    //freopen(Task".out", "w",stdout);
    cin >> n >> m;
    FOR(i,1,m)
    {
        int u,v,w;
        cin >> u >> v >> w;
        ed[i] = {u,v,w,i};
    }
    sort(ed + 1,ed + m + 1);
    FOR(i,1,m)
    {
        int u = ed[i].u,
            v = ed[i].v,
            w = ed[i].w,
            id = ed[i].id;
        int saveu = u,savev = v;
        u = Get_root(u),
        v = Get_root(v);
        if (u == v) continue;
        root[u] = v;
        dd[id] = 1;
        a[saveu].pb(mp(mp(savev,w),id));
        a[savev].pb(mp(mp(saveu,w),id));
    }
    Dfs(1);
    FOR(i,1,m)
    {
        int u = ed[i].u,
            v = ed[i].v,
            w = ed[i].w,
            id = ed[i].id;
        if (dd[id]) continue;
        pii ww = Lca(u,v);
        ans[id] = ww.S - 1;
        q[u].pb(mp(h[ww.F],w));
        q[v].pb(mp(h[ww.F],w));
    }
    Dfs_Get_ans(1,0);
    FOR(i,1,m) cout << ans[i] << ' ';
    return 0;
}


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

#define int long long

typedef pair<int,int> ii;
vector <ii> ed[200069];

int sum[200069],re[200069],Free[200069],n,u,v,m,x,d[200069],l[200069],p[200069][25],mi[200069][25];
vector <int> add[200069];
vector <int> del[200069];
multiset <int> s;

struct data{
    int x,u,v,id;
}da[200069];
int cmp(data x,data y){
    return x.x < y.x;
}
void DFS(int u,int pre,int deep){
    int v;
    l[u]=deep;
    for (int i=0;i<ed[u].size();i++){
        v=ed[u][i].first;
        if (v!=pre){
            p[v][0]=u;
            mi[v][0]=ed[u][i].second;
            DFS(v,u,deep+1);
        }
    }
}
void cal(int u,int pre){
    int v;
    for (int i = 0;i < ed[u].size(); i++){
        v = ed[u][i].first;
        if (v == pre) continue;
        cal(v,u);
    }
    for (int i = 0;i < add[u].size(); i++) s.insert(add[u][i]);
    for (int i = 0;i < del[u].size(); i++) s.erase(del[u][i]);
    sum[u] = (*s.begin());
}
int lca(int x,int y,int t){
    int mic=-1e9;
    for (int i=22;i>=0;i--){
        if (l[p[x][i]]>=l[y]){
            mic=max(mic,mi[x][i]);
            x=p[x][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (l[p[y][i]]>=l[x]){
            mic=max(mic,mi[y][i]);
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (p[x][i]!=p[y][i]){
            mic=max(mic,mi[x][i]);
            mic=max(mic,mi[y][i]);
            x=p[x][i];
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (x!=y){
            mic=max(mic,mi[x][0]);
            mic=max(mic,mi[y][0]);
            x=p[x][0];
            y=p[y][0];
        }
    }
    if (t == 0) return x;
    else return mic;
}
int root(int x){
    if (d[x]<0) return x;
    else return d[x]=root(d[x]);
}
void Union(int x,int y){
    d[x]=d[x]+d[y];
    d[x]=y;
}
void init(){
    DFS(1,0,1);
    for (int i=1;i<=22;i++)
    for (int j=1;j<=n;j++){
        p[j][i]=p[p[j][i-1]][i-1];
        mi[j][i]=max(mi[j][i-1],mi[p[j][i-1]][i-1]);
    }
}
main(){
    cin >> n >> m;
    for (int i = 1;i <= m; i++){
        scanf("%lld%lld%lld",&da[i].u,&da[i].v,&da[i].x);
        da[i].id = i;
    }
    sort(da + 1,da + 1 + m,cmp);
    memset(d,-1,sizeof(d));
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        //cout << x << endl;
        if (root(u) != root(v)){
            Free[i] = 1;
            Union(root(u),root(v));
            ed[u].push_back(ii(v,x));
            ed[v].push_back(ii(u,x));
        }
    }
    init();
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (Free[i] == 0){
            add[u].push_back(x);
            add[v].push_back(x);
            del[lca(u,v,0)].push_back(x);
        }
    }
    cal(1,0);
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (l[u] > l[v]) swap(u,v);
        if (Free[i]){
            re[da[i].id] = sum[v] - 1;
        }
        else {
            re[da[i].id] = lca(u,v,1) - 1;
        }
    }
    for (int i = 1;i <= m; i++){
        printf("%lld ",re[i]);
    }
}


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

#define int long long

typedef pair<int,int> ii;
vector <ii> ed[200069];

int sum[200069],re[200069],Free[200069],n,u,v,m,x,d[200069],l[200069],p[200069][25],mi[200069][25];
vector <int> add[200069];
vector <int> del[200069];
multiset <int> s;

struct data{
    int x,u,v;
}da[200069];
int cmp(data x,data y){
    return x.x < y.x;
}
void DFS(int u,int pre,int deep){
    int v;
    l[u]=deep;
    for (int i=0;i<ed[u].size();i++){
        v=ed[u][i].first;
        if (v!=pre){
            p[v][0]=u;
            mi[v][0]=ed[u][i].second;
            DFS(v,u,deep+1);
        }
    }
}
void cal(int u,int pre){
    int v;
    for (int i = 0;i < ed[u].size(); i++){
        v = ed[u][i].first;
        if (v == pre) continue;
        cal(v,u);
    }
    for (int i = 0;i < add[u].size(); i++) s.insert(add[u][i]);
    for (int i = 0;i < del[u].size(); i++) s.erase(del[u][i]);
    sum[u] = (*s.begin());
}
int lca(int x,int y,int t){
    int mic=1e9;
    for (int i=22;i>=0;i--){
        if (l[p[x][i]]>=l[y]){
            mic=min(mic,mi[x][i]);
            x=p[x][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (l[p[y][i]]>=l[x]){
            mic=min(mic,mi[y][i]);
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (p[x][i]!=p[y][i]){
            mic=min(mic,mi[x][i]);
            mic=min(mic,mi[y][i]);
            x=p[x][i];
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (x!=y){
            mic=min(mic,mi[x][0]);
            mic=min(mic,mi[y][0]);
            x=p[x][0];
            y=p[y][0];
        }
    }
    if (t == 0) return x;
    else return mic;
}
int root(int x){
    if (d[x]<0) return x;
    else return d[x]=root(d[x]);
}
void Union(int x,int y){
    d[x]=d[x]+d[y];
    d[x]=y;
}
void init(){
    DFS(1,0,1);
    for (int i=1;i<=22;i++)
    for (int j=1;j<=n;j++){
        p[j][i]=p[p[j][i-1]][i-1];
        mi[j][i]=min(mi[j][i-1],mi[p[j][i-1]][i-1]);
    }
}
main(){
    cin >> n >> m;
    for (int i = 1;i <= m; i++){
        scanf("%lld%lld%lld",&da[i].u,&da[i].v,&da[i].x);
    }
    sort(da + 1,da + 1 + m,cmp);
    memset(d,-1,sizeof(d));
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (root(u) != root(v)){
            Free[i] = 1;;
            Union(root(u),root(v));
            ed[u].push_back(ii(v,x));
            ed[v].push_back(ii(u,x));
        }
    }
    init();
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (Free[i] == 0){
            add[u].push_back(x);
            add[v].push_back(x);
            del[lca(u,v,0)].push_back(x);
        }
    }
    cal(1,0);
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (l[u] > l[v]) swap(u,v);
        if (Free[i]){
            re[i] = sum[v] - 1;
        }
        else {
            re[i] = lca(u,v,1) - 1;
        }
    }
    for (int i = 1;i <= m; i++){
        printf("%lld ",re[i]);
    }
}


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

#define int long long

typedef pair<int,int> ii;
vector <ii> ed[200069];

int mx[200069][25],sum[200069],re[200069],Free[200069],n,u,v,m,x,d[200069],l[200069],p[200069][25],mi[200069][25];
vector <int> add[200069];
vector <int> del[200069];
set <int> s;

struct data{
    int x,u,v,id;
}da[200069];
int cmp(data x,data y){
    return x.x < y.x;
}
void DFS(int u,int pre,int deep){
    int v;
    l[u]=deep;
    for (int i=0;i<ed[u].size();i++){
        v=ed[u][i].first;
        if (v!=pre){
            p[v][0]=u;
            mi[v][0]=ed[u][i].second;
            DFS(v,u,deep+1);
        }
    }
}
void jump(int x,int y,int v){
    for (int i=22;i>=0;i--){
        if (l[p[x][i]]>=l[y]){
            mx[x][i] = min(mx[x][i],v);
            x = p[x][i];
        }
    }
}
int lca(int x,int y,int t){
    int mic=-1e9;
    for (int i=22;i>=0;i--){
        if (l[p[x][i]]>=l[y]){
            mic=max(mic,mi[x][i]);
            x=p[x][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (l[p[y][i]]>=l[x]){
            mic=max(mic,mi[y][i]);
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (p[x][i]!=p[y][i]){
            mic=max(mic,mi[x][i]);
            mic=max(mic,mi[y][i]);
            x=p[x][i];
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (x!=y){
            mic=max(mic,mi[x][0]);
            mic=max(mic,mi[y][0]);
            x=p[x][0];
            y=p[y][0];
        }
    }
    if (t == 0) return x;
    else return mic;
}
int root(int x){
    if (d[x]<0) return x;
    else return d[x]=root(d[x]);
}
void Union(int x,int y){
    d[x]=d[x]+d[y];
    d[x]=y;
}
void init(){
    DFS(1,0,1);
    for (int i=1;i<=22;i++)
    for (int j=1;j<=n;j++){
        p[j][i]=p[p[j][i-1]][i-1];
        mi[j][i]=max(mi[j][i-1],mi[p[j][i-1]][i-1]);
    }
}
main(){
    cin >> n >> m;
    for (int i = 1;i <= m; i++){
        scanf("%lld%lld%lld",&da[i].u,&da[i].v,&da[i].x);
        da[i].id = i;
    }
    sort(da + 1,da + 1 + m,cmp);
    memset(d,-1,sizeof(d));
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        //cout << x << endl;
        if (root(u) != root(v)){
            Free[i] = 1;
            Union(root(u),root(v));
            ed[u].push_back(ii(v,x));
            ed[v].push_back(ii(u,x));
        }
    }
    init();
    for (int i=0;i<=22;i++)
    for (int j=1;j<=n;j++) mx[j][i] = 1e9;
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (Free[i] == 0){
            jump(u,lca(u,v,0),x);
            jump(v,lca(u,v,0),x);
        }
    }
    for (int i=22;i>=1;i--)
    for (int j=1;j<=n;j++){
        mx[j][i-1] = min(mx[j][i-1],mx[j][i]);
        mx[p[j][i-1]][i-1] = min(mx[p[j][i-1]][i-1],mx[j][i]);
    }
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (l[u] > l[v]) swap(u,v);
        if (Free[i]){
            re[da[i].id] = mx[v][0] - 1;
        }
        else {
            re[da[i].id] = lca(u,v,1) - 1;
        }
    }
    for (int i = 1;i <= m; i++){
        printf("%lld ",re[i]);
    }
}


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

#define int long long

typedef pair<int,int> ii;
vector <ii> ed[200069];

int mx[200069][25],sum[200069],re[200069],Free[200069],n,u,v,m,x,d[200069],l[200069],p[200069][25],mi[200069][25];
vector <int> add[200069];
vector <int> del[200069];
set <int> s;

struct data{
    int x,u,v,id;
}da[200069];
int cmp(data x,data y){
    return x.x < y.x;
}
void DFS(int u,int pre,int deep){
    int v;
    l[u]=deep;
    for (int i=0;i<ed[u].size();i++){
        v=ed[u][i].first;
        if (v!=pre){
            p[v][0]=u;
            mi[v][0]=ed[u][i].second;
            DFS(v,u,deep+1);
        }
    }
}
void jump(int x,int y,int v){
    for (int i=22;i>=0;i--){
        if (l[p[x][i]]>=l[y]){
            mx[x][i] = min(mx[x][i],v);
            x = p[x][i];
        }
    }
}
int lca(int x,int y,int t){
    int mic=-1e9;
    for (int i=22;i>=0;i--){
        if (l[p[x][i]]>=l[y]){
            mic=max(mic,mi[x][i]);
            x=p[x][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (l[p[y][i]]>=l[x]){
            mic=max(mic,mi[y][i]);
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (p[x][i]!=p[y][i]){
            mic=max(mic,mi[x][i]);
            mic=max(mic,mi[y][i]);
            x=p[x][i];
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (x!=y){
            mic=max(mic,mi[x][0]);
            mic=max(mic,mi[y][0]);
            x=p[x][0];
            y=p[y][0];
        }
    }
    if (t == 0) return x;
    else return mic;
}
int root(int x){
    if (d[x]<0) return x;
    else return d[x]=root(d[x]);
}
void Union(int x,int y){
    d[x]=d[x]+d[y];
    d[x]=y;
}
void init(){
    DFS(1,0,1);
    for (int i=1;i<=22;i++)
    for (int j=1;j<=n;j++){
        p[j][i]=p[p[j][i-1]][i-1];
        mi[j][i]=max(mi[j][i-1],mi[p[j][i-1]][i-1]);
    }
}
main(){
    cin >> n >> m;
    for (int i = 1;i <= m; i++){
        scanf("%lld%lld%lld",&da[i].u,&da[i].v,&da[i].x);
        da[i].id = i;
    }
    sort(da + 1,da + 1 + m,cmp);
    memset(d,-1,sizeof(d));
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        //cout << x << endl;
        if (root(u) != root(v)){
            Free[i] = 1;
            Union(root(u),root(v));
            ed[u].push_back(ii(v,x));
            ed[v].push_back(ii(u,x));
        }
    }
    init();
    for (int i=0;i<=22;i++)
    for (int j=1;j<=n;j++) mx[j][i] = 1e9;
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (Free[i] == 0){
            jump(u,lca(u,v,0),x);
            jump(v,lca(u,v,0),x);
        }
    }
    for (int i=22;i>=1;i--)
    for (int j=1;j<=n;j++){
        mx[j][i-1] = min(mx[j][i-1],mx[j][i]);
        mx[p[j][i-1]][i-1] = min(mx[p[j][i-1]][i-1],mx[j][i]);
    }
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (l[u] > l[v]) swap(u,v);
        if (Free[i]){
            if (mx[v][0] != 1e9) re[da[i].id] = mx[v][0] - 1;
            else re[da[i].id] = -1;
        }
        else {
            re[da[i].id] = lca(u,v,1) - 1;
        }
    }
    for (int i = 1;i <= m; i++){
        printf("%lld ",re[i]);
    }
}


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

#define int long long

typedef pair<int,int> ii;
vector <ii> ed[200069];

int mx[200069][25],sum[200069],re[200069],Free[200069],n,u,v,m,x,d[200069],l[200069],p[200069][25],mi[200069][25];
vector <int> add[200069];
vector <int> del[200069];
set <int> s;

struct data{
    int x,u,v,id;
}da[200069];
int cmp(data x,data y){
    return x.x < y.x;
}
void DFS(int u,int pre,int deep){
    int v;
    l[u]=deep;
    for (int i=0;i<ed[u].size();i++){
        v=ed[u][i].first;
        if (v!=pre){
            p[v][0]=u;
            mi[v][0]=ed[u][i].second;
            DFS(v,u,deep+1);
        }
    }
}
void jump(int x,int y,int v){
    for (int i=22;i>=0;i--){
        if (l[p[x][i]]>=l[y]){
            mx[x][i] = min(mx[x][i],v);
            x = p[x][i];
        }
    }
}
int lca(int x,int y,int t){
    int mic=-1e18;
    for (int i=22;i>=0;i--){
        if (l[p[x][i]]>=l[y]){
            mic=max(mic,mi[x][i]);
            x=p[x][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (l[p[y][i]]>=l[x]){
            mic=max(mic,mi[y][i]);
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (p[x][i]!=p[y][i]){
            mic=max(mic,mi[x][i]);
            mic=max(mic,mi[y][i]);
            x=p[x][i];
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (x!=y){
            mic=max(mic,mi[x][0]);
            mic=max(mic,mi[y][0]);
            x=p[x][0];
            y=p[y][0];
        }
    }
    if (t == 0) return x;
    else return mic;
}
int root(int x){
    if (d[x]<0) return x;
    else return d[x]=root(d[x]);
}
void Union(int x,int y){
    d[x]=d[x]+d[y];
    d[x]=y;
}
void init(){
    DFS(1,0,1);
    for (int i=1;i<=22;i++)
    for (int j=1;j<=n;j++){
        p[j][i]=p[p[j][i-1]][i-1];
        mi[j][i]=max(mi[j][i-1],mi[p[j][i-1]][i-1]);
    }
}
main(){
    cin >> n >> m;
    for (int i = 1;i <= m; i++){
        scanf("%lld%lld%lld",&da[i].u,&da[i].v,&da[i].x);
        da[i].id = i;
    }
    sort(da + 1,da + 1 + m,cmp);
    memset(d,-1,sizeof(d));
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        //cout << x << endl;
        if (root(u) != root(v)){
            Free[i] = 1;
            Union(root(u),root(v));
            ed[u].push_back(ii(v,x));
            ed[v].push_back(ii(u,x));
        }
    }
    init();
    for (int i=0;i<=22;i++)
    for (int j=1;j<=n;j++) mx[j][i] = 1e18;
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (Free[i] == 0){
            jump(u,lca(u,v,0),x);
            jump(v,lca(u,v,0),x);
        }
    }
    for (int i=22;i>=1;i--)
    for (int j=1;j<=n;j++){
        mx[j][i-1] = min(mx[j][i-1],mx[j][i]);
        mx[p[j][i-1]][i-1] = min(mx[p[j][i-1]][i-1],mx[j][i]);
    }
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (l[u] > l[v]) swap(u,v);
        if (Free[i]){
            if (mx[v][0] != 1e18) re[da[i].id] = mx[v][0] - 1;
            else re[da[i].id] = -1;
        }
        else {
            re[da[i].id] = lca(u,v,1) - 1;
        }
    }
    for (int i = 1;i <= m; i++){
        printf("%lld ",re[i]);
    }
}


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

#define int long long

typedef pair<int,int> ii;
vector <ii> ed[200069];

int sum[200069],re[200069],Free[200069],n,u,v,m,x,d[200069],l[200069],p[200069][25],mi[200069][25];
vector <int> add[200069];
vector <int> del[200069];
multiset <int> s;

struct data{
    int x,u,v;
}da[200069];
int cmp(data x,data y){
    return x.x < y.x;
}
void DFS(int u,int pre,int deep){
    int v;
    l[u]=deep;
    for (int i=0;i<ed[u].size();i++){
        v=ed[u][i].first;
        if (v!=pre){
            p[v][0]=u;
            mi[v][0]=ed[u][i].second;
            DFS(v,u,deep+1);
        }
    }
}
void cal(int u,int pre){
    int v;
    for (int i = 0;i < ed[u].size(); i++){
        v = ed[u][i].first;
        if (v == pre) continue;
        cal(v,u);
    }
    for (int i = 0;i < add[u].size(); i++) s.insert(add[u][i]);
    for (int i = 0;i < del[u].size(); i++) s.erase(del[u][i]);
    sum[u] = (*s.begin());
}
int lca(int x,int y,int t){
    int mic=1e9;
    for (int i=22;i>=0;i--){
        if (l[p[x][i]]>=l[y]){
            mic=min(mic,mi[x][i]);
            x=p[x][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (l[p[y][i]]>=l[x]){
            mic=min(mic,mi[y][i]);
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (p[x][i]!=p[y][i]){
            mic=min(mic,mi[x][i]);
            mic=min(mic,mi[y][i]);
            x=p[x][i];
            y=p[y][i];
        }
    }
    for (int i=22;i>=0;i--){
        if (x!=y){
            mic=min(mic,mi[x][0]);
            mic=min(mic,mi[y][0]);
            x=p[x][0];
            y=p[y][0];
        }
    }
    if (t == 0) return x;
    else return mic;
}
int root(int x){
    if (d[x]<0) return x;
    else return d[x]=root(d[x]);
}
void Union(int x,int y){
    d[x]=d[x]+d[y];
    d[x]=y;
}
void init(){
    DFS(1,0,1);
    for (int i=1;i<=22;i++)
    for (int j=1;j<=n;j++){
        p[j][i]=p[p[j][i-1]][i-1];
        mi[j][i]=min(mi[j][i-1],mi[p[j][i-1]][i-1]);
    }
}
main(){
    cin >> n >> m;
    for (int i = 1;i <= m; i++){
        scanf("%lld%lld%lld",&da[i].u,&da[i].v,&da[i].x);
    }
    sort(da + 1,da + 1 + m,cmp);
    memset(d,-1,sizeof(d));
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (root(u) != root(v)){
            Free[i] = 1;;
            Union(u,v);
            ed[u].push_back(ii(v,x));
            ed[v].push_back(ii(u,x));
        }
    }
    init();
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (Free[i] == 0){
            add[u].push_back(x);
            add[v].push_back(x);
            del[lca(u,v,0)].push_back(x);
        }
    }
    cal(1,0);
    for (int i = 1;i <= m; i++){
        u = da[i].u;v = da[i].v;x = da[i].x;
        if (l[u] > l[v]) swap(u,v);
        if (Free[i]){
            re[i] = sum[v] - 1;
        }
        else {
            re[i] = lca(u,v,1) - 1;
        }
    }
    for (int i = 1;i <= m; i++){
        printf("%lld ",re[i]);
    }
}


#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef vector<pii> vii;
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()

struct edge
{
    int x, y, w, id;

    bool operator < (const edge &o) const
    {
        return w < o.w;
    }
};

struct diredge
{
    int to, id;
};

struct dsu
{
    int n;
    vector<edge> e;
    vector<vvi> comp;
    vvi in;
    vi par, ans;
    vector<set<int>> sons;
    vector<vector<diredge>> g;
    // 0 - comp, 1 - bicomp

    void add_edge (int cx, int cy)
    {
        assert(cx != cy);
        assert(par[cx] == -1);
        par[cx] = cy;
        sons[cy].insert(cx);
    }

    void del_edge (int cx, int cy)
    {
        assert(par[cx] == cy);
        par[cx] = -1;
        sons[cy].erase(cx);
    }

    void reroot (int cx)
    {
        if (par[cx] != -1)
        {
            int to = par[cx];
            reroot(to);
            del_edge(cx, to);
            add_edge(to, cx);
        }
        assert(par[cx] == -1);
    }

    dsu (int n_, vector<edge> ore) : n(n_), e(ore)
    {
        par.assign(n, -1);
        comp.assign(2, vvi(n));
        forn (i, 2) forn (j, n) comp[i][j] = vi(1, j);
        in.assign(2, vi(n));
        forn (i, 2) iota(all(in[i]), 0);
        sons.resize(n);

        g.resize(n);
        forn (i, sz(e))
        {
            g[e[i].x].pb(diredge{e[i].y, e[i].id});
            g[e[i].y].pb(diredge{e[i].x, e[i].id});
        }

        sort(all(ore));
        ans = vi(sz(ore), -1);
        for (const auto &ed : ore)
            add(ed);
    }

    int merge (int x, int y, int layer, int id)
    {
        assert(e[id].id == id);
        if (layer == 0)
            assert(x == e[id].x && y == e[id].y);
        if (sz(comp[layer][x]) > sz(comp[layer][y]))
            swap(x, y);

        int bx = in[1][x], by = in[1][y];
        x = in[layer][x], y = in[layer][y];
        if (x == y)
            return y;

        for (int v : comp[layer][x])
        {
            comp[layer][y].pb(v);
            for (diredge from : g[v]) if (in[layer][from.to] == y)
            if ((layer == 1) || (layer == 0 && from.id != id))
            {
                if (ans[from.id] == -1)
                    ans[from.id] = e[id].w - 1;
            }
        }

        for (int v : comp[layer][x])
            in[layer][v] = y;

        if (layer == 1 && par[x] != -1)
            del_edge(x, par[x]);

        comp[layer][x].clear();
        comp[layer][x].shrink_to_fit();

        if (layer == 0)
        {
            reroot(bx);
            add_edge(bx, by);
        }

        return y;
    }

    void add (const edge &ed)
    {
        int x = ed.x, y = ed.y;
        if (in[1][x] == in[1][y])
            return;

        if (in[0][x] != in[0][y])
            merge(x, y, 0, ed.id);
        else
        {
            vi vs = get_path(in[1][x], in[1][y]);
            int last = vs[0];
            int realp = par[vs.back()];
            forn (i, sz(vs) - 1)
                last = merge(comp[1][last][0], comp[1][vs[i + 1]][0], 1, ed.id);

            for (int v : vs)
            {
                vi memsons(all(sons[v]));
                for (int b : memsons)
                {
                    del_edge(b, v);
                    if (b != last)
                        add_edge(b, last);
                }
            }

            if (par[last] != -1)
                del_edge(last, par[last]);
            if (realp != -1)
                add_edge(last, realp);
        }
    }

    vi used;
    int tms;

    vi get_path (int x, int y)
    {
        if (used.empty())
        {
            tms = 0;
            used = vi(n);
        }
        tms++;

        vi vx, vy;
        int lca = -1;

        while (true)
        {
            bool ok = false;
            forn (it, 2)
            {
                int &cur = (it == 0 ? x : y);
                vi &vcur = (it == 0 ? vx : vy);
                if (cur != -1)
                {
                    vcur.pb(cur);
                    if (used[cur] == tms)
                    {
                        lca = cur;
                        ok = true;
                        break;
                    }

                    used[cur] = tms;
                    cur = par[cur];
                }
            }

            if (ok)
                break;

            if (x == -1 && y == -1)
            {
                copy(all(vx), ostream_iterator<int>(cerr, " "));
                cerr << endl;
                copy(all(vy), ostream_iterator<int>(cerr, " "));
                cerr << endl;
                assert(false);
            }
        }

        vi res;
        forn (it, 2)
        {
            vi &vcur = (it == 0 ? vx : vy);
            int pos = find(all(vcur), lca) - vcur.begin();
            assert(pos != sz(vcur));
            copy(vcur.begin(), vcur.begin() + pos + it, back_inserter(res));
        }

        return res;
    }
};

struct normal_dsu
{
    vi par;

    normal_dsu (int n)
    {
        par = vi(n);
        iota(all(par), 0);
    }

    int group (int x)
    {
        if (x == par[x])
            return x;
        return par[x] = group(par[x]);
    }

    void merge (int x, int y)
    {
        par[group(x)] = group(y);
    }
};

vi brut (int n, vector<edge> e)
{
    sort(all(e));
    vi ans(sz(e), -1);

    forn (i, sz(e))
    {
        normal_dsu dsu(n);
        forn (j, sz(e)) if (j != i)
        {
            dsu.merge(e[j].x, e[j].y);
            if (dsu.group(e[i].x) == dsu.group(e[i].y))
            {
                ans[e[i].id] = e[j].w - 1;
                break;
            }
        }
    }

    return ans;
}

void solve (int n)
{
    int m;
    cin >> m;
    vector<edge> e(m);
    forn (i, m)
    {
        int x, y, w;
        cin >> x >> y >> w;
        --x, --y;
        e[i] = edge{x, y, w, i};
    }

    dsu dsu(n, e);

//    vi bans = brut(n, e);
//    assert(dsu.ans == bans);

    forn (i, m)
        cout << dsu.ans[i] << " \n"[i + 1 == m];
}

int main()
{
#ifdef LOCAL
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    while (cin >> n)
        solve(n);
}


#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef vector<pii> vii;
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()

struct edge
{
    int x, y, w, id;

    bool operator < (const edge &o) const
    {
        return w < o.w;
    }
};

struct diredge
{
    int to, id;
};

struct dsu
{
    int n;
    vector<edge> e;
    vector<vvi> comp;
    vvi in;
    vi par, ans;
    vector<unordered_set<int>> sons;
    vector<vector<diredge>> g;

    void add_edge (int cx, int cy)
    {
        assert(cx != cy);
        assert(par[cx] == -1);
        par[cx] = cy;
        sons[cy].insert(cx);
    }

    void del_edge (int cx, int cy)
    {
        assert(par[cx] == cy);
        par[cx] = -1;
        sons[cy].erase(cx);
    }

    void reroot (int cx)
    {
        if (par[cx] != -1)
        {
            int to = par[cx];
            reroot(to);
            del_edge(cx, to);
            add_edge(to, cx);
        }
        assert(par[cx] == -1);
    }

    dsu (int n_, vector<edge> ore) : n(n_), e(ore)
    {
        par.assign(n, -1);
        comp.assign(2, vvi(n));
        forn (i, 2) forn (j, n) comp[i][j] = vi(1, j);
        in.assign(2, vi(n));
        forn (i, 2) iota(all(in[i]), 0);
        sons.resize(n);

        g.resize(n);
        forn (i, sz(e))
        {
            g[e[i].x].pb(diredge{e[i].y, e[i].id});
            g[e[i].y].pb(diredge{e[i].x, e[i].id});
        }

        sort(all(ore));
        ans = vi(sz(ore), -1);
        for (const auto &ed : ore)
            add(ed);
    }

    int merge (int x, int y, int layer, int id)
    {
        assert(e[id].id == id);
        if (layer == 0)
            assert(x == e[id].x && y == e[id].y);

        int bx = in[1][x], by = in[1][y];
        x = in[layer][x], y = in[layer][y];
        if (sz(comp[layer][x]) > sz(comp[layer][y]))
            swap(x, y);

        if (x == y)
            return y;

        for (int v : comp[layer][x])
        {
            comp[layer][y].pb(v);
            for (diredge from : g[v]) if (in[layer][from.to] == y)
            if ((layer == 1) || (layer == 0 && from.id != id))
            {
                if (ans[from.id] == -1)
                    ans[from.id] = e[id].w - 1;
            }
        }

        for (int v : comp[layer][x])
            in[layer][v] = y;

        if (layer == 1 && par[x] != -1)
            del_edge(x, par[x]);

        comp[layer][x].clear();
        comp[layer][x].shrink_to_fit();

        if (layer == 0)
        {
            reroot(bx);
            add_edge(bx, by);
        }

        return y;
    }

    void add (const edge &ed)
    {
        int x = ed.x, y = ed.y;
        if (in[1][x] == in[1][y])
        {
            assert(in[0][x] == in[0][y]);
            return;
        }

        if (in[0][x] != in[0][y])
            merge(x, y, 0, ed.id);
        else
        {
            vi vs = get_path(in[1][x], in[1][y]);
            int last = vs[0];
            int realp = par[vs.back()];
            forn (i, sz(vs) - 1)
                last = merge(comp[1][last][0], comp[1][vs[i + 1]][0], 1, ed.id);

            for (int v : vs) if (v != last)
            {
                vi memsons(all(sons[v]));
                for (int b : memsons)
                {
                    del_edge(b, v);
                    if (b != last)
                        add_edge(b, last);
                }
            }

            if (par[last] != realp)
            {
                if (par[last] != -1)
                    del_edge(last, par[last]);
                if (realp != -1)
                    add_edge(last, realp);
            }
        }
    }

    vi used;
    int tms;

    vi get_path (int x, int y)
    {
        if (used.empty())
        {
            tms = 0;
            used = vi(n);
        }
        tms++;

        vi vx, vy;
        int lca = -1;

        while (true)
        {
            bool ok = false;
            forn (it, 2)
            {
                int &cur = (it == 0 ? x : y);
                vi &vcur = (it == 0 ? vx : vy);
                if (cur != -1)
                {
                    vcur.pb(cur);
                    if (used[cur] == tms)
                    {
                        lca = cur;
                        ok = true;
                        break;
                    }

                    used[cur] = tms;
                    cur = par[cur];
                }
            }

            if (ok)
                break;
        }

        vi res;
        forn (it, 2)
        {
            vi &vcur = (it == 0 ? vx : vy);
            int pos = find(all(vcur), lca) - vcur.begin();
            assert(pos != sz(vcur));
            copy(vcur.begin(), vcur.begin() + pos + it, back_inserter(res));
        }

        return res;
    }
};

void solve (int n)
{
    int m;
    cin >> m;
    vector<edge> e(m);
    forn (i, m)
    {
        int x, y, w;
        cin >> x >> y >> w;
        --x, --y;
        e[i] = edge{x, y, w, i};
    }

    dsu dsu(n, e);
    forn (i, m)
        cout << dsu.ans[i] << " \n"[i + 1 == m];
}

int main()
{
#ifdef LOCAL
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    while (cin >> n)
        solve(n);
}


#ifdef LOCAL
//#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef vector<pii> vii;
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()

struct edge
{
    int x, y, w, id;

    bool operator < (const edge &o) const
    {
        return w < o.w;
    }
};

struct diredge
{
    int to, id;
};

struct dsu
{
    int n;
    vvi dpar, rk;
    vector<vector<unordered_set<int>>> sons;
    // comp and bicomp dsu structures
    vi par;
    // bicomp tree
    vector<vector<diredge>> g;
    vector<edge> e;
    vi ans;
    // original graph

    dsu (int n_, vector<edge> ore) : n(n_), e(ore)
    {
        par.assign(n, -1);
        rk.assign(2, vi(n, 1));
        dpar.assign(2, vi(n));
        forn (i, 2) iota(all(dpar[i]), 0);
        sons.assign(2, vector<unordered_set<int>>(n));

        ans.resize(sz(e));
        g.resize(n);
        forn (i, sz(e))
        {
            g[e[i].x].pb(diredge{e[i].y, e[i].id});
            g[e[i].y].pb(diredge{e[i].x, e[i].id});
        }

        sort(all(ore));
        ans = vi(sz(ore), -1);
        for (const auto &ed : ore)
            add(ed);
    }

    void change (int layer, int x, int from, int to)
    {
        assert(dpar[layer][x] == from);
        dpar[layer][x] = to;
        sons[layer][from].erase(x);
        sons[layer][to].insert(x);
    }

    int get (int layer, int x)
    {
        if (layer == 0)
            x = get(1, x);
        if (x == -1 || dpar[layer][x] == x)
            return x;
        int to = get(layer, dpar[layer][x]);
        change(layer, x, dpar[layer][x], to);
        return to;
    }

    int bicomp (int x)
    {
        return get(1, x);
    }

    int comp (int x)
    {
        return get(0, x);
    }

    vi bfs (int x, int layer)
    {
        vi res(1, x);
        int l = 0;
        while (l < sz(res))
        {
            int cur = res[l++];
            for (int dest : sons[layer][cur])
            {
                assert(dest != cur);
                res.pb(dest);
            }
        }

        return res;
    }

    void reroot (int v)
    {
        v = bicomp(v);
        int root = v, child = -1;
        while (v != -1)
        {
            int p = bicomp(par[v]);
            par[v] = child;
            dpar[0][v] = child;
            child = v, v = p;
        }
        rk[0][root] = rk[0][child];
    }

    void merge (int x, int y, int layer, int id)
    {
        // x and y already comps / bicomps
        assert(dpar[layer][x] == x && dpar[layer][y] == y);
        if (rk[layer][x] > rk[layer][y])
            swap(x, y);

        vi vs = bfs(x, layer);
        for (int v : vs)
        {
            for (diredge from : g[v]) if (get(layer, from.to) == y)
            if ((layer == 1) || (layer == 0 && from.id != id))
            {
                if (ans[from.id] == -1)
                    ans[from.id] = e[id].w - 1;
            }
        }
    }

    void add (const edge &ed)
    {
        int bx = bicomp(ed.x), by = bicomp(ed.y);
        if (bx == by)
            return;
        int cx = comp(ed.x), cy = comp(ed.y);
        if (cx != cy)
        {
            if (rk[0][cx] > rk[0][cy])
            {
                swap(bx, by);
                swap(cx, cy);
            }

            merge(cx, cy, 0, ed.id);
            reroot(bx);
            par[bx] = dpar[0][bx] = by;
            rk[0][cy] += rk[0][cx];
        }
        else
        {
            vi vs = get_path(bx, by);
            forn (i, sz(vs) - 1)
            {
                merge(vs[i], vs.back(), 1, ed.id);
                change(1, vs[i], vs[i], vs.back());
                rk[1][vs.back()] += rk[1][vs[i]];
            }
        }
    }

    vi used;
    int tms;

    vi get_path (int x, int y)
    {
        if (used.empty())
        {
            tms = 0;
            used = vi(n);
        }
        tms++;

        vi vx, vy;
        int lca = -1;

        while (true)
        {
            bool ok = false;
            forn (it, 2)
            {
                int &cur = (it == 0 ? x : y);
                cur = bicomp(cur);
                vi &vcur = (it == 0 ? vx : vy);
                if (cur != -1)
                {
                    vcur.pb(cur);
                    if (used[cur] == tms)
                    {
                        lca = cur;
                        ok = true;
                        break;
                    }

                    used[cur] = tms;
                    cur = par[cur];
                }
            }

            if (ok)
                break;
        }

        vi res;
        forn (it, 2)
        {
            vi &vcur = (it == 0 ? vx : vy);
            int pos = find(all(vcur), lca) - vcur.begin();
            assert(pos != sz(vcur));
            copy(vcur.begin(), vcur.begin() + pos + it, back_inserter(res));
        }
        assert(res.back() == lca);
        for (int v : res)
            assert(dpar[1][v] == v);
        return res;
    }
};

void solve (int n)
{
    int m;
    cin >> m;
    vector<edge> e(m);
    forn (i, m)
    {
        int x, y, w;
        cin >> x >> y >> w;
        --x, --y;
        e[i] = edge{x, y, w, i};
    }

    dsu dsu(n, e);
    forn (i, m)
        cout << dsu.ans[i] << " \n"[i + 1 == m];
}

int main()
{
#ifdef LOCAL
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    while (cin >> n)
        solve(n);
}