/******************************************************************
Copyright: 11D_Beyonder All Rights Reserved
Author: 11D_Beyonder
Problem ID: CF1471B
Date: 01/13/2020
Description: Greedy
*******************************************************************/
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100006;
int _;
int n,x;
int dep[N],a[N];
int main(){
	for(cin>>_;_;_--){
		cin>>n>>x;
		ll ans=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			dep[i]=1;
			ll q=a[i];
			while(q%x==0){
				dep[i]++;
				q/=x;
			}
		}
		int pos,MinDep=0x3f3f3f3f;
		for(int i=1;i<=n;i++){
			if(MinDep>dep[i]){
				MinDep=dep[i];
				pos=i;
			}
		}
		for(int i=1;i<pos;i++){
			ans+=1ll*(MinDep+1)*a[i];
		}
		for(int i=pos;i<=n;i++){
			ans+=1ll*MinDep*a[i];
		}
		cout<<ans<<endl;
	}
	return 0;
}