#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, K;
	cin >> N >> K;
	int A;
	vector<string> res(N);
	for (int i = 0; i < N; i++) {
		cin >> A;
		while (A != 0) {
			if (A < 0) {
				int t = abs(A) % (-K);
				if (t != 0)t = (-K) - t;
				res[i].push_back(t + '0');
				A -= t;
				A /= -K; A =  -A;
			}
			else {
				int t = abs(A) % (-K);
				res[i].push_back(t+ '0');
				A -= t;
				A /= -K; A = -A;
			}
		}
		if (res[i].size() == 0)res[i].push_back('0');
		reverse(res[i].begin(), res[i].end());
	}
	for (int i = 0; i < N; i++) {
		cout << res[i] << endl;
	}
}