import Control.Monad
import qualified Data.ByteString.Char8 as C
import qualified Data.IntMap.Strict as M
import Data.List
import Data.Maybe

main = join $ sub <$> (C.getLine *> get) <*> get

get = unfoldr (C.readInt . C.dropWhile (<'0')) <$> C.getLine

sub as [q] = sol s m <$> replicateM q get >>= mapM_ print
  where
  s = sum $ fmap fromIntegral as :: Integer
  m = M.fromList . fmap ((,) <$> head <*> length) . group $ sort as

sol :: Integer -> M.IntMap Int -> [[Int]] -> [Integer]
sol s m = fmap fst . tail . scanl' f (s,m) 

f (s,m) [b,c] = (s+fromIntegral ((c-b)*v),M.insertWith (+) c v $ M.delete b m)
  where
  v = M.findWithDefault 0 b m