#include <bits/stdc++.h>

#define ll                 long long
#define sf                 scanf
#define pf                 printf
#define nl                 printf("\n")
#define endl               '\n'
#define pb                 push_back
#define rep(i,a,b)         for(int i=a; i<b; i++)
#define repr(i,a,b)        for(int i=a; i>=b; i--)
#define TEST               int Test;cin>>Test;for(int _t=1;_t<=Test;_t++)
#define isValid(a,b,A,B)   (0<=a&&a<A) and (0<=b&&b<B)

using namespace std;

template <typename... T> void read(T &... args){ ((cin >> args), ...); }

void yes(){ pf("Yes\n"); }
void no() { pf("No\n"); }

int main()
{	
	ll n,x,a;
	TEST{
		sf("%lld %lld",&n,&x);
		vector<ll> vec;
		ll mx = 0;
		rep(i,0,n) sf("%lld",&a), mx=max(mx,a), vec.pb(a);
		ll sum = 0;
		rep(i,0,n) sum += vec[i];
		ll pow = x;
		while(pow<=mx){
			rep(i,0,n){
				if(vec[i]%pow==0) sum += vec[i];
				else{
					mx = -1;
					break;
				}
			}
			pow *= x;
		}
		pf("%lld\n",sum);
	}
	return 0;
}
