#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll bi(ll x,ll y,ll k){
	ll s=0,base=1;
	while(x||y){
		s+=((x%k+y%k)%k+k)%k*base;
		x/=k,y/=k;
		base*=k;
	}
	return s;
}
void out(ll &s){
	printf("%lld\n",s);
	fflush(stdout);
}
void work(){
	int n,i,k,r;
	ll sum=0,s,t=1;
	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++,t*=-1){
		s=bi(i*t,sum*t*-1,k);
		out(s);
		scanf("%d",&r);
		if(r){
			return;
		}
		sum=bi(sum,s*t,k);
	}
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		work();
	}
	return 0;
}

