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

main :: IO ()
main = readLine >>= print . solve

solve :: [Int] -> Int
solve [a, b, c, d] = b - a - cc - dc + cdc
    where
        cc = count c $ minv c
        dc = count d $ minv d
        cdc = count (lcm c d) . minv $ lcm c d

        minv x 
            | m > 0 = (r + 1) * x
            | otherwise = a
            where
                (r, m) = a `divMod` x

        count v m = (b - m) `div` v

            

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