#include <iostream> 
#include <bits/stdc++.h> 
#define ll long long 
using namespace std; 
void solve() 
{
	ll n,x; cin >> n >> x;
	ll arr[n];  
	ll sum = 0; 
	ll b[n]; 
	for(ll i = 0; i< n;i ++ ) 
	{
		cin >> arr[i];
		b[i]= arr[i];  
		sum+=arr[i];
	}
		 // ll temp=arr[i];
		ll i=0; 
		while(arr[i]%x==0)
		{
			sum+=b[i]; 
			arr[i] = arr[i]/x;
			i++;
			if(i == n )
			{
				i = 0; 
			}
		}
	
	cout << sum << endl; 
}
int main() 
{
	int t; cin >> t; 
	while (t--) 
	{
		solve(); 
	}
}