#include<bits/stdc++.h>
using namespace std;
 
int main(){
	int t; cin>>t;
	while(t--){
		int n ,k ,x;
		cin>>n>>k;
		vector< pair < int,int > > arr;
		long long sum = 0 ;
		for(int i = 0 ; i<n ; i++){
			cin>>x;
			
			arr.push_back(make_pair(x,k));
			sum+=x;
			}
		unsigned int i = 0 ;
		bool flag = true;
		while(i<arr.size()){
			
			int z = arr[i].first/k;
			int rem = arr[i].first%k;
			
			if(rem!=0) break;
			if(z%k!=0){
				
				flag = false;
			}
			if(flag){
				arr.push_back(make_pair(z,arr[i].second*k));
			}
			sum+=z*arr[i].second;
			
			i++;
			}
		cout<<sum<<endl;
		
		}
	return 0;
}
