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

main :: IO ()
main = readContents 3 >>= print . solve

solve :: [[Int]] -> Int
solve [[n], as@[ga, sa, ba], bs@[gb, sb, bb]] 
    | res && ya > yb = exchange rabs $ exchange rbas n 
    | res = exchange rbas n
    | check rabs = exchange rabs n
    | otherwise = n
    where
        sf xs = sortBy (flip (comparing (uncurry (%)))) . zip xs
        rbas@((xb, xa) : _) = sf bs as
        rabs@((ya, yb) : _) = sf as bs

        res = check rbas
        
        check = not . any (\(x, y) -> x > y && y >= n)

        exchange rs c = edit . foldl' ff (0, c) . sortBy cmpf $ filter (\(_, y) -> c >= y) rs
            where
                edit = uncurry (+)
                cmpf lhs rhs = comparing (uncurry (%)) rhs lhs `mappend` comparing snd lhs rhs
                ff (acc, bcc) (x, y)
                    | bcc >= y = (acc + r * x, m)
                    | otherwise = (acc, bcc)
                    where
                        (r, m) = bcc `divMod` y

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