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

main :: IO ()
main = readContents 2 >>= main'
    where
        main' [[n, m], as] = readContents m >>= print . solve n m as

solve :: Int -> Int -> [Int] -> [[Int]] -> Int
solve n m as bcs = count (sum fs) es ds
    where
        (es, fs) = span (< (last (head ds))) $ sort as
        ds = sortBy (flip (comparing last)) bcs

        count acc (x : xs) ([b, c] : ys)
            | b == 0 = count acc (x : xs) ys
            | x < c = count (acc + c) xs $ [b - 1, c] : ys
            | otherwise = acc + sum (x : xs)
        count acc xxs@(_ : _) _ = acc + sum xxs
        count acc _ _ = acc 

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