#include <bits/stdc++.h>
#define ls i << 1
#define rs i << 1 | 1
#define lowbit(x) (x & (-x))
#define inf 0x3f3f3f3f
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mes(a, b) memset((a), (b), sizeof(a))
#define debug cout << "*****" << endl
#define pb push_back
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
const ll maxx = 1e5 + 10;
const ll mod = 1e9 + 7;
const int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
const int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
ll s[maxx];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        ll sum=0;
        int n,x;
        cin>>n>>x;
        for(int i=0;i<n;i++)
        {
            cin>>s[i];
            sum+=s[i];
        }
        ll pos=x;
        for(int i=0;i<n;i++)
        {
            if(s[i]%pos==0)
                sum+=s[i];
            else
                break;
            if(i==n-1)
                pos*=x,i=-1;
        }
        cout<<sum<<endl;
    }
    return 0;
}
