main :: IO ()
main = do
  ss <- fmap (fmap (fmap (read :: String -> Int)) . fmap words . lines)
          getContents
  mapM_ putStrLn $ map func ss

func :: [Int] -> String
func ss = calc ss [] ""

calc :: [Int] -> [Int] -> String -> String
calc [0,q] _ s = s
calc [p,q] save s
  | p `div` q > 0 = calc [p - (p `div` q) * q, q]
                         (save ++ [p - (p `div` q) * q])
                         s
  | elem p save = s ++ "\n" ++ (repeats p save)
  | otherwise = calc [p*10 - (p*10 `div` q)*q,q]
                     (save ++ [p])
                     (s ++ (show $ p*10 `div` q))

repeats :: Int -> [Int] -> String
repeats p (x:xs)
  | p == x = replicate ((length xs) + 1) '^'
  | otherwise = ' ':(repeats p xs)