T, B = gets.split.map(&:to_i) T.times do n = gets.to_i ans = '' while n != 0 x = n n, m = n.divmod(B) if m != 0 n += 1 m -= B end ans << m.to_s end puts ans.reverse end