#include <bits/stdc++.h>
using namespace std;
const int maxn = (5e5) + 7;
const int inf = (1e9) + 7;
const long long LLinf = (1e18) + 7;
const long double eps = 1e-9;
const long long mod = 1e9 + 7;
const int maxk = 20;
int father[maxn][maxk];
int cost[maxn][maxk];
vector<pair<int, int> > wek[maxn];
pair<int, int> kraw[maxn];
bool czy[maxn];
bool vis[maxn];
int deep[maxn];
int koszt[maxn];
int res[maxn];
int naj[maxn];
int big[maxn];
int numer[maxn];
const int pot = 1 << 21;
int tree[(1 << 22) + 7];
int pre[maxn];
int post[maxn];
int t;
int val[maxn];
vector<int> wtor[maxn];
void ustaw(int x, int va) {
  x += pot;
  tree[x] = va;
  while (x > 1) {
    x /= 2;
    tree[x] = min(tree[2 * x], tree[2 * x + 1]);
  }
}
int mini() { return tree[1]; }
void dfs(int x) {
  pre[x] = t++;
  vis[x] = 1;
  for (auto s : wek[x])
    if (czy[s.second] == 1 && vis[s.first] == 0) {
      numer[s.first] = s.second;
      father[s.first][0] = x;
      cost[s.first][0] = koszt[s.second];
      deep[s.first] = deep[x] + 1;
      dfs(s.first);
    }
  post[x] = t;
}
bool CZY(int a, int b) {
  if (pre[a] <= pre[b] && post[b] <= post[a]) return 1;
  return 0;
}
inline int przedzial(int a, int b) {
  if (deep[a] > deep[b]) swap(a, b);
  int curr = 0;
  int k = maxk - 1;
  while (k > -1) {
    if (deep[b] - (1 << k) >= deep[a]) {
      curr = max(curr, cost[b][k]);
      b = father[b][k];
    }
    k--;
  }
  return curr - 1;
}
int F[maxn];
int fin(int x) {
  if (x != F[x]) F[x] = fin(F[x]);
  return F[x];
}
void uni(int a, int b) { F[fin(a)] = fin(b); }
priority_queue<pair<int, int> > q;
int roz[maxn];
void D(int x, int f) {
  roz[x] = ((int)(wtor[x]).size());
  for (auto s : wek[x]) {
    if (czy[s.second] == 1 && s.first == f) continue;
    if (czy[s.second] == 1) {
      D(s.first, x);
      if (roz[s.first] >= naj[x]) {
        big[x] = s.first;
        naj[x] = roz[s.first];
      }
      roz[x] += roz[s.first];
    } else
      roz[x]++;
  }
}
set<int> *zb[maxn];
inline void wlacz(int x) { ustaw(x, val[x]); }
inline void wylacz(int x) { ustaw(x, inf); }
void czys(int x) {
  if (x >= pot) {
    tree[x] = inf;
    return;
  }
  if (tree[2 * x] < inf) czys(2 * x);
  if (tree[2 * x + 1] < inf) czys(2 * x + 1);
  tree[x] = inf;
  return;
}
inline void czysc() {
  if (mini() < inf) czys(1);
}
void sack(int x, bool stan, int f) {
  for (auto s : wek[x])
    if (czy[s.second] == 1 && s.first != f && s.first != big[x])
      sack(s.first, 0, x);
  zb[x] = new set<int>;
  if (big[x] != 0) {
    sack(big[x], 1, x);
    zb[x] = zb[big[x]];
  }
  for (auto s : wek[x])
    if (czy[s.second] == 1 && s.first != f && s.first != big[x]) {
      for (auto v : *zb[s.first]) {
        if (!(*zb[x]).count(v)) {
          wlacz(v);
          (*zb[x]).insert(v);
        } else {
          wylacz(v);
          (*zb[x]).erase(v);
        }
      }
      delete (zb[s.first]);
    }
  for (auto s : wtor[x]) {
    if ((*zb[x]).count(s)) {
      wylacz(s);
      (*zb[x]).erase(s);
    } else {
      wlacz(s);
      (*zb[x]).insert(s);
    }
  }
  if (x != 1) res[numer[x]] = mini() - 1;
  if (stan == 0)
    for (auto s : *zb[x]) wylacz(s);
  assert(stan == 1 || mini() == inf);
}
int lca(int a, int b) {
  int k = maxk - 1;
  if (a == b) return a;
  while (k > -1) {
    if (!CZY(father[b][k], a)) b = father[b][k];
    k--;
  }
  if (!CZY(b, a)) b = father[b][0];
  return b;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < 2 * pot; i++) tree[i] = inf;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    wek[a].push_back({b, i});
    wek[b].push_back({a, i});
    kraw[i].first = a;
    kraw[i].second = b;
    koszt[i] = c;
    q.push({-c, i});
  }
  for (int i = 0; i < n + 1; i++) F[i] = i;
  while (!q.empty()) {
    int a = q.top().second;
    q.pop();
    if (fin(kraw[a].first) != fin(kraw[a].second)) {
      czy[a] = 1;
      uni(kraw[a].first, kraw[a].second);
    }
  }
  dfs(1);
  father[1][0] = 1;
  for (int k = 1; k < maxk; k++)
    for (int i = 1; i < n + 1; i++) {
      father[i][k] = father[father[i][k - 1]][k - 1];
      cost[i][k] = max(cost[i][k - 1], cost[father[i][k - 1]][k - 1]);
    }
  int num = 0;
  for (int i = 0; i < m; i++)
    if (!czy[i]) {
      int lc = lca(kraw[i].first, kraw[i].second);
      res[i] = max(przedzial(kraw[i].first, lc), przedzial(kraw[i].second, lc));
      if (lc != kraw[i].first) {
        wtor[kraw[i].first].push_back(num);
        wtor[lc].push_back(num);
      }
      val[num] = val[num + 1] = koszt[i];
      num++;
      if (lc != kraw[i].second) {
        wtor[lc].push_back(num);
        wtor[kraw[i].second].push_back(num);
      }
      num++;
    }
  D(1, 0);
  sack(1, 0, 0);
  for (int i = 0; i < m; i++)
    if (res[i] > inf - 2)
      cout << "-1 ";
    else
      cout << res[i] << " ";
  cout << endl;
  return 0;
}
