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