import java.io.*; import java.util.Scanner; public class Main_yukicoder782 { private static Scanner sc; private static Printer pr; private static void solve() { int t = sc.nextInt(); int b = sc.nextInt(); for (int tcase = 0; tcase < t; tcase++) { int n = sc.nextInt(); StringBuilder ans = new StringBuilder(); while (n != 0) { int mod = n % b; if (mod < 0) { mod -= b; } ans.append(mod); n = (n - mod) / b; } pr.println(ans.reverse().toString()); } } // --------------------------------------------------- public static void main(String[] args) { sc = new Scanner(System.in); pr = new Printer(System.out); solve(); pr.close(); sc.close(); } static class Printer extends PrintWriter { Printer(OutputStream out) { super(out); } } }