def negative_base(n: int, base: int) -> list[int]: assert base < -1 if n == 0: return [0] res = [] x = n while x != 0: x, m = divmod(x, base) if m < 0: m += abs(base) x += 1 res.append(m) res.reverse() return res T, B = map(int, input().split()) for _ in range(T): N = int(input()) ans = negative_base(N, B) print(*ans, sep='')