#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 200010;
int tc, cs = 1;
int n;
ll x, arr[N], cnt[N];

ll getval(ll num, ll cnt) {
  ll ret = 0, p = 1;
  for (int step = 0; step <= cnt; ++step) {
    ret += p*num;
    num /= x;
    p *= x;
  }
  return ret;
}

void test_cases() {
  cin >> n >> x;
  for (int i = 1; i <= n; ++i) cnt[i] = 0;
  for (int i = 1; i <= n; ++i) cin >> arr[i];
  ll mn = INT_MAX, mnidx = -1;
  for (int i = 1; i <= n; ++i) {
    ll tmp = arr[i];
    while (tmp % x == 0) {
      cnt[i]++;
      tmp /= x;
    }
    if (cnt[i] < mn) {
      mn = cnt[i];
      mnidx = i;
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (i < mnidx) ans += getval(arr[i], min(mn+1, cnt[i]));
    else ans += getval(arr[i], mn);
  }
  cout << ans << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> tc;
  while (tc--) {
    test_cases();
  }
}