$<.each do |l|
  p, q = l.split.map(&:to_i)
  i, rs = 0, { p => 0 }
  loop do
    i += 1
    x, p = (p * 10).divmod q
    print x
    if p.zero?
      puts
      break
    end
    if rs.key?(p)
      puts
      puts ' ' * rs[p] + ?^ * (i - rs[p])
      break
    end
    rs[p] = i
  end
end