#include <bits/stdc++.h>
using namespace std;
struct edge {
  int u, v, w, id;
  edge(int _u = 0, int _v = 0, int _w = 0, int _id = 0)
      : u(_u), v(_v), w(_w), id(_id) {}
  bool operator<(const edge &rhs) const { return w < rhs.w; }
} e[200005];
int n, m;
map<pair<int, int>, int> maId;
void setup() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    maId[make_pair(u, v)] = maId[make_pair(v, u)] = i;
    e[i] = {u, v, w, i};
  }
}
int inMST[200005], root[200005];
vector<pair<int, int> > g[200005];
int get_root(int u) { return (root[u] == 0) ? u : root[u] = get_root(root[u]); }
void buildMST() {
  sort(e + 1, e + m + 1);
  for (int i = 1; i <= m; i++) {
    int u = e[i].u, v = e[i].v, w = e[i].w, id = e[i].id;
    int p = get_root(u);
    int q = get_root(v);
    if (p == q) continue;
    inMST[id] = 1;
    root[p] = q;
    g[u].push_back(make_pair(v, w));
    g[v].push_back(make_pair(u, w));
  }
}
int in[200005];
int h[200005], p[200005][19], wMin[200005][19];
void DFSMST(int u) {
  for (auto z : g[u]) {
    int v = z.first;
    int w = z.second;
    if (v == p[u][0]) continue;
    in[u]++;
    h[v] = h[u] + 1;
    p[v][0] = u;
    wMin[v][0] = w;
    for (int i = 1; i <= 19 - 1; i++) {
      p[v][i] = p[p[v][i - 1]][i - 1];
      wMin[v][i] = max(wMin[v][i - 1], wMin[p[v][i - 1]][i - 1]);
    }
    DFSMST(v);
  }
}
pair<int, int> getMinPath(int u, int v) {
  int ans = 0;
  if (h[u] > h[v]) swap(u, v);
  int delta = h[v] - h[u];
  for (int i = 0; i <= 19 - 1; i++)
    if ((delta >> i) & 1) {
      ans = max(ans, wMin[v][i]);
      v = p[v][i];
    }
  if (u == v) return make_pair(u, ans);
  for (int i = 19 - 1; i >= 0; i--)
    if (p[u][i] != p[v][i]) {
      ans = max({ans, wMin[u][i], wMin[v][i]});
      u = p[u][i];
      v = p[v][i];
    }
  return make_pair(p[u][0], max({ans, wMin[u][0], wMin[v][0]}));
}
int res[200005];
void solveType1() {
  for (int i = 1; i <= m; i++) {
    int u = e[i].u, v = e[i].v, id = e[i].id;
    if (inMST[id]) continue;
    res[id] = getMinPath(u, v).second - 1;
  }
}
set<pair<int, int> > s[200005];
void Merge(int u, int v) {
  if (s[u].size() < s[v].size()) swap(s[u], s[v]);
  for (auto z : s[v]) s[u].insert(z);
}
vector<pair<int, int> > delE[200005];
void prepareType2() {
  for (int i = 1; i <= m; i++) {
    int u = e[i].u, v = e[i].v, w = e[i].w, id = e[i].id;
    if (inMST[id]) continue;
    int lca = getMinPath(u, v).first;
    s[u].insert(make_pair(w, id));
    s[v].insert(make_pair(w, id));
    delE[lca].push_back(make_pair(w, id));
  }
}
void solveType2() {
  queue<int> q;
  for (int i = 1; i <= n; i++)
    if (in[i] == 0) q.push(i);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto z : delE[u]) {
      s[u].erase(z);
    }
    int v = p[u][0];
    int w = wMin[u][0];
    int id = maId[make_pair(u, v)];
    if (s[u].empty())
      res[id] = -1;
    else {
      int ww = (*s[u].begin()).first;
      res[id] = ww - 1;
    }
    Merge(v, u);
    in[v]--;
    if (in[v] == 0) q.push(v);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  setup();
  buildMST();
  DFSMST(1);
  solveType1();
  prepareType2();
  solveType2();
  for (int i = 1; i <= m; i++) cout << res[i] << ' ';
}
