//Bishmillahir Rahmanir Rahim
//sajeedreefy
#include<bits/stdc++.h>
using namespace std;
#define SZ 100005
typedef long long int ll;
#define pb push_back
#define ull unsigned long long
#define mp make_pair
#define fr(i,n) for (ll i=0;i<n;i++)
#define ff first
#define ss second
#define mod 1000000007
void fastIO()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

signed main()
{
    fastIO();
    ll n,t=1,mini,x,l,c,m,i,j,k,count,y,sum;
    string st;
    cin >> t;
  //  ll arr[SZ];
    while(t--)
    {
        cin >> n >> x;
        ll arr[n+1];
        sum=0;
        vector <ll> vec,sec;
        fr(i,n)
        {
            cin >> arr[i];
            sum=sum+arr[i];
            vec.pb(arr[i]);
        }

        count=0;
        sec=vec;
        c=0;
        while(1)
        {
            for(i=0;i<sec.size();i++)
            {
                if(sec[i]%x!=0)
                {
                    c=1;
                    break;
                }
                else
                {
                    sum=sum+vec[i];
                    sec[i]=sec[i]/x;
                }
            }
            if(c==1)
            {
                break;
            }
        }
        cout << sum << endl;

    }
    return 0;

}
