loop :: Int -> Int -> Int -> Int
loop _ 0 0 = 0
loop q a b
  |a == b = f q a (rem (b * 10) q)
  |otherwise = loop q (rem (a * 10) q) (rem (b * 100) q)
  where
    f :: Int -> Int -> Int -> Int
    f n r s
      |r == s = 1
      |otherwise = 1 + f n r (rem (s * 10) n)

loopSize :: Int -> Int -> Int
loopSize p q = loop q p (rem (p * 10) q)

timesDo :: Int -> Int -> Int -> Int
timesDo 0 _ r = r
timesDo t q r = timesDo (t - 1) q (rem (r * 10) q)

endSize :: Int -> Int -> Int -> Int
endSize p q d
  |p == d = 0
  |otherwise = 1 + endSize (rem (p * 10) q) q (rem (d * 10) q)

culDigit :: Int -> Int -> Int -> [Int]
culDigit _ _ 0 = []
culDigit p q t = div (p * 10) q : culDigit (rem (p * 10) q) q (t - 1)

solve :: Int -> Int -> String
solve p q
  |lsize == 0 = flat (culDigit p q esize)
  |otherwise = flat (culDigit p q (lsize + esize)) ++ (getStr esize lsize)
  where
    lsize = loopSize p q
    esize = endSize p q (if lsize == 0 then 0 else (timesDo lsize q p))
    getStr :: Int -> Int -> String
    getStr 0 0 = "\n"
    getStr 0 r = '^' : getStr 0 (r - 1)
    getStr n r = ' ' : getStr (n - 1) r

flat :: [Int] -> String
flat [] = "\n"
flat (a:xr) = (show a) ++ (flat xr)

split :: Char -> String -> [String]
split _ [] = [""]
split c (h:t)
  |h == c = [h] : (split c t)
  |otherwise = (h : (head $ split c t)) : (tail $ split c t)

main = do
  input <- getContents
  mapM_ putStr (map (\(a:xr) -> solve a (head xr)) (map ((map read) . (split ' ')) (lines input)))