#include <iostream>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <unordered_map>

using namespace std;

#define int int64_t
#define FOR(k,a,b) for(int k=(a); k<(b); ++k)
#define REP(k,a) for(int k=0; k<(a);++k)
#define SZ(a) int((a).size())
#define ALL(c) (c).begin(),(c).end()
#define MOD 1000000007

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;

signed main()
{
    ios_base::sync_with_stdio(false);

    #ifndef ONLINE_JUDGE
    freopen("sample2.txt", "r", stdin);
    #endif

    int T;
    cin >> T;
    REP(tc, T) {
        int N, X; 
        cin >> N >> X;
        int sum = 0, mini = 0;

        vi strange(N, 0);
        vi pf(N, 0);
        REP(i, N) {
            int xi;
            cin >> xi;
            strange[i] = xi;
            sum += xi;

            while (xi % X == 0) {
                xi /= X;
                pf[i]++;
            }

            if (i == 0)
                mini = pf[i];
            else if (mini > pf[i]) 
                mini = pf[i];
        }

        bool fin = false;
        REP(i, N) {
            if (pf[i] == mini) {
                fin = true;
            }

            if (fin) {
                sum += mini * strange[i];
            } else {
                sum += (mini + 1) * strange[i];
            }
        }

        cout << sum << endl;
    }

    return 0;
}
