T,B=map(int,input().split()) for t in range(T): N=int(input()) ans_lst=[] while N: ans_lst.append(N%(-B)) N-=ans_lst[-1] N//=B if not ans_lst: ans_lst.append(0) print(*ans_lst[::-1],sep="")