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:
// time-limit: 5000
// problem-url: https://codeforces.com/problemset/problem/1543/D2
//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;

#define show(x) cerr << #x" = " << (x) << "\n"
#define pb push_back
#define pp pop_back
#define mp make_pair
#define fst first
#define snd second
#define FOR(var, from, to) for(int var = from; var < int(to); ++var)
#define all(x) x.begin(), x.end()
#define rev(x) x.rbegin(), x.rend()
#define sz(x) int(x.size())
#define INF 2000000000

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
// use unique second element of pair to work as multiset
//typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
const ll mod = 1e9 + 7;

template<typename T, typename U> ostream &operator<<(ostream &os, pair<T,U> p){os << "(" << p.fst << "," << p.snd << ")"; return os;}
template<typename T, typename U> istream &operator>>(istream &is, pair<T,U> &p){is >> p.fst >> p.snd; return is;}
template<typename T> istream &operator>>(istream &is, vector<T> &v){FOR(i, 0, v.size()) is >> v[i]; return is;}
template<typename T> ostream &operator<<(ostream &os, vector<T> v){for(T x : v) os << x << " "; return os;}
template<typename T> ostream &operator<<(ostream &os, set<T> s){for(T x : s) os << x << " "; return os;} 
//ostream &operator<<(ostream &os, ordered_set s){for(int x : s) os << x << " "; return os;} 
//ostream &operator<<(ostream &os, ordered_multiset s){for(pii x : s) os << x.fst << " "; return os;} 
template<typename T> ostream &operator<<(ostream &os, multiset<T> s){for(T x : s) os << x << " "; return os;} 
template<typename T, typename U> ostream &operator<<(ostream &os, map<T,U> m){for(auto x : m) os << x << " "; return os;} 
template<typename T> T sum(vector<T> v){T summ = 0; FOR(i, 0, sz(v)) summ += v[i]; return summ;}
ll mod_pow(ll a, ll b){ ll sol = 1; while(b){ if(b&1){ sol = (sol*a)%mod; b--; }else{ a = (a*a)%mod; b/=2; } } return sol;}
ll rem(ll a, ll b){ ll res = a%b; return res < 0 ? res+b : res; }

vector<int> to_base(int n, int k){
	vector<int> v;
	if(n == 0){
		v.pb(0);
	}
	while(n > 0){
		v.pb(n%k);
		n /= k;
	}
	reverse(all(v));
	return v;
}
int knxor(int x,int y,int k)
{
    int z=0;
    int p=1;
    while(x>0 || y>0)
    {
        int a=x%k;
        x=x/k;
        int b=y%k;
        y=y/k;
        int c=(a-b+k)%k;
        z=z+p*c;
        p=p*k;
    }
    return z;
}
int sub(int i1, int i2, int k){
	vector<int> a = to_base(i1, k), b = to_base(i2, k);
	int pow = 1;
	int res = 0;
	while(!a.empty() || !b.empty()){
		if(a.empty()){
			res += pow * rem(-b.back(), k);
			b.pp();
		}else if(b.empty()){
			res += pow * a.back();
			a.pp();
		}else{
			res += pow * rem(a.back() - b.back(), k);
			a.pp();
			b.pp();
		}
		pow *= k;
	}
	return res;
}

void ask(int i, int k){
	if(i == 0){
		cout << 0 << endl;
	}else if(i & 1){
		cout << knxor(i-1, i, k) << endl;
		//cout << sub(i-2, i-1, k) << endl;
	}else{
		cout << knxor(i, i-1, k) << endl;
		//cout << sub(i-1, i-2, k) << endl;
	}
}

void test_case(){
	int n,k; cin >> n >> k;
	int res = 0;
	FOR(i, 0, n){
		ask(i, k);
		//if(i&1){
		//	cerr << sub(3, i, k) << "\n";
		//	cerr << knxor(3, i, k) << "\n";
		//}else{
		//	cerr << sub(i, 3, k) << "\n";
		//	cerr << knxor(i, 3, k) << "\n";
		//}
		cin >> res;
		if(res == -1){
			cout << "aaaaaaa" << endl;
			return;
		}else if(res == 1){
			break;
		}
	//	i++;
	}
}

int main(){
	#ifndef DEBUG
	ios_base::sync_with_stdio(false);
  	cin.tie(NULL);
	#endif

	int t; cin >> t;
	FOR(i, 0, t) test_case();
	return 0;
}