#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int res = 0;
  int neg;
  while (true) {
    char ch = getchar();
    if (ch >= '0' && ch <= '9' || ch == '-') {
      if (ch == '-')
        neg = -1;
      else
        neg = 1, res = ch - '0';
      break;
    }
  }
  while (true) {
    char ch = getchar();
    if (ch >= '0' && ch <= '9')
      res *= 10, res += ch - '0';
    else
      break;
  }
  return res * neg;
}
const int maxn = 200020;
const int maxm = 1000020;
const int MOd = 1e9 + 7;
const int inf = 1e9 + 147;
int a, b, dad[maxn];
multiset<pair<int, int> > st[maxn];
vector<int> w[maxn];
vector<pair<int, pair<int, int> > > v;
int val[maxn], ans[maxn];
bool var[maxn], H[maxn];
int heavy[maxn];
int der[maxn], lca[maxn][20];
int dfs(int n, int back, int s) {
  int ret = 1;
  lca[n][0] = back;
  der[n] = s;
  for (int i = 1; (1 << i) <= s; i++) lca[n][i] = lca[lca[n][i - 1]][i - 1];
  int h = 0, maxi = 0;
  for (int i = 0; i < w[n].size(); i++) {
    if (w[n][i] != back) {
      int t = dfs(w[n][i], n, s + 1);
      if (maxi < t) {
        maxi = t;
        h = i;
      }
      ret += t;
    }
  }
  if (w[n].size() != 1) {
    H[w[n][h]] = 1;
    swap(w[n][h], w[n][0]);
  }
  return ret;
}
int beg[maxn], en[maxn], cnt;
void dfss(int n, int back, int last) {
  if (H[n] && !H[back]) last = n;
  if (H[n]) heavy[n] = last;
  beg[n] = ++cnt;
  for (int i = 0; i < w[n].size(); i++)
    if (w[n][i] != back) dfss(w[n][i], n, last);
  en[n] = cnt;
}
int lc(int x, int y) {
  if (der[x] < der[y]) swap(x, y);
  for (int k = 18; k >= 0; k--)
    if ((1 << k) <= der[x] && der[lca[x][k]] > der[y]) x = lca[x][k];
  if (der[x] > der[y]) x = lca[x][0];
  for (int k = 18; k >= 0; k--)
    if ((1 << k) <= der[x] && lca[x][k] != lca[y][k])
      x = lca[x][k], y = lca[y][k];
  if (x != y) x = lca[x][0];
  return x;
}
int find(int n) {
  if (dad[n] == n) return n;
  return dad[n] = find(dad[n]);
}
bool comp(const pair<int, pair<int, int> > &a,
          const pair<int, pair<int, int> > &b) {
  return val[a.first] < val[b.first];
}
int segment[maxn * 3][2], ar[maxn], N, lazy[maxn * 3][2];
void push(int k, int tp) {
  if ((tp == 0 && lazy[k][tp] < inf) || (tp == 1 && lazy[k][tp] > 0)) {
    if (k < N) {
      if (tp == 0) {
        lazy[k + k][tp] = min(lazy[k + k][tp], (lazy[k][tp]));
        lazy[k + k + 1][tp] = min(lazy[k + k + 1][tp], (lazy[k][tp]));
      } else if (tp == 1) {
        lazy[k + k][tp] = max(lazy[k + k][tp], (lazy[k][tp]));
        lazy[k + k + 1][tp] = max(lazy[k + k + 1][tp], (lazy[k][tp]));
      }
    }
    if (tp == 0)
      segment[k][tp] = min(segment[k][tp], (lazy[k][tp])), lazy[k][tp] = inf;
    else
      segment[k][tp] = max(segment[k][tp], (lazy[k][tp])), lazy[k][tp] = 0;
  }
}
void up(int k, int b, int s, int x, int y, int t, int tp) {
  push(k, tp);
  if (b > y || x > s) return;
  if (x <= b && s <= y) {
    if (tp == 0) lazy[k][tp] = min(lazy[k][tp], (t));
    if (tp == 1) lazy[k][tp] = max(lazy[k][tp], (t));
    push(k, tp);
    return;
  }
  up(k + k, b, (b + s) / 2, x, y, t, tp);
  up(k + k + 1, (b + s) / 2 + 1, s, x, y, t, tp);
  if (tp == 0)
    segment[k][tp] = min(segment[k + k][tp], segment[k + k + 1][tp]);
  else
    segment[k][tp] = max(segment[k + k][tp], segment[k + k + 1][tp]);
}
int get(int k, int b, int s, int x, int y, int tp) {
  push(k, tp);
  if (b > y || x > s) {
    if (tp == 0) return inf;
    if (tp == 1) return 0;
  }
  if (x <= b && s <= y) return segment[k][tp];
  if (tp == 0)
    return min(get(k + k, b, (b + s) / 2, x, y, tp),
               get(k + k + 1, (b + s) / 2 + 1, s, x, y, tp));
  if (tp == 1)
    return max(get(k + k, b, (b + s) / 2, x, y, tp),
               get(k + k + 1, (b + s) / 2 + 1, s, x, y, tp));
}
void add(int x, int y, int t, int tp) {
  if (der[x] < der[y]) assert(0);
  while (der[x] > der[y]) {
    if (H[x]) {
      if (der[heavy[x]] > der[y]) {
        up(1, 1, N, beg[heavy[x]], beg[x], t, tp);
        x = lca[heavy[x]][0];
      } else {
        up(1, 1, N, beg[y] + 1, beg[x], t, tp);
        break;
      }
    } else {
      if (tp == 0) ar[x] = min(ar[x], (t));
      if (tp == 1) ar[x] = max(ar[x], (t));
      x = lca[x][0];
    }
  }
}
int look(int x, int y, int tp) {
  if (der[x] < der[y]) assert(0);
  int ret = inf;
  if (tp == 1) ret = 0;
  while (der[x] > der[y]) {
    if (H[x]) {
      if (der[heavy[x]] > der[y]) {
        if (tp == 0) ret = min(ret, (get(1, 1, N, beg[heavy[x]], beg[x], tp)));
        if (tp == 1) ret = max(ret, (get(1, 1, N, beg[heavy[x]], beg[x], tp)));
        x = lca[heavy[x]][0];
      } else {
        if (tp == 0) ret = min(ret, (get(1, 1, N, beg[y] + 1, beg[x], tp)));
        if (tp == 1) ret = max(ret, (get(1, 1, N, beg[y] + 1, beg[x], tp)));
        break;
      }
    } else {
      if (tp == 0) ret = min(ret, (ar[x]));
      if (tp == 1) ret = max(ret, (ar[x]));
      x = lca[x][0];
    }
  }
  return ret;
}
int main() {
  scanf("%d %d", &a, &b);
  for (int i = 1; i <= a; i++) dad[i] = i;
  for (int i = 1, j, k, t; i <= b; i++) {
    scanf("%d %d %d", &j, &k, &t);
    val[i] = t;
    v.push_back(pair<int, pair<int, int> >(i, pair<int, int>(j, k)));
  }
  N = 1;
  while (N < a) N <<= 1;
  for (int i = 1; i <= a; i++) ar[i] = inf;
  for (int i = 1; i <= N + N; i++) segment[i][0] = lazy[i][0] = inf;
  sort(v.begin(), v.end(), comp);
  for (int i = 0; i < v.size(); i++) {
    int x = find(v[i].second.first);
    int y = find(v[i].second.second);
    if (x == y) continue;
    dad[x] = y;
    var[v[i].first] = 1;
    w[v[i].second.second].push_back(v[i].second.first);
    w[v[i].second.first].push_back(v[i].second.second);
  }
  dfs(1, 0, 0);
  dfss(1, 0, 0);
  for (int i = 0; i < v.size(); i++) {
    int n = v[i].first;
    int x = v[i].second.first;
    int y = v[i].second.second;
    int l = lc(x, y);
    if (!var[n]) {
      add(x, l, val[n], 0);
      add(y, l, val[n], 0);
    }
  }
  for (int i = 0; i < v.size(); i++) {
    int n = v[i].first;
    int x = v[i].second.first;
    int y = v[i].second.second;
    int l = lc(x, y);
    if (var[n]) ans[v[i].first] = min(look(x, l, 0), look(y, l, 0));
  }
  for (int i = 1; i <= a; i++) ar[i] = 0;
  for (int i = 0; i < v.size(); i++) {
    int n = v[i].first;
    int x = v[i].second.first;
    int y = v[i].second.second;
    int l = lc(x, y);
    if (var[n]) {
      add(x, l, val[n], 1);
      add(y, l, val[n], 1);
    }
  }
  for (int i = 0; i < v.size(); i++) {
    int n = v[i].first;
    int x = v[i].second.first;
    int y = v[i].second.second;
    int l = lc(x, y);
    if (!var[n]) ans[v[i].first] = max(look(x, l, 1), look(y, l, 1));
  }
  for (int i = 1; i <= b; i++) {
    if (ans[i] == inf) ans[i] = 0;
    printf("%d ", ans[i] - 1);
  }
  printf("\n");
  return 0;
}
