import Control.Arrow
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
import Data.Ord

main :: IO ()
main = readLn >>= readContents . pred >>= main'
    where
        main' xs = readLine >>= printRes . solve xs
        printRes (m, ds) = print m
                        >> putStrLn (unwords $ map show ds)

solve :: [[Int]] -> [Int] -> (Int, [Int])
solve ns cs = (m, ds)
    where
        m = sum rs
        (r : rs) = sortOn Down cs
        ds = map fst . sortOn (fst . snd) . zip (r : rs) . sortBy (flip (comparing snd)) . map ((,) <$> head <*> length) . group . sort $ concat ns 

readLine :: IO [Int]
readLine = unfoldr (fmap (second (maybe B.empty snd . B.uncons)) . B.readInt) <$> B.getLine

readContents :: Int -> IO [[Int]]
readContents = flip replicateM readLine