#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <cstring>
#include <string>
#include <cassert>
#include <set>
#include <cmath>

using namespace std;

#define int long long
const int SZ = 2e5 + 4;
const double eps = 1e-9;

int n, k;

void read() {
    cin >> n >> k;
}

int add(int a, int b) {
    int ret = 0;
    int p = 1;
    while (p <= 2e9) {
        ret += (a % k + b % k) % k * p;
        a /= k;
        b /= k;
        p *= k;
    }

    return ret;
}

int subs(int a, int b) {
    int ret = 0;
    int p = 1;
    while (p <= 2e9) {
        ret += (a % k - b % k + k) % k * p;
        a /= k;
        b /= k;
        p *= k;
    }

    return ret;
}

void solve() {
    int cur = 0;

    for (int i = 0; i < n; ++i) {
        int tmp;
        if (i & 1) {
            tmp = subs(cur, i);
        } else {
            tmp = add(cur, i);
        }
        cout << tmp << endl;

        int x;
        cin >> x;
        if (x == 0) {
            cur = subs(tmp, cur);
        } else {
            break;
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

#ifdef LOCAL
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif

    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        read();
        solve();
    }

    return 0;
}
