#include <bits/stdc++.h>

using namespace std;

void testCase()
{
	long long n, x;
	cin >> n >> x;
	deque<pair<long long, long long>> a(n);
	for (long long i = 0, ai; i < n; i++)
	{
		cin >> ai;
		a.push_back({ai, 1});
	}
	long long sum = 0;
	for (long long ai; !a.empty() && (ai = a.front().first) % x == 0; a.pop_front())
	{
		a.push_back({ai / x, a.front().second * x});
		sum += ai * a.front().second;
	}
	for (long long i = 0; i < a.size(); i++)
	{
		sum += a[i].first * a[i].second;
	}
	cout << sum << '\n';
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int t;
	cin >> t;
	while (t--)
		testCase();
}