#include<bits/stdc++.h>
using namespace std;

int main() {

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 0;
    cin >> t;
    while(t--) {
        int n = 0, x = 0;
        cin >> n >> x;

        vector<long long> v(n, 0);
        vector<int> tm(n, 0);
        for(int i = 0; i < n; i ++) cin >> v[i];

        long long sum = 0;
        
        
        for(int i = 0; i < n; i++) {
            int temp = v[i];
            int cnt = 0;
            while(temp % x == 0) {
                cnt++;
                temp = temp/x;
            }
            tm[i] = cnt;
        }

        long long mn = INT_MAX;
        for(int i = 0; i < n; i++) mn = min(mn, (long long)tm[i]);
        int found = 0;
        // for(int i = 0; i < n; i++) {
        //     sum += v[i];
        // }
        // for(int i = 0; i < n; i++) cout << tm[i] << " ";
        // cout << endl;
        for(int i = 0; i < n; i++) sum += v[i];
        for(int i = 0; i < n; i++) sum += (long long) (mn) * v[i];
        for(int i = 0; i < n; i++) {
            if(tm[i] != mn) {
                sum += v[i];
            }
            else break;
        }


        cout << sum << endl;
    }

    return 0;
}