#include<bits/stdc++.h>
#define ll long long int
#define vll vector<pair<ll, ll> >
#define vl vector<ll>
#define vi vector<int>
#define vii vector<pair<int, int> >
const ll mod = 1e9 + 7;
const ll inf = LLONG_MAX;
using namespace std;

ll power_mod(ll x, ll y, ll p)
{
	ll ans = 1;
	x = x%p;
	if(x==0)
	return p;

	while(y>0)
	{
		if(y&1)
		{
			ans = (ans*x)%p;
		}
		y = y>>1;
		x = (x*x)%p;
	}
	return ans;
} 

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int tt;
	cin>>tt;
	while(tt--)
	{
		ll i,j,k,l,m,n;
		ll x;
		cin>>n>>x;
		vl a(n, 0);
		
		
		ll sum=0;
		
		for(i=0;i<n;i++)
		{
		    cin>>a[i];
		    sum+=a[i];
		}
		ll maxdepth = inf;
		ll minpos = n;
		for(i=0;i<n;i++)
		{
		    
		    ll temp = 0;
		    ll temp2 = a[i];
		    while(temp2%x==0)
		    {
		        temp2/=x;
		        temp++;
		        if(temp2==0)
		        break;
		    }
		    if(temp<maxdepth)
		    {
		        maxdepth = temp;
		        minpos = i;
		    }
		    //cout<<"adding "<<temp*a[i]<<"\n";
		    //sum += temp*a[i];
		    
		}
		
		//cout<<"minpos:"<<minpos<<" maxdepth:"<<maxdepth<<"\n";
		
		for(i=0;i<n;i++)
		{
		    
		    ll temp = 0;
		    ll temp2 = a[i];
		    
		    if(i<minpos)
		    {
		        while(temp2%x==0)
    		    {
    		        temp2/=x;
    		        temp++;
    		        if(temp2==0)
    		        break;
    		    }
    		    temp = min(temp, maxdepth+1);
		    }
		    else
		    {
		        while(temp2%x==0)
    		    {
    		        temp2/=x;
    		        temp++;
    		        if(temp2==0)
    		        break;
    		    }
    		    temp = min(temp, maxdepth);
		    }
		    
		    
		    
		    //cout<<"adding "<<temp*a[i]<<"\n";
		    sum += temp*a[i];
		    
		}
		
		cout<<sum<<"\n";
	}
	return 0;
}
