main = interact $ unlines . map (show . joseph . map read . words) . init . lines

joseph [n,m] = joseph' (cycle [1..n])
 where
  joseph' xs@(x1:x2:_)
   | x1 == x2 = x1
   | otherwise = let (x:xs') = (drop (m-1) xs) in joseph' (filter (/= x) xs')