#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

int T, B;

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    std::cin >> T >> B;

    for(int i=0;i<T;++i){
        int N;
        std::cin >> N;

        if(N == 0){
            std::cout << "0" << std::endl;
            continue;
        }

        std::vector<int> v;
        
        while(N != 0){
            int rem = N % B;
            N /= B;

            if(rem < 0){
                rem += -B;
                N += 1;
            }

            v.emplace_back(rem);
        }

        std::reverse(v.begin(), v.end());

        for(int r : v){
            std::cout << r;
        }
        std::cout << std::endl;
    }
}