#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
using pll=pair<ll,ll>;

ll arr[100010];

int main(){
	int t,n,x,cnt,idx,tmpcnt;
	ll ans,tmp;
	bool flag;
	for(scanf("%d",&t);t--;){
		scanf("%d %d",&n,&x);
		for(int i=0;i<n;++i)scanf("%d",arr+i);
		ans=0;cnt=1;idx=0;
		flag=false;
		tmp=arr[0];
		while(tmp%x==0){
			cnt++;
			tmp/=x;
		}
		for(int i=1;i<n;++i){
			tmpcnt=1;tmp=arr[i];
			while(tmp%x==0){
				tmpcnt++;
				tmp/=x;
			}
			if(cnt>tmpcnt){
				cnt=tmpcnt;
				idx=i;
			}
		}
		for(int i=0;i<idx;++i)ans+=arr[i]*(cnt+1);
		for(int i=idx;i<n;++i)ans+=arr[i]*cnt;
		printf("%lld\n",ans);
	}
}