#include <bits/stdc++.h>
#define sz(x) (int)x.size()

using ll = long long;
using namespace std;

void solve() {
  int n, x;
  cin >> n >> x;
  vector<pair<ll, ll>> a;
  ll ans = 0;
  for (int i = 0, l; i < n; ++i) {
    cin >> l;
    a.emplace_back(l, 1);
  }
  int i = 0;
  while (1) {
      if (i == a.size()) break;
      if (a[i].first % x) {
        break;
      }
      a.emplace_back(a[i].first / x, a[i].second * x);
      ++i;
  }
  for (int i = 0; i < a.size(); i++) {
    ans += a[i].first * a[i].second;
  }
  cout << ans << '\n';
}
 
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1;
  cin >> t;
  while (t--) { 
    solve();
  }
  return 0;
}