#include <bits/stdc++.h>
using namespace std;
const int MM = 1e5 + 5;
long long t, n, A[MM], x, cnt = INT_MAX, idx0 = -1;

int main() {
    cin.tie(0);
    cin >> t;
    while(t--) {
        cnt = INT_MAX, idx0 = -1;
        cin >> n >> x;
        for (int i = 1; i <= n; i++) cin >> A[i];
        for (int i = 1; i <= n; i++) {
            int tmp = A[i], tcnt = 0;
            while (tmp % x == 0) tmp /= x, tcnt++;
            if (tcnt < cnt) {
                cnt = tcnt;
                idx0 = i;
            }
        }
        long long sum = 0;
        for (int i = 1; i < idx0; i++) sum += (cnt + 2) * A[i];
        for (int i = idx0; i <= n; i++) sum += (cnt + 1) * A[i];
        cout << sum << "\n";
    }
}