#include #include #include #include using namespace std; void verify(const string &ans, int n, int b) { int m = (int) ans.size(); long long v = 0; for (int i = 0; i < m; i++) { int d = ans[i] - '0'; assert(d >= 0); assert(d < -b); v *= b; v += d; } assert(v == n); assert(n == 0 || ans[0] != '0'); } string solve(int n, int b) { string ans; while (n) { int m = n % b; n /= b; if (m < 0) { m -= b; n++; } ans += '0' + m; } reverse(ans.begin(), ans.end()); if (ans == "") ans += '0'; return ans; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int t, b, n; cin >> t >> b; assert(t >= 1); assert(t <= 100000); assert(b >= -10); assert(b <= -2); while (t--) { cin >> n; assert(n >= 0); assert(n <= 1000000000); string ans = solve(n, b); cout << ans << endl; verify(ans, n, b); } return 0; }