T, B = map(int, input().split()) B = -B for _ in range(T): N = int(input()) if N == 0: print(0) continue cnt = 0 ans = [] while N: v = N % B # print(cnt, v, N) if cnt % 2: if v: ans.append(B - v) N += B else: ans.append(0) else: ans.append(v) N //= B cnt += 1 ans = ans[::-1] print(*ans, sep="")