#include<bits/stdc++.h>
using namespace std;
#define int         long long
#define mod         1000000007
#define FAST        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define f(i,n)      for(int i=0;i<n;i++)
#define fp(i,k,n)   for(int i=k;i<=n;i++)
#define fr(i,k,n)   for(int i=k;i>=n;i--)
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define dbg(x)      cout << (#x) << " is " << (x) << '\n' 
#define F           first
#define S           second
#define sz(x)       (int)(x).size()
#define lb          lower_bound
#define ub          upper_bound
#define mems(x)     memset(x,0,sizeof(x))
#define all(a)      a.begin(),a.end()
 
/*---------------------------------------------------------------------------------------------------*/
 
void solve()
{
    int m, n, k;
    cin >> n >> k;

    int a[n];

    int r = 0;
    int pref[n];
    int j = n - 1;
    int mn = LLONG_MAX;
    f (i, n)
    {
        cin >> a[i];
        r += a[i];
        pref[i] = r;

        int ai = a[i];

        int iter = 0;
        while(ai%k == 0)
        {
            iter+=1;
            ai = ai/k;
        }
        if(mn > iter)
        {
            j = i;
            mn = iter;
        }
    }
    cout << (mn + 1) * pref[n - 1] + ((j > 0) ? pref[j - 1] : 0) << endl;

    // int L = -1;
    // f (i, j + ((j == n - 1) ? 1 : 2))
    // {
    //     int curr = 0;
    //     int ai = a[i];

    //     while ((ai % k == 0))
    //     {
    //         ai = ai / k;
    //         curr++;
    //     }

    //     if (mn > curr)
    //     {
    //         mn = curr;
    //         L = i;
    //     }
    // }
    // dbg (mn);
    // // dbg(L);
    
} 
 
signed main()
{
    FAST
    int T = 1;
    cin >> T; 

    while (T--) solve();

    return 0;
}