#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
struct edge {
  int u, v;
  int value;
  int index;
};
bool operator<(const edge A, const edge B) {
  if (A.value != B.value) return A.value < B.value;
  return A.index < B.index;
}
int n, m;
edge allEdges[MAXN * 10];
struct dsu {
  vector<int> component[MAXN];
  vector<pair<int, int>> parent[MAXN];
  void Init(int n) {
    for (int i = 1; i <= n; i++) {
      this->component[i] = {i};
      this->parent[i] = {{i, -1}};
    }
  }
  int Find(int x) {
    if (parent[x].back().first == x) return x;
    return Find(parent[x].back().first);
  }
  bool areConnected(int u, int v) {
    u = this->Find(u);
    v = this->Find(v);
    if (u == v) return true;
    return false;
  }
  int state(int x, int value) {
    int output = -1;
    for (int i = 0; i < this->parent[x].size(); i++) {
      if (allEdges[this->parent[x][i].second].value >= value) break;
      output = this->parent[x][i].first;
    }
    return output;
  }
  void Union(int u, int v, int index) {
    u = this->Find(u);
    v = this->Find(v);
    if (u == v) return;
    if (this->component[u].size() < this->component[v].size()) swap(u, v);
    for (int x : this->component[v]) {
      this->parent[x].push_back({u, index});
      this->component[u].push_back(x);
    }
  }
};
dsu *T = new dsu();
int answer[MAXN * 10];
vector<edge> graph[MAXN];
set<pair<int, int>> inMST;
void solve1() {
  T->Init(n);
  memset(answer, -1, sizeof(answer));
  for (int i = 0; i < m; i++) {
    int u = allEdges[i].u;
    int v = allEdges[i].v;
    int value = allEdges[i].value;
    if (T->areConnected(u, v) == true) {
      int l, r, mid;
      l = 0;
      r = 1e9;
      while (l + 1 < r) {
        mid = (l + r) / 2;
        if (T->state(u, mid) != T->state(v, mid))
          l = mid;
        else
          r = mid - 1;
      }
      if (T->state(u, r) != T->state(v, r))
        answer[allEdges[i].index] = r;
      else
        answer[allEdges[i].index] = l;
    } else {
      inMST.insert({u, v});
      inMST.insert({v, u});
      T->Union(u, v, i);
    }
  }
}
int sparseParent[25][MAXN];
int depth[MAXN], parent[MAXN];
void DFSInit(int x, int last, int level) {
  depth[x] = level;
  parent[x] = last;
  for (edge e : graph[x]) {
    if (e.v == last) continue;
    DFSInit(e.v, x, level + 1);
  }
}
int biggest[MAXN];
map<int, int> mp[MAXN];
int findLCA(int u, int v) {
  if (depth[u] > depth[v]) swap(u, v);
  for (int i = 19; i >= 0; i--) {
    if (sparseParent[i][v] != -1 && depth[sparseParent[i][v]] >= depth[u])
      v = sparseParent[i][v];
  }
  if (u == v) return u;
  for (int i = 19; i >= 0; i--) {
    if (sparseParent[i][u] != sparseParent[i][v]) {
      u = sparseParent[i][u];
      v = sparseParent[i][v];
    }
  }
  return parent[u];
}
vector<int> toRemove[MAXN];
void DFS(int x, int last, int lastEdge) {
  biggest[x] = x;
  for (edge e : graph[x]) {
    if (e.v != last && inMST.count({e.u, e.v}) == true) {
      DFS(e.v, x, e.index);
      if (mp[biggest[e.v]].size() > mp[biggest[x]].size())
        biggest[x] = biggest[e.v];
    }
  }
  for (edge e : graph[x]) {
    if (e.v != last && inMST.count({e.u, e.v}) == true &&
        biggest[e.v] != biggest[x]) {
      for (pair<int, int> item : mp[biggest[e.v]])
        mp[biggest[x]][item.first] += item.second;
      mp[biggest[e.v]].clear();
    }
  }
  for (edge e : graph[x]) {
    if (e.v != last && inMST.count({e.u, e.v}) == false &&
        findLCA(x, e.v) != x) {
      toRemove[findLCA(x, e.v)].push_back(e.value);
      mp[biggest[x]][e.value]++;
    }
  }
  for (int val : toRemove[x]) {
    auto it = mp[biggest[x]].find(val);
    it->second--;
    if (it->second == 0) mp[biggest[x]].erase(it);
  }
  if (lastEdge != -1) {
    if (mp[biggest[x]].empty() == true)
      answer[lastEdge] = 1e9;
    else
      answer[lastEdge] = mp[biggest[x]].begin()->first;
  }
}
void solve2() {
  for (edge e : allEdges) {
    if (inMST.count({e.u, e.v}) == true) {
      graph[e.u].push_back(e);
      swap(e.u, e.v);
      graph[e.u].push_back(e);
    }
  }
  DFSInit(1, -1, 1);
  for (int x = 1; x <= n; x++) {
    sparseParent[0][x] = parent[x];
  }
  for (int i = 1; i < 20; i++) {
    for (int x = 1; x <= n; x++) {
      if (sparseParent[i - 1][x] == -1)
        sparseParent[i][x] = -1;
      else
        sparseParent[i][x] = sparseParent[i - 1][sparseParent[i - 1][x]];
    }
  }
  for (edge e : allEdges) {
    if (inMST.count({e.u, e.v}) == false) {
      graph[e.u].push_back(e);
      swap(e.u, e.v);
      graph[e.u].push_back(e);
    }
  }
  DFS(1, -1, -1);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> allEdges[i].u >> allEdges[i].v >> allEdges[i].value;
    allEdges[i].index = i;
  }
  sort(allEdges, allEdges + m);
  solve1();
  solve2();
  for (int i = 0; i < m; i++) {
    cout << answer[i] << '\n';
  }
}
