#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;

int T, n;
ll x, cnt[maxn], a[maxn];

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.txt", "r", stdin);
#endif
	scanf("%d", &T);
	while (T--) {
		scanf("%d%lld", &n, &x);
		ll mn = 1e9, res = 0;
		for (int i = 1; i <= n; ++i) {
			scanf("%lld", &a[i]);
			ll tmp = a[i];
			cnt[i] = 0;
			while (tmp % x == 0) {
				++cnt[i];
				tmp /= x;
			}
			mn = min(mn, cnt[i] + 1);
		}
		bool flag = false;
		for (int i = 1; i <= n; ++i) {
			if (flag) {
				res += mn * a[i];
				continue;
			}
			if (cnt[i] + 1 > mn) {
				res += (mn + 1) * a[i];
			} else {
				res += mn * a[i];
				flag = true;
			}
		}
		printf("%lld\n", res);
	}
	return 0;
}