#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

using LL = long long;

int Rev(int a, int k) {
  vector<int> x;
  while (a > 0) {
    x.push_back((k - a % k) % k);
    a /= k;
  }
  reverse(x.begin(), x.end());
  int ans = 0;
  for (int i : x) ans = ans * k + i;
  return ans;
}

int Xor(int a, int b, int k) {
  vector<int> x;
  while (a > 0 || b > 0) {
    x.push_back((a + b) % k);
    a /= k;
    b /= k;
  }
  reverse(x.begin(), x.end());
  int ans = 0;
  for (int i : x) ans = ans * k + i;
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  int times;
  cin >> times;
  while (times--) {
    int n, k;
    cin >> n >> k;
    vector<int> x {0, Rev(1, k)};
    for (int i = 2; i < n; i += 2) {
      x.push_back(Xor(i, Rev(i - 1, k), k));
      x.push_back(Xor(i, Rev(i + 1, k), k));
    }
    for (int i = 0; i < n; ++i) {
      cout << x[i] << endl;
      int r;
      cin >> r;
      if (r == 1) break;
    }
  }
  return 0;
}