#include <bits/stdc++.h>
#define FIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define FOR(a,b,c) for(int a = (b); a <= (c); a++)
#define RFOR(a,b,c) for(int a = (b); a >= (c); a--)
#define all(x) (x).begin(), (x).end()
#define vi vector <int>
#define vvi vector <vi>
#define ll long long
#define vll vector <ll>
#define vvl vector <vll>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define eb emplace_back
#define ep emplace
#define fs first
#define sd second
#define mod 998244353
#define EPS 1e-9
using namespace std;

int main() {
	FIO;
	int t; cin >> t; while (t--) {
		int n, x;
		cin >> n >> x;

		queue <pair<int, int>> q;
		FOR(i, 1, n) {
			int a; cin >> a;
			q.push({ a,a });
		}

		ll ans = 0;

		while (q.size()) {
			auto a = q.front();
			q.pop();
			ans += a.sd;
			if (a.fs % x) break;
			
			q.push({ a.fs / x, a.sd });
		}
		while (q.size()) {
			ans += q.front().sd;
			q.pop();
		}

		cout << ans << '\n';
	}
	return 0;
}