require 'set'

class UnionFind
  def initialize
    @obj_to_node = {}
  end

  def add(obj)
    @obj_to_node[obj] ||= UnionFind::Node.new(obj)
  end

  def size(x)
    node = @obj_to_node[x]
    node ? node.root.size : 1
  end

  class UnionFind::Node
    def initialize(obj)
      @obj = obj
      @parent = self
      @size = 1
      @rank = 0
    end

    def root
      return self if @parent == self
      @parent = @parent.root
    end  

    attr_accessor :parent, :size, :rank
  end

  def union(x, y)
    u = (@obj_to_node[x] ||= UnionFind::Node.new(x)).root
    v = (@obj_to_node[y] ||= UnionFind::Node.new(y)).root
    return if u == v
    if u.rank > v.rank
      v.parent = u
      u.size += v.size
    elsif v.rank > u.rank
      u.parent = v
      v.size += u.size
    else
      u.parent = v
      v.rank += 1
      v.size += u.size
    end
  end

  def same_group?(x, y)
    return false unless u = @obj_to_node[x]
    return false unless v = @obj_to_node[y]
    u.root == v.root
  end

  def groups
    @obj_to_node.values.group_by(&:root).values
  end
end

class LazySegmentTree
  def initialize(arr, l = 0, r = arr.size - 1)
    @l, @r = l, r
    @lazy = nil
    if l == r
      @edge = arr[l]
      @repair_cost = Float::INFINITY
    else
      c = (l + r) / 2
      @left_child = self.class.new(arr, l, c)
      @right_child = self.class.new(arr, c + 1, r)
    end
  end

  attr_reader :repair_cost, :l, :r

  def update(l, r, x)
    if @l == @r
      @repair_cost = x if x < @repair_cost
    elsif l == @l && r == @r
      if @lazy
        @lazy = x if x < @lazy
      else
        @lazy = x
      end
    else
      force
      if r <= @left_child.r
        @left_child.update(l, r, x)
      elsif @right_child.l <= l
        @right_child.update(l, r, x)
      else
        @left_child.update(l, @left_child.r, x)
        @right_child.update(@right_child.l, r, x)
      end
    end
  end

  attr_reader :lazy, :left_child, :right_child

  def force
    return if !@lazy
    x = @lazy
    @lazy = nil
    @left_child.update(@left_child.l, @left_child.r, x)
    @right_child.update(@right_child.l, @right_child.r, x)
  end

  def force_all
    return if @l == @r
    force
    @left_child.force_all
    @right_child.force_all
  end

  def enum(&block)
    if @l == @r
      block.call(@edge, @repair_cost)
    else
      @left_child.enum(&block)
      @right_child.enum(&block)
    end
  end
end

n, m = gets.split.map(&:to_i)
edges = []
m.times do
  edges << gets.split.map(&:to_i)
end

# MST by Kruskal
undirected_tree = Hash[(1..n).map {|i| [i, {}] }]
mst_weight = 0
nontree_edges = []
uf = UnionFind.new
edges.sort_by(&:last).each do |e|
  i, j, w = e
  if uf.same_group?(i, j)
    nontree_edges << e
  else
    mst_weight += w
    uf.union(i, j)
    undirected_tree[i][j] = w
    undirected_tree[j][i] = w
  end
end

if m - nontree_edges.size != n - 1
  edges.each do
    puts -1
  end
  exit
end

# orientation
tree = Hash[(1..n).map {|i| [i, {}] }]
parent = {}
out_deg = Hash[(1..n).map {|i| [i, 0] }]
queue = [1]
reached = Set[1]
until queue.empty?
  i = queue.shift
  undirected_tree[i].each do |j, w|
    next if reached.include?(j)
    reached << j
    tree[i][j] = w
    parent[j] = i
    queue << j
    out_deg[i] += 1
  end
end

# compute subtree size
subtree_size = Hash[(1..n).map {|i| [i, 1] }]
queue = (1..n).select {|i| out_deg[i] == 0 }
paths = []
until queue.empty?
  i = queue.shift
  tree[i].each_key do |j|
    subtree_size[i] += subtree_size[j]
  end
  j = parent[i]
  if j
    out_deg[j] -= 1
    queue << j if out_deg[j] == 0
  end
end

# Heavy-Light Decomposition & Lazy Segment Trees
queue = [1]
position = {}
ord = 0
paths = []
segs = []
until queue.empty?
  i = queue.shift
  path = [i]
  loop do
    heavy = true
    tree[i].each_key.sort_by {|j| -subtree_size[j]}.each do |j|
      if heavy
        path << j
        i = j
        heavy = false
      else
        queue << j
      end
    end
    break if heavy
  end
  path.unshift parent[path[0]] if parent[path[0]]
  seg = LazySegmentTree.new(path.each_cons(2).map.to_a)
  (1..path.size-1).each do |i|
    position[path[i]] = [path, i - 1, ord, seg]
  end
  paths << path
  segs << seg
  ord += 1
end
position[1] = [paths[0], -1, 0, segs[0]]

# compute minimum cost to repair when each tree edge is removed
nontree_edges.each do |u, v, w|
  # p [u, v, w]
  path1, i, o1, seg1 = position[u]
  path2, j, o2, seg2 = position[v]
  until o1 == o2
    if o1 > o2
      seg1.update(0, i, w)
      path1, i, o1, seg1 = position[u = path1[0]]
    else
      seg2.update(0, j, w)
      path2, j, o2, seg2 = position[v = path2[0]]
    end
  end
  if i < j
    seg1.update(i + 1, j, w)
  elsif j < i
    seg2.update(j + 1, i, w)
  end
end

repair_cost = {}
segs.each do |seg|
  seg.force_all
  seg.enum do |e, c|
    # p [e, c]
    repair_cost[e.sort] = c
  end
end

edges.each do |u, v, w|
  if repair_cost[[u, v].sort]
    repaired = mst_weight - w + repair_cost[[u, v].sort]
    puts repaired == Float::INFINITY ? -1 : repaired
  else
    puts mst_weight
  end
end



Measured memory_bytes: 158752