T,B = map(int,input().split()) def calc(N): l = [] while N: r = N % (-B) l.append(str(r)) N = (N - r) // B return "".join(reversed(l)) for _ in range(T): N = int(input()) if N == 0:print(0) else: print(calc(N))