#include<bits/stdc++.h>

using namespace std;

#define endl "\n"
#define ll long long int

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    ll t;
    cin>>t;
    
    while(t--)
    {
        ll n,x;
        cin>>n>>x;
        
        vector<pair<ll,ll>> v;
        
        for(int i=0;i<n;i++)
        {
            ll k;
            cin>>k;
            v.push_back({k,1});
        }
        
        ll l=x;
        
        for(int i=1;i<=v.size();i++)
        {
            if(v[i-1].first%x!=0)
                break;
            v.push_back({(v[i-1].first/x),l});
            if(i%n==0)
               l*=x;
        }
        
        /*for(auto c:v)
            cout<<c.first<<" "<<c.second<<endl;*/
        
        ll sum=0;
        
        for(int i=0;i<v.size();i++)
        {
            sum+=(v[i].first*v[i].second);
        }
        
        cout<<sum<<endl;
        
    }
    
    return 0;
}










