{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64, Int16)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (foldl', sort, foldl1')
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }
data EdgeTo = EdgeTo !Int !Int

instance Eq Edge where
  (==) = (==) `on` edgeW
instance Ord Edge where
  compare = compare `on` edgeW

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sort es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (EdgeTo u w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter (\(EdgeTo u _) -> u /= p) $ adjacents ! u
        dfs 0 0 (EdgeTo 1 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [EdgeTo]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, EdgeTo v w)

    skips u i = (ps ! ii, ms ! ii) where ii = (u, i)

    ps = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        p <- read (v, i-1)
        pp <- read (p, i-1)
        write (v, i) pp
      return a

    ms = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        let p = ps ! (v, i-1)
        w <- read (v, i-1)
        ww <- read (p, i-1)
        write (v, i) $ max w ww
      return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max (max w3 w4) w''
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', w') = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, w) i = (v, max w w') where (v, w') = skips u i
            bits v = filter (testBit v) ix

        (v', u'', w'') = foldl' step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u' then (v', u', max w (max w1 w2)) else a
              where
                (v', w1) = skips v i
                (u', w2) = skips u i

        w3 = vertexWeight (vs ! v')
        w4 = vertexWeight (vs ! u'')

  putStr . unlines . map show $ map calc es

{-# LANGUAGE FlexibleContexts #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.Bits (testBit)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.List (foldl', sortBy)
import Data.Maybe (fromJust)
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }
data EdgeTo = EdgeTo !Int !Int

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sortBy (compare `on` edgeW) es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (EdgeTo u w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter (\(EdgeTo u _) -> u /= p) $ adjacents ! u
        dfs 0 0 (EdgeTo 1 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [EdgeTo]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, EdgeTo v w)

    skips u i = (ps ! ii, ms ! ii)
      where
        ii = (u, i)

        ps = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            p <- read (v, i-1)
            pp <- read (p, i-1)
            write (v, i) pp
          return a

        ms = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            let p = ps ! (v, i-1)
            w <- read (v, i-1)
            ww <- read (p, i-1)
            write (v, i) $ max w ww
          return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = mu
      | otherwise = m
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', mu) = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, m) i = (v, max m m') where (v, m') = skips u i
            bits v = filter (testBit v) ix

        m = max m' $ max (vertexWeight $ vs ! v') (vertexWeight $ vs ! u'')
          where
            (v', u'', m') = foldl' step (v, u', mu) ix
              where
                step a@(v, u, m) i =
                    if v' /= u' then (v', u', max m (max mv mu)) else a
                  where
                    (v', mv) = skips v i
                    (u', mu) = skips u i

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64, Int16)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (foldl', sort, foldl1')
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }

instance Eq Edge where
  (==) = (==) `on` edgeW
instance Ord Edge where
  compare = compare `on` edgeW

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sort es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (u, w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter ((/= p) . fst) $ adjacents ! u
        dfs 0 0 (1, 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [(Int, Int)]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, (v, w))

    skips u i = (ps ! ii, ms ! ii) where ii = (u, i)

    ps = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        p <- read (v, i-1)
        pp <- read (p, i-1)
        write (v, i) pp
      return a

    ms = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        let p = ps ! (v, i-1)
        w <- read (v, i-1)
        ww <- read (p, i-1)
        write (v, i) $ max w ww
      return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max (max w3 w4) w''
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', w') = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, w) i = (v, max w w') where (v, w') = skips u i
            bits v = filter (testBit v) ix

        (v', u'', w'') = foldl' step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u' then (v', u', max w (max w1 w2)) else a
              where
                (v', w1) = skips v i
                (u', w2) = skips u i

        w3 = vertexWeight (vs ! v')
        w4 = vertexWeight (vs ! u'')

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<*>), (<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
-- import Data.Array (Array, accumArray, listArray, (!))
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (elems, listArray, accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray,runSTArray, STUArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64, Int16)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (foldl', sort, foldl1')
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import Data.Function (on)
import System.Random (newStdGen, random)
import Prelude hiding (getLine,getContents, words, lines)

data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }

instance Eq Edge where
  (==) = (==) `on` edgeW
instance Ord Edge where
  compare = compare `on` edgeW

readInts = map (fst . fromJust . readInt) . words

main = do
  [n, _] <- readInts `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . readInts) . lines) `fmap` getContents

  let
    mst = runST $ do
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sort es

    l = foldl1' (+) $ map (\(Edge _ _ w) -> fromIntegral w :: Int64) mst

    adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [(Int, Int)]
      where
          swap (Edge u v w) = Edge v u w
          shift (Edge u v w) = (u, (v, w))

    parents = runSTArray $ do
      a <- newArray (1, n) (0, 0) :: ST s (STArray s Int (Int, Int))
      let dfs p (u, w) = do
          writeArray a u (p, w)
          mapM_ (dfs u) . filter ((/= p) . fst) $ adjacents ! u
      dfs 0 (1, 0)
      return a

    depths = runSTUArray $ do
      a <- newArray (1, n) 0 :: ST s (STUArray s Int Int)
      let dfs p d u = do
          writeArray a u d
          mapM_ (dfs u (d+1)) $ filter (/= p) $ map fst $ adjacents ! u
      dfs 0 0 1
      return a

    parents' = runSTArray $ do
        a <- newArray ((0, 0), (n, k)) (0, 0)

        forM_ [0..k] $ \i -> writeArray a (0, i) (0, 0)
        forM_ [1..n] $ \v -> writeArray a (v, 0) (parents ! v)

        forM_ [1..k] $ \i ->
          forM_ [1..n] $ \v -> do
            (v', w) <- readArray a (v, i-1)
            (!v'', w') <- readArray a (v', i-1)
            let !ww = max w w'
            writeArray a (v, i) (v'', ww)

        return a

    calc (Edge u v w)
        | fst (parents ! u) == v || fst (parents ! v) == u = l
        | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]
    ix = reverse [0..k]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max (max w3 w4) w''
      where
        (dv, du) = (depths ! v, depths ! u)

        (u', w') = foldl' step (u, 0) ix
          where
            step :: (Int, Int) -> Int -> (Int, Int)
            step a@(u, w) i = if depths ! u - dv >= 2^i then (v, max w w') else a
              where
                (v, w') = parents' ! (u, i)

        (v', u'', w'') = foldl' step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u'
                   then (v', u', max w (max w1 w2))
                   else a
              where
                (v', w1) = parents' ! (v, i)
                (u', w2) = parents' ! (u, i)

        (_, w3) = parents ! v'
        (_, w4) = parents ! u''

  putStr . unlines . map show $ map calc es

{-# LANGUAGE FlexibleContexts #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.Bits (testBit)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.List (foldl', sortBy)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Prelude hiding (getLine,getContents, words, lines)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }
data EdgeTo = EdgeTo !Int !Int

readInt' = fst . fromJust . readInt

newSets n = do
  ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
  rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)
  return (ps, rs)

findSet s@(ps, _) u = do
  p <- readArray ps u
  if p == u
    then return p
    else do
      v <- findSet s p
      writeArray ps u v
      return v

unionSets s@(ps, rs) a b = do
  pa <- findSet s a
  pb <- findSet s b

  when (pa /= pb) $ do
    ra <- readArray rs a
    rb <- readArray rs b

    let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

    writeArray ps b' a'
    when (ra == rb) $ writeArray rs a' (ra' + 1)

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        s@(ps, rs) <- newSets n

        let
          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet s u
            pv <- findSet s v

            if pu /= pv
              then do
                unionSets s pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sortBy (comparing edgeW) es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (EdgeTo u w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter (\(EdgeTo u _) -> u /= p) $ adjacents ! u
        dfs 0 0 (EdgeTo 1 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [EdgeTo]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, EdgeTo v w)

    skips u i = (ps ! ii, ms ! ii)
      where
        ii = (u, i)

        ps = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            p <- read (v, i-1)
            pp <- read (p, i-1)
            write (v, i) pp
          return a

        ms = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            let p = ps ! (v, i-1)
            w <- read (v, i-1)
            ww <- read (p, i-1)
            write (v, i) $ max w ww
          return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = mu
      | otherwise = m
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', mu) = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, m) i = (v, max m m') where (v, m') = skips u i
            bits v = filter (testBit v) ix

        m = max m' $ max (vertexWeight $ vs ! v') (vertexWeight $ vs ! u'')
          where
            (v', u'', m') = foldl' step (v, u', mu) ix
              where
                step a@(v, u, m) i =
                    if v' /= u' then (v', u', max m (max mv mu)) else a
                  where
                    (v', mv) = skips v i
                    (u', mu) = skips u i

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (elems, listArray, accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray,runSTArray, STUArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64, Int16)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (foldl', sort, foldl1')
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Vertex = Vertex { vertexDepth :: Int, vertexParent :: Int, vertexWeight :: Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }

instance Eq Edge where
  (==) = (==) `on` edgeW
instance Ord Edge where
  compare = compare `on` edgeW

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sort es

    l = foldl1' (+) $ map (\(Edge _ _ w) -> fromIntegral w :: Int64) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (u, w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter ((/= p) . fst) $ adjacents ! u
        dfs 0 0 (1, 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [(Int, Int)]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, (v, w))

    skips u i = (ps ! ii, ms ! ii) where ii = (u, i)

    ps = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        p <- read (v, i-1)
        pp <- read (p, i-1)
        write (v, i) pp
      return a

    ms = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        let p = ps ! (v, i-1)
        w <- read (v, i-1)
        ww <- read (p, i-1)
        write (v, i) $ max w ww
      return a

    calc (Edge u v w)
        | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
        | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max (max w3 w4) w''
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', w') = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, w) i = (v, max w w') where (v, w') = skips u i
            bits v = filter (testBit v) ix

        (v', u'', w'') = foldl' step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u' then (v', u', max w (max w1 w2)) else a
              where
                (v', w1) = skips v i
                (u', w2) = skips u i

        w3 = vertexWeight (vs ! v')
        w4 = vertexWeight (vs ! u'')

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64, Int16)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (foldl', sortBy, foldl1')
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }
data EdgeTo = EdgeTo !Int !Int

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sortBy (compare `on` edgeW) es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (EdgeTo u w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter (\(EdgeTo u _) -> u /= p) $ adjacents ! u
        dfs 0 0 (EdgeTo 1 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [EdgeTo]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, EdgeTo v w)

    skips u i = (ps ! ii, ms ! ii) where ii = (u, i)

    ps = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        p <- read (v, i-1)
        pp <- read (p, i-1)
        write (v, i) pp
      return a

    ms = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        let p = ps ! (v, i-1)
        w <- read (v, i-1)
        ww <- read (p, i-1)
        write (v, i) $ max w ww
      return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max (max w3 w4) w''
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', w') = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, w) i = (v, max w w') where (v, w') = skips u i
            bits v = filter (testBit v) ix

        (v', u'', w'') = foldl' step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u' then (v', u', max w (max w1 w2)) else a
              where
                (v', w1) = skips v i
                (u', w2) = skips u i

        w3 = vertexWeight (vs ! v')
        w4 = vertexWeight (vs ! u'')

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.Maybe (fromJust)
import Data.List (foldl', sortBy)
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }
data EdgeTo = EdgeTo !Int !Int

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sortBy (compare `on` edgeW) es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (EdgeTo u w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter (\(EdgeTo u _) -> u /= p) $ adjacents ! u
        dfs 0 0 (EdgeTo 1 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [EdgeTo]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, EdgeTo v w)

    skips u i = (ps ! ii, ms ! ii)
      where
        ii = (u, i)

        ps = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            p <- read (v, i-1)
            pp <- read (p, i-1)
            write (v, i) pp
          return a

        ms = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            let p = ps ! (v, i-1)
            w <- read (v, i-1)
            ww <- read (p, i-1)
            write (v, i) $ max w ww
          return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = mu
      | otherwise = m
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', mu) = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, m) i = (v, max m m') where (v, m') = skips u i
            bits v = filter (testBit v) ix

        m = max m' $ max (vertexWeight $ vs ! v') (vertexWeight $ vs ! u'')
          where
            (v', u'', m') = foldl' step (v, u', mu) ix
              where
                step a@(v, u, m) i =
                    if v' /= u' then (v', u', max m (max mv mu)) else a
                  where
                    (v', mv) = skips v i
                    (u', mu) = skips u i

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (elems, listArray, accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray,runSTArray, STUArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64, Int16)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (foldl', sort, foldl1')
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Vertex = Vertex { vertexDepth :: Int, vertexParent :: Int, vertexWeight :: Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }

instance Eq Edge where
  (==) = (==) `on` edgeW
instance Ord Edge where
  compare = compare `on` edgeW

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sort es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (u, w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter ((/= p) . fst) $ adjacents ! u
        dfs 0 0 (1, 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [(Int, Int)]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, (v, w))

    skips u i = (ps ! ii, ms ! ii) where ii = (u, i)

    ps = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        p <- read (v, i-1)
        pp <- read (p, i-1)
        write (v, i) pp
      return a

    ms = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        let p = ps ! (v, i-1)
        w <- read (v, i-1)
        ww <- read (p, i-1)
        write (v, i) $ max w ww
      return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max (max w3 w4) w''
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', w') = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, w) i = (v, max w w') where (v, w') = skips u i
            bits v = filter (testBit v) ix

        (v', u'', w'') = foldl' step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u' then (v', u', max w (max w1 w2)) else a
              where
                (v', w1) = skips v i
                (u', w2) = skips u i

        w3 = vertexWeight (vs ! v')
        w4 = vertexWeight (vs ! u'')

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (elems, listArray, accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray, freeze)
import Data.Array.ST.Safe (STArray,runSTArray, STUArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64, Int16)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (foldl', sort, foldl1')
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import Data.Function (on)
import System.Random (newStdGen, random)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Node = Node { nodeParent :: !Int, nodeMin :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }

instance Eq Edge where
  (==) = (==) `on` edgeW
instance Ord Edge where
  compare = compare `on` edgeW

{-# INLINE readInt' #-}
readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sort es

    l = foldl1' (+) $ map (\(Edge _ _ w) -> fromIntegral w :: Int64) mst

    adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [(Int, Int)]
      where
          swap (Edge u v w) = Edge v u w
          shift (Edge u v w) = (u, (v, w))

    parents = runSTArray $ do
      a <- newArray (1, n) (0, 0) :: ST s (STArray s Int (Int, Int))
      let dfs p (u, w) = do
          writeArray a u (p, w)
          mapM_ (dfs u) . filter ((/= p) . fst) $ adjacents ! u
      dfs 0 (1, 0)
      return a

    depths = runSTUArray $ do
      a <- newArray (1, n) 0 :: ST s (STUArray s Int Int)
      let dfs p d u = do
          writeArray a u d
          mapM_ (dfs u (d+1)) $ filter (/= p) $ map fst $ adjacents ! u
      dfs 0 0 1
      return a

    r :: (UArray (Int, Int) Int, UArray (Int, Int) Int)
    r@(jps, jms) = runST $ do
        ps <- newArray ((0, 0), (n, k)) 0 :: ST s (STUArray s (Int, Int) Int)
        ms <- newArray ((0, 0), (n, k)) 0 :: ST s (STUArray s (Int, Int) Int)

        forM_ [0..k] $ \i -> do
          writeArray ps (0, i) 0
          writeArray ms (0, i) 0

        forM_ [1..n] $ \v -> do
          let (p, w) = parents ! v
          writeArray ps (v, 0) p
          writeArray ms (v, 0) w

        forM_ [1..k] $ \i ->
          forM_ [1..n] $ \v -> do
            v' <- readArray ps (v, i-1)
            v'' <- readArray ps (v', i-1)
            w <- readArray ms (v, i-1)
            w' <- readArray ms (v', i-1)
            writeArray ps (v, i) v''
            writeArray ms (v, i) $ max w w'

        ps' <- freeze ps
        ms' <- freeze ms
        return (ps', ms')

    calc (Edge u v w)
        | fst (parents ! u) == v || fst (parents ! v) == u = l
        | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]
    ix = reverse [0..k]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max (max w3 w4) w''
      where
        (dv, du) = (depths ! v, depths ! u)

        (u', w') = foldl' step (u, 0) $ bits (du - dv)
          where
            step :: (Int, Int) -> Int -> (Int, Int)
            step a@(u, w) i = (v, max w w') where
              v = jps ! (u, i)
              w' = jms ! (u, i)

            bits v = filter (testBit v) $ ix

        (v', u'', w'') = foldl' step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u'
                   then (v', u', max w (max w1 w2))
                   else a
              where
                v' = jps ! (v, i)
                w1 = jms ! (v, i)
                u' = jps ! (u, i)
                w2 = jms ! (u, i)

        (_, w3) = parents ! v'
        (_, w4) = parents ! u''

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.Bits (testBit)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.List (foldl', sortBy)
import Data.Maybe (fromJust)
import Data.Function (on)
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import System.Random
import Prelude hiding (getLine,getContents, words, lines)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }
data EdgeTo = EdgeTo !Int !Int

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents
  gen <- getStdGen

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        gr <- newSTRef gen

        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              g <- readSTRef gr
              let (b, g') = random g
              writeSTRef gr g'

              if b
                 then writeArray ps pa pb
                 else writeArray ps pb pa

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sortBy (compare `on` edgeW) es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (EdgeTo u w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter (\(EdgeTo u _) -> u /= p) $ adjacents ! u
        dfs 0 0 (EdgeTo 1 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [EdgeTo]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, EdgeTo v w)

    skips u i = (ps ! ii, ms ! ii)
      where
        ii = (u, i)

        ps = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            p <- read (v, i-1)
            pp <- read (p, i-1)
            write (v, i) pp
          return a

        ms = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            let p = ps ! (v, i-1)
            w <- read (v, i-1)
            ww <- read (p, i-1)
            write (v, i) $ max w ww
          return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = mu
      | otherwise = m
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', mu) = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, m) i = (v, max m m') where (v, m') = skips u i
            bits v = filter (testBit v) ix

        m = max m' $ max (vertexWeight $ vs ! v') (vertexWeight $ vs ! u'')
          where
            (v', u'', m') = foldl' step (v, u', mu) ix
              where
                step a@(v, u, m) i =
                    if v' /= u' then (v', u', max m (max mv mu)) else a
                  where
                    (v', mv) = skips v i
                    (u', mu) = skips u i

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O3 -funbox-strict-fields #-}

import Control.Applicative ((<*>), (<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
-- import Data.Array (Array, accumArray, listArray, (!))
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (elems, listArray, accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray,runSTArray, STUArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64, Int16)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (foldl', sort, foldl1')
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import Data.Function (on)
import System.Random (newStdGen, random)
import Prelude hiding (getLine,getContents, words, lines)

data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }

instance Eq Edge where
  (==) = (==) `on` edgeW
instance Ord Edge where
  compare = compare `on` edgeW

readInts = map (fst . fromJust . readInt) . words

main = do
  [n, _] <- readInts `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . readInts) . lines) `fmap` getContents

  let
    mst = runST $ do
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sort es

    l = foldl1' (+) $ map (\(Edge _ _ w) -> fromIntegral w :: Int64) mst

    adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [(Int, Int)]
      where
          swap (Edge u v w) = Edge v u w
          shift (Edge u v w) = (u, (v, w))

    parents = runSTArray $ do
      a <- newArray (1, n) (0, 0) :: ST s (STArray s Int (Int, Int))
      let dfs p (u, w) = do
          writeArray a u (p, w)
          mapM_ (dfs u) . filter ((/= p) . fst) $ adjacents ! u
      dfs 0 (1, 0)
      return a

    depths = runSTUArray $ do
      a <- newArray (1, n) 0 :: ST s (STUArray s Int Int)
      let dfs p d u = do
          writeArray a u d
          mapM_ (dfs u (d+1)) $ filter (/= p) $ map fst $ adjacents ! u
      dfs 0 0 1
      return a

    parents' = runSTArray $ do
        a <- newArray ((0, 0), (n, k)) (0, 0)

        forM_ [0..k] $ \i -> writeArray a (0, i) (0, 0)
        forM_ [1..n] $ \v -> writeArray a (v, 0) (parents ! v)

        forM_ [1..k] $ \i ->
          forM_ [1..n] $ \v -> do
            (v', w) <- readArray a (v, i-1)
            (!v'', w') <- readArray a (v', i-1)
            let !ww = max w w'
            writeArray a (v, i) (v'', ww)

        return a

    calc (Edge u v w)
        | fst (parents ! u) == v || fst (parents ! v) == u = l
        | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]
    ix = reverse [0..k]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max (max w3 w4) w''
      where
        (dv, du) = (depths ! v, depths ! u)

        (u', w') = foldl' step (u, 0) ix
          where
            step :: (Int, Int) -> Int -> (Int, Int)
            step a@(u, w) i = if depths ! u - dv >= 2^i then (v, max w w') else a
              where
                (v, w') = parents' ! (u, i)

        (v', u'', w'') = foldl' step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u'
                   then (v', u', max w (max w1 w2))
                   else a
              where
                (v', w1) = parents' ! (v, i)
                (u', w2) = parents' ! (u, i)

        (_, w3) = parents ! v'
        (_, w4) = parents ! u''

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.Maybe (fromJust)
import Data.List (foldl', sortBy)
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }
data EdgeTo = EdgeTo !Int !Int

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sortBy (compare `on` edgeW) es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (EdgeTo u w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter (\(EdgeTo u _) -> u /= p) $ adjacents ! u
        dfs 0 0 (EdgeTo 1 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [EdgeTo]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, EdgeTo v w)

    skips u i = (ps ! ii, ms ! ii)
      where
        ii = (u, i)

        ps = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            p <- read (v, i-1)
            pp <- read (p, i-1)
            write (v, i) pp
          return a

        ms = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            let p = ps ! (v, i-1)
            w <- read (v, i-1)
            ww <- read (p, i-1)
            write (v, i) $ max w ww
          return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max (max w3 w4) w''
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', w') = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, w) i = (v, max w w') where (v, w') = skips u i
            bits v = filter (testBit v) ix

        (v', u'', w'') = foldl' step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u' then (v', u', max w (max w1 w2)) else a
              where
                (v', w1) = skips v i
                (u', w2) = skips u i

        w3 = vertexWeight (vs ! v')
        w4 = vertexWeight (vs ! u'')

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.Bits (testBit)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.List (foldl', sortBy)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Prelude hiding (getLine,getContents, words, lines)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }
data EdgeTo = EdgeTo !Int !Int

readInt' = fst . fromJust . readInt

{- NOTE Vector of (Int, Int) could be prettier -}
data Sets s = Sets (STUArray s Int Int) (STUArray s Int Int)

newSets n = do
  ps <- newListArray (1, n) [1..n]
  rs <- newArray (1, n) 0
  return $ Sets ps rs

{- NOTE it's too generic, so haskell cannot optimize it -}
findSet :: Sets s -> Int -> ST s Int
findSet s@(Sets ps _) = f
  where
    f u = do
      p <- readArray ps u
      if p == u
        then return p
        else do
          v <- f p
          writeArray ps u v
          return v

unionSets s@(Sets ps rs) a b = do
  pa <- findSet s a
  pb <- findSet s b

  when (pa /= pb) $ do
    ra <- readArray rs a
    rb <- readArray rs b

    let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

    writeArray ps b' a'
    when (ra == rb) $ writeArray rs a' (ra' + 1)

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        s <- newSets n

        let
          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet s u
            pv <- findSet s v

            if pu /= pv
              then do
                unionSets s pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sortBy (comparing edgeW) es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (EdgeTo u w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter (\(EdgeTo u _) -> u /= p) $ adjacents ! u
        dfs 0 0 (EdgeTo 1 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [EdgeTo]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, EdgeTo v w)

    skips u i = (ps ! ii, ms ! ii)
      where
        ii = (u, i)

        ps = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            p <- read (v, i-1)
            pp <- read (p, i-1)
            write (v, i) pp
          return a

        ms = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            let p = ps ! (v, i-1)
            w <- read (v, i-1)
            ww <- read (p, i-1)
            write (v, i) $ max w ww
          return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = mu
      | otherwise = m
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', mu) = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, m) i = (v, max m m') where (v, m') = skips u i
            bits v = filter (testBit v) ix

        m = max m' $ max (vertexWeight $ vs ! v') (vertexWeight $ vs ! u'')
          where
            (v', u'', m') = foldl' step (v, u', mu) ix
              where
                step a@(v, u, m) i =
                    if v' /= u' then (v', u', max m (max mv mu)) else a
                  where
                    (v', mv) = skips v i
                    (u', mu) = skips u i

  putStr . unlines . map show $ map calc es

{-# LANGUAGE FlexibleContexts #-}

import Control.Applicative ((<*>), (<$>))
import Control.Monad (mapM_)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array, accumArray, listArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray,runSTArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (sortBy)
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import System.Random (newStdGen, random)
import Prelude hiding (getLine,getContents, words, lines)

readInts = map (fst . fromJust . readInt) . words

main = do
  [n, _] <- readInts `fmap` getLine
  es' <- (map ((\[u, v, w] -> (u, v, w)) . readInts) . lines) `fmap` getContents

  gen <- newStdGen

  let
    es = sortBy (comparing (\(_, _, w) -> w)) es'

    (l, treeEdges) = runST $ do
      ps <- newListArray (1, n) [1..n] :: ST s (STArray s Int Int)
      genr <- newSTRef gen

      let
          get u = do
            p <- readArray ps u
            if p == u
               then return p
               else do
                 pp <- get p
                 writeArray ps u pp
                 return pp

          unite u v = do
            pu <- get u
            pv <- get v

            g <- readSTRef genr
            let (r, g') = random g
            writeSTRef genr g'

            if pu /= pv
              then if r
                then writeArray ps pu pv
                else writeArray ps pv pu
              else return ()

          mst' [] = return (0, [])
          mst' (e@(u, v, w):es) = do
            pu <- get u
            pv <- get v

            if pu /= pv
              then do
                unite pu pv
                (l, treeEdges) <- mst' es
                return (l + (fromIntegral w :: Int64), e:treeEdges)
              else mst' es

      mst' es

    parents = runSTArray $ do
        a <- newArray (1, n) (0, 0)
        let dfs p (u, w) = do
            writeArray a u (p, w)
            mapM_ (dfs u) . filter ((/= p) . fst) $ adjacents ! u
        dfs 0 (1, 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ treeEdges ++ map swap treeEdges
          where
              swap (u, v, w) = (v, u, w)
              shift (u, v, w) = (u, (v, w))

    calc (u, v, w)
        | fst (parents ! u) == v || fst (parents ! v) == u = l
        | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max pw w''
      where
        (dv, du) = (depths ! v, depths ! u)

        ix :: [Int]
        ix = reverse [0..k]

        (u', w') = foldl step (u, 0) ix
          where
            step :: (Int, Int) -> Int -> (Int, Int)
            step a@(u, w) i = if depths ! u - dv >= 2^i then (v, max w w') else a
              where
                (v, w') = parents' ! (u, i)

        (v', _, w'') = foldl step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u'
                   then (v', u', max w (max w1 w2))
                   else a
              where
                (v', w1) = parents' ! (v, i)
                (u', w2) = parents' ! (u, i)

        (p, pw) = parents ! v'

    depths = listArray (1, n) $ map f [1..n]
      where
        f 1 = 0
        f u = depths ! (fst $ parents ! u) + 1

    parents' = listArray (head bs, last bs) $ map f bs :: Array (Int, Int) (Int, Int)
      where
        bs = (,) <$> [0..n] <*> [0..k]

        f :: (Int, Int) -> (Int, Int)
        f (0, i) = (0, 0)
        f (v, 0) = parents ! v
        f (v, i) = (v'', max w w')
          where
            (v', w) = parents' ! (v, i-1)
            (v'', w') = parents' ! (v', i-1)

  putStr . unlines . map show $ map calc es'

{-# LANGUAGE FlexibleContexts #-}

import Control.Applicative ((<*>), (<$>))
import Control.Monad (mapM_)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array, accumArray, listArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray,runSTArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (sortBy)
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import System.Random (newStdGen, random)
import Prelude hiding (getLine,getContents, words, lines)

readInts = map (fst . fromJust . readInt) . words

main = do
  [n, _] <- readInts `fmap` getLine
  es' <- (map ((\[u, v, w] -> (u, v, w)) . readInts) . lines) `fmap` getContents

  gen <- newStdGen

  let
    es = sortBy (comparing (\(_, _, w) -> w)) es'

    (l, treeEdges) = runST $ do
      ps <- newListArray (1, n) [1..n] :: ST s (STArray s Int Int)
      genr <- newSTRef gen

      let
          get u = do
            p <- readArray ps u
            if p == u
               then return p
               else do
                 pp <- get p
                 writeArray ps u pp
                 return pp

          unite u v = do
            pu <- get u
            pv <- get v

            g <- readSTRef genr
            let (r, g') = random g
            writeSTRef genr g'

            if pu /= pv
              then if r
                then writeArray ps pu pv
                else writeArray ps pv pu
              else return ()

          mst' [] = return (0, [])
          mst' (e@(u, v, w):es) = do
            pu <- get u
            pv <- get v

            if pu /= pv
              then do
                unite pu pv
                (l, treeEdges) <- mst' es
                return (l + w, e:treeEdges)
              else mst' es

      mst' es

    parents = runSTArray $ do
        a <- newArray (1, n) (0, 0)
        let dfs p (u, w) = do
            writeArray a u (p, w)
            mapM_ (dfs u) . filter ((/= p) . fst) $ adjacents ! u
        dfs 0 (1, 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ treeEdges ++ map swap treeEdges
          where
              swap (u, v, w) = (v, u, w)
              shift (u, v, w) = (u, (v, w))

    calc (u, v, w)
        | fst (parents ! u) == v || fst (parents ! v) == u = l
        | otherwise = l + w - (heaviest u v)

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = w''
      where
        (dv, du) = (depths ! v, depths ! u)

        ix :: [Int]
        ix = reverse [0..k]

        (u', w') = foldl step (u, 0) ix
          where
            step :: (Int, Int) -> Int -> (Int, Int)
            step a@(u, w) i = if depths ! u - dv >= 2^i then (v, max w w') else a
              where
                (v, w') = parents' ! (u, i)

        (v', _, w'') = foldl step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u'
                   then (v', u, max w (max w1 w2))
                   else a
              where
                (v', w1) = parents' ! (v, i)
                (u', w2) = parents' ! (u, i)

    depths = listArray (1, n) $ map f [1..n]
      where
        f 1 = 0
        f u = depths ! (fst $ parents ! u) + 1

    parents' = listArray (head bs, last bs) $ map f bs :: Array (Int, Int) (Int, Int)
      where
        bs = (,) <$> [0..n] <*> [0..k]

        f :: (Int, Int) -> (Int, Int)
        f (0, i) = (0, 0)
        f (v, 0) = parents ! v
        f (v, i) = (v'', max w w')
          where
            (v', w) = parents' ! (v, i-1)
            (v'', w') = parents' ! (v', i-1)

  putStr . unlines . map show $ map calc es'

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.Maybe (fromJust)
import Data.List (foldl', sortBy)
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }
data EdgeTo = EdgeTo !Int !Int

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sortBy (compare `on` edgeW) es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (EdgeTo u w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter (\(EdgeTo u _) -> u /= p) $ adjacents ! u
        dfs 0 0 (EdgeTo 1 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [EdgeTo]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, EdgeTo v w)

    skips u i = (ps ! ii, ms ! ii)
      where
        ii = (u, i)

        ps = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            p <- read (v, i-1)
            pp <- read (p, i-1)
            write (v, i) pp
          return a

        ms = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            let p = ps ! (v, i-1)
            w <- read (v, i-1)
            ww <- read (p, i-1)
            write (v, i) $ max w ww
          return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | otherwise = m
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', mu) = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, m) i = (v, max m m') where (v, m') = skips u i
            bits v = filter (testBit v) ix

        m = max m' $ max (vertexWeight $ vs ! v') (vertexWeight $ vs ! u'')
          where
            (v', u'', m') = foldl' step (v, u', mu) ix
              where
                step a@(v, u, m) i =
                    if v' /= u' then (v', u', max m (max mv mu)) else a
                  where
                    (v', mv) = skips v i
                    (u', mu) = skips u i

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (elems, listArray, accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray,runSTArray, STUArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64, Int16)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (foldl', sort, foldl1')
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Vertex = Vertex { vertexDepth :: Int, vertexParent :: Int, vertexWeight :: Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }

instance Eq Edge where
  (==) = (==) `on` edgeW
instance Ord Edge where
  compare = compare `on` edgeW

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sort es

    l = sum $ map edgeW mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (u, w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter ((/= p) . fst) $ adjacents ! u
        dfs 0 0 (1, 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [(Int, Int)]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, (v, w))

    skips u i = (ps ! ii, ms ! ii) where ii = (u, i)

    ps = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        p <- read (v, i-1)
        pp <- read (p, i-1)
        write (v, i) pp
      return a

    ms = runSTUArray $ do
      a <- newArray ((0, 0), (n, k)) 0
      let { read = readArray a; write = writeArray a }
      forM_ [0..k] $ \i -> write (0, i) 0
      forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
      forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
        let p = ps ! (v, i-1)
        w <- read (v, i-1)
        ww <- read (p, i-1)
        write (v, i) $ max w ww
      return a

    calc (Edge u v w) = l + (fromIntegral w) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max (max w3 w4) w''
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', w') = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, w) i = (v, max w w') where (v, w') = skips u i
            bits v = filter (testBit v) ix

        (v', u'', w'') = foldl' step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u' then (v', u', max w (max w1 w2)) else a
              where
                (v', w1) = skips v i
                (u', w2) = skips u i

        w3 = vertexWeight (vs ! v')
        w4 = vertexWeight (vs ! u'')

  putStr . unlines . map show $ map calc es

{-# LANGUAGE BangPatterns, FlexibleContexts #-}

import Control.Applicative ((<*>), (<$>))
import Control.Monad (mapM_)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array, accumArray, listArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray,runSTArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (foldl', sortBy)
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import System.Random (newStdGen, random)
import Prelude hiding (getLine,getContents, words, lines)

readInts = map (fst . fromJust . readInt) . words

main = do
  [n, _] <- readInts `fmap` getLine
  es' <- (map ((\[u, v, w] -> (u, v, w)) . readInts) . lines) `fmap` getContents

  gen <- newStdGen

  let
    es = sortBy (comparing (\(_, _, w) -> w)) es'

    treeEdges = runST $ do
      ps <- newListArray (1, n) [1..n] :: ST s (STArray s Int Int)
      genr <- newSTRef gen

      let
          get u = do
            p <- readArray ps u
            if p == u
               then return p
               else do
                 pp <- get p
                 writeArray ps u pp
                 return pp

          unite u v = do
            pu <- get u
            pv <- get v

            g <- readSTRef genr
            let (r, g') = random g
            writeSTRef genr g'

            if pu /= pv
              then if r
                then writeArray ps pu pv
                else writeArray ps pv pu
              else return ()

          mst' [] = return []
          mst' (e@(u, v, w):es) = do
            pu <- get u
            pv <- get v

            if pu /= pv
              then do
                unite pu pv
                treeEdges <- mst' es
                return $ e:treeEdges
              else mst' es

      mst' es

    l = fromIntegral (sum $ map (\(_, _, w) -> w) treeEdges) :: Int64

    parents = runSTArray $ do
        a <- newArray (1, n) (0, 0)
        let dfs p (u, w) = do
            writeArray a u (p, w)
            mapM_ (dfs u) . filter ((/= p) . fst) $ adjacents ! u
        dfs 0 (1, 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ treeEdges ++ map swap treeEdges
          where
              swap (u, v, w) = (v, u, w)
              shift (u, v, w) = (u, (v, w))

    calc (u, v, w)
        | fst (parents ! u) == v || fst (parents ! v) == u = l
        | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = max (max w3 w4) w''
      where
        (dv, du) = (depths ! v, depths ! u)

        ix :: [Int]
        ix = reverse [0..k]

        (u', w') = foldl' step (u, 0) ix
          where
            step :: (Int, Int) -> Int -> (Int, Int)
            step a@(u, w) i = if depths ! u - dv >= 2^i then (v, max w w') else a
              where
                (v, w') = parents' ! (u, i)

        (v', u'', w'') = foldl' step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u'
                   then (v', u', max w (max w1 w2))
                   else a
              where
                (v', w1) = parents' ! (v, i)
                (u', w2) = parents' ! (u, i)

        (_, w3) = parents ! v'
        (_, w4) = parents ! u''

    depths = listArray (1, n) $ map f [1..n]
      where
        f 1 = 0
        f u = depths ! (fst $ parents ! u) + 1

    parents' = listArray (head bs, last bs) $ map f bs :: Array (Int, Int) (Int, Int)
      where
        bs = (,) <$> [0..n] <*> [0..k]

        f :: (Int, Int) -> (Int, Int)
        f (0, i) = (0, 0)
        f (v, 0) = parents ! v
        f (v, i) = (v'', max w w')
          where
            (v', w) = parents' ! (v, i-1)
            (v'', w') = parents' ! (v', i-1)

  putStr . unlines . map show $ map calc es'

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.Maybe (fromJust)
import Data.List (foldl', sortBy)
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }
data EdgeTo = EdgeTo !Int !Int

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sortBy (compare `on` edgeW) es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (EdgeTo u w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter (\(EdgeTo u _) -> u /= p) $ adjacents ! u
        dfs 0 0 (EdgeTo 1 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [EdgeTo]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, EdgeTo v w)

    skips u i = (ps ! ii, ms ! ii)
      where
        ii = (u, i)

        ps = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            p <- read (v, i-1)
            pp <- read (p, i-1)
            write (v, i) pp
          return a

        ms = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            let p = ps ! (v, i-1)
            w <- read (v, i-1)
            ww <- read (p, i-1)
            write (v, i) $ max w ww
          return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = mu
      | otherwise = m
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', mu) = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, m) i = (v, max m m') where (v, m') = skips u i
            bits v = filter (testBit v) ix

        m = max m' $ max (vertexWeight $ vs ! v') (vertexWeight $ vs ! u')
          where
            (v', u'', m') = foldl' step (v, u', mu) ix
              where
                step a@(v, u, m) i =
                    if v' /= u' then (v', u', max m (max mv mu)) else a
                  where
                    (v', mv) = skips v i
                    (u', mu) = skips u i

  putStr . unlines . map show $ map calc es

{-# LANGUAGE FlexibleContexts #-}

import Control.Applicative ((<*>), (<$>))
import Control.Monad (mapM_)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array, accumArray, listArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray,runSTArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.Maybe (fromJust)
import Data.Ord (comparing)
import Data.List (sortBy)
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import System.Random (newStdGen, random)
import Prelude hiding (getLine,getContents, words, lines)

readInts = map (fst . fromJust . readInt) . words

main = do
  [n, _] <- readInts `fmap` getLine
  es' <- (map ((\[u, v, w] -> (u, v, w)) . readInts) . lines) `fmap` getContents

  gen <- newStdGen

  let
    es = sortBy (comparing (\(_, _, w) -> w)) es'

    (l, treeEdges) = runST $ do
      ps <- newListArray (1, n) [1..n] :: ST s (STArray s Int Int)
      genr <- newSTRef gen

      let
          get u = do
            p <- readArray ps u
            if p == u
               then return p
               else do
                 pp <- get p
                 writeArray ps u pp
                 return pp

          unite u v = do
            pu <- get u
            pv <- get v

            g <- readSTRef genr
            let (r, g') = random g
            writeSTRef genr g'

            if pu /= pv
              then if r
                then writeArray ps pu pv
                else writeArray ps pv pu
              else return ()

          mst' [] = return (0, [])
          mst' (e@(u, v, w):es) = do
            pu <- get u
            pv <- get v

            if pu /= pv
              then do
                unite pu pv
                (l, treeEdges) <- mst' es
                return (l + (fromIntegral w :: Int64), e:treeEdges)
              else mst' es

      mst' es

    parents = runSTArray $ do
        a <- newArray (1, n) (0, 0)
        let dfs p (u, w) = do
            writeArray a u (p, w)
            mapM_ (dfs u) . filter ((/= p) . fst) $ adjacents ! u
        dfs 0 (1, 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ treeEdges ++ map swap treeEdges
          where
              swap (u, v, w) = (v, u, w)
              shift (u, v, w) = (u, (v, w))

    calc (u, v, w)
        | fst (parents ! u) == v || fst (parents ! v) == u = l
        | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | v == u' = w'
      | otherwise = w''
      where
        (dv, du) = (depths ! v, depths ! u)

        ix :: [Int]
        ix = reverse [0..k]

        (u', w') = foldl step (u, 0) ix
          where
            step :: (Int, Int) -> Int -> (Int, Int)
            step a@(u, w) i = if depths ! u - dv >= 2^i then (v, max w w') else a
              where
                (v, w') = parents' ! (u, i)

        (v', _, w'') = foldl step (v, u', w') ix
          where
            step a@(v, u, w) i =
                if v' /= u'
                   then (v', u, max w (max w1 w2))
                   else a
              where
                (v', w1) = parents' ! (v, i)
                (u', w2) = parents' ! (u, i)

    depths = listArray (1, n) $ map f [1..n]
      where
        f 1 = 0
        f u = depths ! (fst $ parents ! u) + 1

    parents' = listArray (head bs, last bs) $ map f bs :: Array (Int, Int) (Int, Int)
      where
        bs = (,) <$> [0..n] <*> [0..k]

        f :: (Int, Int) -> (Int, Int)
        f (0, i) = (0, 0)
        f (v, 0) = parents ! v
        f (v, i) = (v'', max w w')
          where
            (v', w) = parents' ! (v, i-1)
            (v'', w') = parents' ! (v', i-1)

  putStr . unlines . map show $ map calc es'

{-# LANGUAGE BangPatterns, FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (mapM_, forM_, when)
import Control.Monad.ST.Safe (ST, runST)
import Data.Array (Array)
import Data.Array.IArray (accumArray, (!))
import Data.Array.MArray.Safe (newListArray, readArray, writeArray, newArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import Data.ByteString.Char8 (readInt, getLine, getContents, words, lines)
import Data.Int (Int64)
import Data.Maybe (fromJust)
import Data.List (foldl', sortBy)
import Data.Function (on)
import Prelude hiding (getLine,getContents, words, lines)
import Data.Bits (testBit)

data Vertex = Vertex { vertexDepth :: !Int, vertexParent :: !Int, vertexWeight :: !Int }
data Edge = Edge { edgeU :: !Int, edgeV ::  !Int, edgeW :: !Int }
data EdgeTo = EdgeTo !Int !Int

readInt' = fst . fromJust . readInt

main = do
  [n, _] <- (map readInt' . words) `fmap` getLine
  es <- (map ((\[u, v, w] -> Edge u v w) . map readInt' . words) . lines) `fmap` getContents

  let
    mst = runST $ do
        {- TODO extract Sets type -}
        ps <- newListArray (1, n) [1..n] :: ST s (STUArray s Int Int)
        rs <- newArray (1, n) 0 :: ST s (STUArray s Int Int)

        let
          findSet u = do
            p <- readArray ps u
            if p == u
              then return p
              else do
                v <- findSet p
                writeArray ps u v
                return v

          unionSets a b = do
            pa <- findSet a
            pb <- findSet b

            when (pa /= pb) $ do
              ra <- readArray rs a
              rb <- readArray rs b

              let (a', b', ra') = if ra < rb then (b, a, rb) else (a, b, ra)

              writeArray ps b' a'
              when (ra == rb) $ writeArray rs a' (ra' + 1)

          mst' [] = return []
          mst' (e@(Edge u v w):es) = do
            pu <- findSet u
            pv <- findSet v

            if pu /= pv
              then do
                unionSets pu pv
                (e:) <$> mst' es
              else mst' es

        mst' es'
      where es' = sortBy (compare `on` edgeW) es

    l = sum $ map (fromIntegral . edgeW) mst

    vs = runSTArray $ do
        a <- newArray (1, n) (Vertex 0 0 0) :: ST s (STArray s Int Vertex)
        let dfs d p (EdgeTo u w) = do
            writeArray a u (Vertex d p w)
            mapM_ (dfs (d+1) u) . filter (\(EdgeTo u _) -> u /= p) $ adjacents ! u
        dfs 0 0 (EdgeTo 1 0)
        return a
      where
        adjacents = accumArray (flip (:)) [] (1, n) . map shift $ mst ++ map swap mst :: Array Int [EdgeTo]
          where
              swap (Edge u v w) = Edge v u w
              shift (Edge u v w) = (u, EdgeTo v w)

    skips u i = (ps ! ii, ms ! ii)
      where
        ii = (u, i)

        ps = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexParent (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            p <- read (v, i-1)
            pp <- read (p, i-1)
            write (v, i) pp
          return a

        ms = runSTUArray $ do
          a <- newArray ((0, 0), (n, k)) 0
          let { read = readArray a; write = writeArray a }
          forM_ [0..k] $ \i -> write (0, i) 0
          forM_ [1..n] $ \v -> write (v, 0) $ vertexWeight (vs ! v)
          forM_ [1..k] $ \i -> forM_ [1..n] $ \v -> do
            let p = ps ! (v, i-1)
            w <- read (v, i-1)
            ww <- read (p, i-1)
            write (v, i) $ max w ww
          return a

    calc (Edge u v w)
      | vertexParent (vs ! u) == v || vertexParent (vs ! v) == u = l
      | otherwise = l + (fromIntegral w :: Int64) - (fromIntegral (heaviest u v))

    k = last $ takeWhile (\i -> 2^i <= n) [0..]

    heaviest v u
      | dv > du = heaviest u v
      | otherwise = m
      where
        ix = reverse [0..k]

        (dv, du) = (vertexDepth (vs ! v), vertexDepth (vs ! u))

        (u', mu) = foldl' step (u, 0) $ bits (du - dv)
          where
            step (u, m) i = (v, max m m') where (v, m') = skips u i
            bits v = filter (testBit v) ix

        m = max m' $ max (vertexWeight $ vs ! v') (vertexWeight $ vs ! u')
          where
            (v', u'', m') = foldl' step (v, u', mu) ix
              where
                step a@(v, u, m) i =
                    if v' /= u' then (v', u', max m (max mv mu)) else a
                  where
                    (v', mv) = skips v i
                    (u', mu) = skips u i

  putStr . unlines . map show $ map calc es