import Control.Monad

main :: IO ()
main = do
  n <- readLn
  ls <- replicateM n getLine
  mapM_ print $ solve (map (map read . words) ls)

solve :: [[Int]] -> [Int]
solve = map minVal
  where
    minVal :: [Int] -> Int
    minVal [x,y,b,p]
      | b < 5 && p < 2 = min normalVal ((x * 5 + y * 2) `div` 10 * 8)
      | b < 5 && p >= 2 = min normalVal ((x * 5 + y * p) `div` 10 * 8)
      | b >= 5 && p < 2 = min normalVal ((x * b + y * 2) `div` 10 * 8)
      | otherwise = normalVal `div` 10 * 8
      where
        normalVal = x * b + y * p