#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define int ll
#define debug(v) cout << ">>" << #v << ':' << v << endl;
#define endl "\n"
#define pb push_back
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define umap unordered_map
#define pii pair<int , int>
#define all(v) v.begin() , v.end()   
#define ld long double
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d ) min(a , min(b , min(c,d)))
#define max3(a,b,c) max(a , max(b,c))
#define max4(a,b,c,d) max(max(a,b) , max(c,d))
                                                 
//sort(v.begin(), v.end(), 
//[](auto &left, auto &right) 
//{return left.second < right.second;});
void reader()
{
   #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    freopen("output.txt" , "w" , stdout);
   #endif
}

const int MOD = 1e9 + 7;
const int N = 3e5+5;
const int inf = 1e9;


void solve()
{
   int n , x;
   cin>>n>>x;
   vector<int> v(n);
   int sum=0;
   for(int i=0 ; i<n ; i++)cin>>v[i];
   for(int i=0 ; i<v.size() ; i++)
   {
       if(v[i]%x==0)v.pb( v[i]/x );
       else break;
   }
   for(int i=0 ; i<v.size() ; i++)
   {
       int ex = i/n;
       int val = pow(x,ex);
       val = val*v[i];
       sum+=val;
   }
   cout<<sum<<endl;
}


signed main() 
{
    IOS
    reader();
    int t;
    cin>>t;    
    for(int z=1 ; z<=t ; z++)
    {
        //cout<<"Case #"<<z<<": ";
        solve();
    }
    
}

