#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n,x;
        cin>>n>>x;
        // vector<int>v;
        
        queue<pair<ll,ll>>q;
        
        for(int i=0;i<n;++i)
        {
            ll ask;
            cin>>ask;
            q.push(make_pair(ask,1));
        }
        
        int flag=0;
        ll sum1=0;
        // int prev = 0;
        while(!q.empty())
        {
            pair<ll,ll>num1 = q.front();
            q.pop();
            
            // cout<<num1.first<<" "<<num1.second<<endl;
            
            
            if(num1.first%x==0 && flag==0)
            {
                q.push(make_pair(num1.first/x,x*num1.second));
            }
            else{
                flag=1;
            }
            
            
            
            sum1+=(num1.first*num1.second);
            
        }
        
        cout<<sum1<<endl;
        
    }
    return 0;
}