import Control.Monad
import Control.Applicative

solve :: String -> Int
solve str = do
	let [n, m] = map read $ words str :: [Int]
	if n `mod` m == 0 then m else n `mod` m

main = do
	n <- readLn :: IO Int
	s <- replicateM n getLine
	mapM_ print $ map solve s