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

using ll = long long;

using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >> t;

    while (t--) {
        int n, x;
        cin >> n >> x;
        vi ar(n);
        for (int i = 0; i < n; ++i) cin >> ar[i];

        ll res = 0;
        int d = 1;
        bool good = 1;
        while (good) {
            for (int i = 0; i < n; ++i) {
                if (ar[i] % d) {
                    good = 0;
                    break;
                } 
                res += ar[i];
            }
            d *= x;
        }
        // for (int i = 0; i < n && ar[i] % d == 0; ++i) res += ar[i];

        cout << res << '\n';
    }
}

