#include <bits/stdc++.h>
#define s second
#define f first
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define read(x) for(auto& qw : (x)) cin >> qw;
#define print(x) for(auto& qw : (x)) cout << qw << " "; cout << endl;
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;

ll fexp(ll a, ll e){
	ll ans = 1;
	while(e){
		if(e & 1)
			ans *= a;
		a *= a;
		e >>= 1;
	}
	return ans;
}

int N;
ll K;
vector<int> V;

ll solve(ll mul){

	bool ok = 1;
	ll sum = 0;
	for(int i = 0; i < N; i++){
		ok &= !(V[i] % K);

		V[i] /= K;
		if(ok)
			sum += V[i]*mul;// cout << "adding " << V[i] << " " << V[i]*mul << endl;
		else break;
	}

	return sum + (ok ? solve(mul*K) : 0);
}

void test_case(){
	cin >> N >> K;

	V.resize(N);
	read(V);

	ll sum = 0;
	for(int i : V) sum += i;
	ll ans = solve(K);

	cout << ans + sum << endl;
}

int main(){ 

	int tt = 1;
	cin >> tt;

	while(tt--){
		test_case();
	}

	return 0;
}
