#include using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; string solve(ll N, ll B){ if(N == 0) return "0"; stringstream ss; while(N != 0){ ll d = N / B; ll r = N % B; if(r < 0){ d++; r += abs(B); } ss << r; N = d; } string ret = ss.str(); reverse(ret.begin(), ret.end()); return ret; } int main(){ int T, B; cin >> T >> B; rep(i,T){ int n; cin >> n; cout << solve(n, B) << endl; } return 0; }