#include <iostream>
#include <cmath>
#include <string>
#include <array>
#include <set>
#include <algorithm>
#include <map>
#include <cstring>
#include <vector>
#include <iomanip>
#include <array>
#include <stack>
#include <queue>
#include <deque>
#include <numeric>
typedef long long ll;

using namespace std;
ll a[1000000],b[1000001],c[100001], n,m,k,ans = 0,ans1 = 0,x ,y ,z,t ,h,v = 0,mx=0,mn = 0,sum = 0,cnt = 0;

int main() {
    cin>>t;
    while(t--){
        cin>>n>>m;
        sum = 0;

        for (int i = 0; i < n; ++i) {
            cin>>a[i];
            b[i] = a[i];
            sum += a[i];
        }

        bool ck = true;
        while(ck){
            for (int i = 0; i < n; ++i) {
                if(b[i] % m == 0) sum+=a[i],b[i] /=m;
                else{
                    ck = false;
                    break;
                }
            }
        }

        cout<<sum<<endl;

    }
}