#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int n;
ll a[100005],x;

ll power(int x,int y){
    ll ans=1;
    while(y>0){
        if(y&1){
            ans = ans*x;
        }
        y = y/2;
        x = x*x;
    }
    return ans;
}

void solve(){
    cin >> n >>x;
    ll sum=0;
    for(int i=0;i<n;i++){
        cin >> a[i];
        sum += a[i];
    }
    ll cnt=0;
    int flag=1,i=0;

    while(flag==1){
        ll p = power(x,cnt);
        if((a[i]/p)%x==0) sum += a[i++];
        else {
            flag=0;
            break;
        }

        if(i==n){
            i=0;
            cnt++;
        } 
    }
    cout << sum << "\n";
}

int main() {
    int t;
    cin >> t;
    while(t--){
        solve();
    }
}
