import collections T, B = map(int, input().split()) for i in range(T): N = int(input()) a = collections.deque() while True: if N % B < 0: a.appendleft(N % B - B) N += B else: a.appendleft(N % B) N //= B if not N: break for j in a: print(j, end='') print()