#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;

void solve() {
    ll n, x; cin >> n >> x;
    vector<ll>a, b;
    ll ans = 0;
    for(int i=0;i<n;++i) {
        ll t; cin >> t;
        a.push_back(t);
        b.push_back(t);
        ans += t;
    }
    bool fl=1;
    while(fl) {
        for(int i=0;i<n;++i) {
            if(b[i] % x != 0) {
                fl = 0;
                break;
            }
            ans += a[i];
            b[i] /= x;
        }
    }
    cout << ans << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);
    int T = 1;
    cin >> T;
    while(T--) solve();
    return 0;
}

