T,b = map(int,input().split()) for _ in range(T): n = int(input()) r = [] while n: r.append(n%-b) n = -(n//-b) if not r: print(0) else: print("".join(map(str,r[::-1])))