#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define sz(v) (int)v.size() 
#define all(v) v.begin(), v.end()
void dbg_out() { cerr << "\b\b]\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ cerr << H << ", "; dbg_out(T...);}
#define watch(...) cerr << "[" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)

int bound(int n, int x) {
  int cnts = 0;
  while (n % x == 0) {
    cnts++;
    n /= x;
  }
  return cnts;
}

void solve() {
  int n, x; cin >> n >> x;
  int idx = -1, mn = 1e9;
  vector <ll> A(n);
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
    sum += A[i];
    int cnts = bound(A[i], x);
    if (cnts < mn) {
      mn = cnts;
      idx = i;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (i < idx) {
      sum += (A[i] * (mn + 1)); 
    }
    else {
      sum += (A[i] * mn);
    }
  }
  cout << sum << endl;
}

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

    int t; cin >> t;
    while (t--) solve();

    return 0;
}

