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): print(calc(int(input())))