#include <bits/stdc++.h>
using namespace std;
struct edge {
  int u, v, w, xb;
  bool operator>(const edge& t) const { return w > t.w; }
} bian[200005 << 1];
int n, m, fa[200005];
vector<int> G[200005];
inline int fr(int x) { return x == fa[x] ? x : fa[x] = fr(fa[x]); }
vector<int> G2[200005];
int dep[200005], lfa[200005][20], gmax[200005][20], update[200005][20];
int fatree[200005];
priority_queue<edge, vector<edge>, greater<edge> > heap;
void dfs(int x, int f, int ceng) {
  lfa[x][0] = f;
  dep[x] = ceng;
  for (int i = 0; i < G2[x].size(); i++) {
    edge& e = bian[G2[x][i]];
    if (e.v != f) {
      gmax[e.v][0] = e.w;
      dfs(e.v, x, ceng + 1);
    } else
      fatree[x] = e.xb;
  }
}
bool intree[200005 << 1];
inline int query(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  int ans = 0;
  for (int i = 19; i >= 0; i--)
    if (dep[a] - (1 << i) >= dep[b]) {
      ans = max(ans, gmax[a][i]);
      a = lfa[a][i];
    }
  if (a == b) return ans;
  for (int i = 19; i >= 0; i--)
    if (lfa[a][i] != lfa[b][i]) {
      ans = max(ans, gmax[a][i]);
      ans = max(ans, gmax[b][i]);
      a = lfa[a][i];
      b = lfa[b][i];
    }
  return max(ans, max(gmax[a][0], gmax[b][0]));
}
int ans[200005];
inline void gx(int a, int b, int val) {
  if (dep[a] < dep[b]) swap(a, b);
  for (int i = 19; i >= 0; i--)
    if (dep[a] - (1 << i) >= dep[b]) {
      update[a][i] = min(update[a][i], val);
      a = lfa[a][i];
    }
  if (a == b) return;
  for (int i = 19; i >= 0; i--)
    if (lfa[a][i] != lfa[b][i]) {
      update[a][i] = min(update[a][i], val);
      update[b][i] = min(update[b][i], val);
      a = lfa[a][i];
      b = lfa[b][i];
    }
  update[a][0] = min(update[a][0], val);
  update[b][0] = min(update[b][0], val);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < 2 * m; i += 2) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    bian[i].u = bian[i + 1].v = u;
    bian[i].v = bian[i + 1].u = v;
    bian[i].w = bian[i + 1].w = w;
    bian[i].xb = i;
    bian[i + 1].xb = i + 1;
    G[u].push_back(i);
    G[v].push_back(i + 1);
    heap.push(bian[i]);
  }
  memset(intree, 0, sizeof(intree));
  for (int i = 1; i <= n; i++) fa[i] = i;
  while (!heap.empty()) {
    edge e = heap.top();
    heap.pop();
    if (fr(e.u) != fr(e.v)) {
      fa[fa[e.u]] = fa[e.v];
      G2[e.u].push_back(e.xb);
      G2[e.v].push_back(e.xb ^ 1);
      intree[e.xb] = intree[e.xb ^ 1] = 1;
    }
  }
  memset(dep, 0, sizeof(dep));
  memset(lfa, 0, sizeof(lfa));
  memset(gmax, 0, sizeof(gmax));
  memset(fatree, 0, sizeof(fatree));
  dfs(1, 0, 1);
  for (int exp = 1; exp < 20; exp++)
    for (int i = 1; i <= n; i++) {
      lfa[i][exp] = lfa[lfa[i][exp - 1]][exp - 1];
      gmax[i][exp] = max(gmax[i][exp - 1], gmax[lfa[i][exp - 1]][exp - 1]);
    }
  memset(update, 0x7f, sizeof(update));
  for (int i = 0; i < 2 * m; i += 2) {
    edge& e = bian[i];
    if (!intree[i]) {
      ans[i / 2 + 1] = query(e.u, e.v);
      gx(e.u, e.v, e.w);
    }
  }
  for (int e = 19; e; e--)
    for (int i = 1; i <= n; i++) {
      update[i][e - 1] = min(update[i][e - 1], update[i][e]);
      update[lfa[i][e - 1]][e - 1] =
          min(update[lfa[i][e - 1]][e - 1], update[i][e]);
    }
  for (int i = 2; i <= n; i++) ans[bian[fatree[i]].xb / 2 + 1] = update[i][0];
  for (int i = 1; i <= m; i++) {
    if (ans[i] == 0x7f7f7f7f)
      printf("-1 ");
    else
      printf("%d ", ans[i] - 1);
  }
  return 0;
}
