#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000



int main() {
    
	int t,b;
	cin>>t>>b;
	rep(_,t){
		int n;
		cin>>n;
		
		string ans = "";
		while(n!=0){
			int c = 0;
			while(n%b!=0){
				c++;
				n--;
			}
			n /= b;
			ans += '0' + c;
		}
		reverse(ans.begin(),ans.end());
		if(ans.size()==0)ans = "0";
		cout<<ans<<endl;
		
	}
    return 0;
}