Problem: This is the hard version of the problem. The only difference is that here 2≤ k≤ 100. You can make hacks only if both the versions of the problem are solved.

This is an interactive problem!

Every decimal number has a base k equivalent. The individual digits of a base k number are called k-its. Let's define the k-itwise XOR of two k-its a and b as (a + b)mod k.

The k-itwise XOR of two base k numbers is equal to the new number formed by taking the k-itwise XOR of their corresponding k-its. The k-itwise XOR of two decimal numbers a and b is denoted by a⊕_{k} b and is equal to the decimal representation of the k-itwise XOR of the base k representations of a and b. All further numbers used in the statement below are in decimal unless specified.

You have hacked the criminal database of Rockport Police Department (RPD), also known as the Rap Sheet. But in order to access it, you require a password. You don't know it, but you are quite sure that it lies between 0 and n-1 inclusive. So, you have decided to guess it. Luckily, you can try at most n times without being blocked by the system. But the system is adaptive. Each time you make an incorrect guess, it changes the password. Specifically, if the password before the guess was x, and you guess a different number y, then the system changes the password to a number z such that x⊕_{k} z=y. Guess the password and break into the system.

Input

The first line of input contains a single integer t (1≤ t≤ 10 000) denoting the number of test cases. t test cases follow.

The first line of each test case contains two integers n (1≤ n≤ 2⋅ 10^5) and k (2≤ k≤ 100).

It is guaranteed that the sum of n over all test cases does not exceed 2⋅ 10^5.

Interaction

For each test case, first read two integers n and k. Then you may ask up to n queries.

For each query, print a single integer y (0≤ y≤ 2⋅ 10^7). Let the current password be x. After that, read an integer r.

If x=y, you will read r=1 and the test case is solved. You must then continue solving the remaining test cases.

Else, you will read r=0. At this moment the password is changed to a number z such that x⊕_{k} z=y.

After printing a query, do not forget to output the end of line and flush the output. Otherwise, you will get the Idleness limit exceeded verdict.

To do this, use:

  * fflush(stdout) or cout.flush() in C++; 
  * System.out.flush() in Java; 
  * flush(output) in Pascal; 
  * stdout.flush() in Python; 
  * see documentation for other languages. 



If you ask an invalid query or exceed n queries, you will read r=-1 and you will receive the Wrong Answer verdict. Make sure to exit immediately to avoid unexpected verdicts.

Note that the interactor is adaptive. That is, the original password is not fixed in the beginning and may depend on your queries. But it is guaranteed that at any moment there is at least one initial password such that all the answers to the queries are consistent.

Hacks:

To use hacks, use the following format of tests:

The first line should contain a single integer t (1≤ t≤ 10 000) — the number of test cases.

The first and only line of each test case should contain two integers n (1≤ n≤ 2⋅ 10^5) and k (2≤ k≤ 100) denoting the number of queries and the base respectively. The optimal original password is automatically decided by the adaptive interactor.

You must ensure that the sum of n over all test cases does not exceed 2⋅ 10^5.

Example

Input


2
5 2

0

0

1
5 3

0

0

1


Output


3

4

5


1

4

6

Note

Test Case 1:

In this case, the hidden password is 2.

The first query is 3. It is not equal to the current password. So, 0 is returned, and the password is changed to 1 since 2⊕_2 1=3.

The second query is 4. It is not equal to the current password. So, 0 is returned, and the password is changed to 5 since 1⊕_2 5=4.

The third query is 5. It is equal to the current password. So, 1 is returned, and the job is done.

Test Case 2:

In this case, the hidden password is 3.

The first query is 1. It is not equal to the current password. So, 0 is returned, and the password is changed to 7 since 3⊕_3 7=1. [3=(10)_3, 7=(21)_3, 1=(01)_3 and (10)_3⊕_3 (21)_3 = (01)_3].

