import Data.List.Split
main = mapM_ (print . solve) . analyze . map (map read . words) . lines =<< getContents
analyze [] = []
analyze ([n]:cs) = let (xs,ys) = splitAt n cs in xs:analyze ys
solve = uncurry (+) . foldl1 f . map (\[t,a]->(t,a))
f (x,y) (t,a) = (u*t, u*a) where
  u = max ((x+t-1)`div`t) ((y+a-1)`div`a)