#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long MAX = 5100000; const long long INF = 1LL << 60; const long long mod = 1000000007LL; //const long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ ll t, b; scanf("%lld %lld", &t, &b); while (t--) { ll n; scanf("%lld", &n); vector res; while (n != 0) { if (n > 0) { res.push_back(n % b); n /= b; } else { ll tmp = n % b; if (tmp < 0) { n /= b; n++; res.push_back(tmp - b); } else { res.push_back(n % b); n /= b; } } } reverse(res.begin(), res.end()); if (res.empty()) puts("0"); else { for (ll i = 0; i < res.size(); i++) { cout << res[i]; } cout << "\n"; } } return 0; }