T,B = map(int,input().split()) A = abs(B) for i in range(T): N = int(input()) ls = [] while N > 0: ls.append(N%A) N //= A ls2 = [0]*40 for j in range(len(ls)): ls2[j] = ls[j] for j in range(39): if j % 2 == 0: ls2[j+1] += ls2[j]//A ls2[j] = ls2[j]%A else: ls2[j+1] += ls2[j]//A ls2[j] = ls2[j]%A if not ls2[j]==0: ls2[j] = A-ls2[j] ls2[j+1] += 1 while ls2[-1] == 0: ls2.pop() ls2.reverse() print(*ls2,sep='')