The second query is 4. It is not equal to the current password. So, 0 is returned, and the password is changed to 6 since 7⊕_3 6=4. [7=(21)_3, 6=(20)_3, 4=(11)_3 and (21)_3⊕_3 (20)_3 = (11)_3].

The third query is 6. It is equal to the current password. So, 1 is returned, and the job is done.

Note that these initial passwords are taken just for the sake of explanation. In reality, the grader might behave differently because it is adaptive.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#ifdef LOCAL
	#include "lib.cpp"
#endif
#define fio ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
#define TC long long T_;cin>>T_;while(T_--)
#define rar(a,n) int n;cin >> n;int a[n];for(int i_=0;i_<n;i_++)cin >> a[i_];
#define rark(a,n,k) int n,k;cin >> n >> k;int a[n];for(int i_=0;i_<n;i_++)cin >> a[i_];
#define A << " " <<
#define fr(i,s,e) for(int i=s;i<e;i++)
#define co(x) cout << x << " ";
#define con(x) cout << x << "\n";
#define fe(x,v) for(auto& x : v)
#define at(s,i) (s.begin()+i)
#define all(s) s.begin(),s.end()
#define has(s,x) (s.find(x)!=s.end())
#define vi2(v,n,m,x) vector<vector<int>> v(n,vector<int>(m,x));
#define pv(v) for(auto x_ : v)cout << x_ << " ";cout << endl;
#define pa(a,n) for(int i_=0;i_<n;i_++)cout << a[i_] << " ";cout << endl;
#define paa(a,n,m) for(int i_=0;i_<n;i_++){for(int j_=0;j_<m;j_++)cout << a[i_][j_] << " ";cout << "\n";}
using namespace std;typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pii;

vector<int> basek(long long n, int k){
	vector<int> v;
	if(n==0)v.push_back(0);
	while(n){
		v.push_back(n%k);
		n/=k;
	}
	return v;
}

ll basek_decimal(vector<int>& v, int k){
	ll sm=0,pw=1;
	for(int i=0;i<(int)v.size();i++){
		sm+=pw*v[i];
		pw*=k;
	}
	return sm;
}

void basek_complement(vector<int>& v,vector<int>& w, int k){
	for(int i=0;i<(int)v.size();i++){
		w[i]=(k-v[i])%k;
	}
}
void basek_xor(vector<int>& a,vector<int>& b,vector<int>& c, int k){
	if(a.size()<b.size())a.resize(b.size());
	if(b.size()<a.size())b.resize(b.size());
	c.resize(a.size());
	for(int i=0;i<(int)a.size();i++){
		c[i]=(a[i]+b[i])%k;
	}
}
void basek_mxor(vector<int>& a,vector<int>& b,vector<int>& c, int k){
	if(a.size()<b.size())a.resize(b.size());
	if(b.size()<a.size())b.resize(b.size());
	c.resize(a.size());
	for(int i=0;i<(int)a.size();i++){
		c[i]=(b[i]-a[i]+k)%k;
	}
}
void basek_increment(vector<int>& v,int k){
	int cry=0,n=v.size();
	v[0]+=1;
	for(int i=0;i<n;i++){
		v[i]+=cry;
		cry=0;
		if(v[i]>=k)v[i]%=k,cry=1;
	}
	if(cry==1){
		v.resize(n+1);
		v[v.size()-1]=1;
	}
}
void f_(){
	int n,k;
	cin >> n >> k;
	vi lq=basek(0,k);
	cout << 0 << endl;
	int x;
	cin >> x;
	if(x==1)return;
	fr(i,1,n){
		vi nq=basek(i,k);
		if(i&1)basek_mxor(nq,lq,lq,k);
		else basek_mxor(lq,nq,lq,k);
		cout << basek_decimal(lq,k) << endl;
		lq=nq;
		int x;
		cin >> x;
		if(x==1)break;
	}
}

signed main() {fio;
	TC
	f_();
	return 0;
}