def keisan(n,b): s ="" while n !=0: s = str(n%abs(b)) +s n = -(n-n%abs(b))//abs(b) print(s) T,B =map(int,input().split()) for _ in range(T): keisan(int(input()),B)