#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define ppp pair<pii,pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define FOR(i,l,r) for (int i=l;i<=r;i++)
#define LOR(i,l,r) for (ll i=l;i<=r;i++)
#define FORD(i,r,l) for (int i=r;i>=l;i--)
#define LORD(i,r,l) for (ll i=r;i>=l;i--)
#define CL(x) memset(x,0,sizeof(x))
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)(x.size())
#define UI(x) (int)(x-'A')
#define LI(x) (int)(x-'a')
#define DI(x) (int)(x-'0')
#define LL_MAX (1LL<<60)
#define DB 0
#define DBG(x) if(DB)cout << #x << " : " << x << '\n'
#define PRALL(v) if(DB)for(auto it : v)cout<<v<<' '; NL;
#define NL cout<<'\n';
#define FND(S,x) (S.find(x)!=S.end())
#define bit(x,y) (!!(((ll)x)&(1LL<<y)))
typedef long long ll;
typedef long double ld;

int T, N, K;
int ans = 1;

int f(int a, int b)
{
    int c = 0;
    int p = 1;
    while( a || b )
    {
        c += (((a%K)+(b%K))%K)*p;
        a/=K;
        b/=K;
        p*=K;
    }
    return c;
}

int rev(int a)
{
    int c = 0;
    int p = 1;
    while( a )
    {
        c += ((K-(a%K))%K)*p;
        a/=K;
        p*=K;
    }
    return c;
}

int q(int x)
{
    int r;
    cout<<x<<'\n';
    cin>>r;
    return r;
    if(x==ans)
    {
        cout<<"!\n";
        return 1;
    }
    else
    {
        ans=f(ans,x);
        return 0;
    }
    return r;
}

int main()
{
    ios::sync_with_stdio(false);
    //cin.tie(0);

    cin>>T;
    FOR(t,1,T)
    {
        cin>>N>>K;
        int x = 0;
        FOR(i,0,N-1)
        {
            int a = (i%2)?rev(i):i;
            int y = f(a,x);
            int r = q(y);
            if( r==1 )break;
            else x=f(rev(x),y);
        }
    }

    return 0;
}


