//#include<algorithm>
//#include<iostream>
//#include<sstream>
//#include<cstring>
//#include<cstdio>
//#include<cctype>
//#include<string>
//#include<vector>
//#include<queue>
//#include<stack>
//#include<cmath>
//#include<map>
//#include<set>
//using namespace std;
//#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#define lowbit(x) ((x) & -(x))
//#define ls (rt << 1)
//#define rs (rt << 1 | 1)
//#define int long long
//
//inline int _() {
//	int f = 1, x = 0; char ch;
//	do { ch = getchar(); if (ch == '-')f = -1; } while (ch < '0' || ch>'9');
//	do { x = x * 10 + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9');
//	return f * x;
//}
//
//const int N = 500 + 9;
//const int INF = 0x3f3f3f3f3f3f3f3f;
//const int MOD = 998244353;
//
//int a[10];
//int cnt1, cnt2, cnt3, ans;
//
//int l, r;
//
//signed main() {
//	IOS;
//
//	l = 1;
//	r = 4;
//
//	for (a[1] = l; a[1] <= r; a[1]++) {
//		for (a[2] = l; a[2] <= r; a[2]++) {
//			for (a[3] = l; a[3] <= r; a[3]++) {
//				for (a[4] = l; a[4] <= r; a[4]++) {
//
//					int cnt = 0;
//					for (int u = 1; u <= 4; u++) {
//						if (a[u] & 1) cnt++;
//					}
//
//					if (~cnt & 1) {
//						if (a[1] == 1) cnt1++;
//						else if (a[1] == 2) cnt2++;
//						else if (a[1] == 3) cnt3++;
//					}
//				}
//			}
//		}
//	}
//
//	printf("%lld %lld %lld\n", cnt1, cnt2, cnt3);
//	cout << ans << endl;
//
//	return 0;
//}

#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
using namespace std;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lowbit(x) ((x) & -(x))
#define ls (rt << 1)
#define rs (rt << 1 | 1)
#define int long long

inline int _() {
	int f = 1, x = 0; char ch;
	do { ch = getchar(); if (ch == '-')f = -1; } while (ch < '0' || ch>'9');
	do { x = x * 10 + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9');
	return f * x;
}

const int INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 998244353;
const int N = 3e5 + 9;

int n, a[N], x, cnt[N];

void solve() {
	int ans = 0;

	cin >> n >> x;
	for (int i = 1; i <= n; i++)
		cin >> a[i], cnt[i] = 1, ans += a[i];

	for (int i = 1; ; i++) {
		if (i == n + 1) i = 1;

		if (a[i] % x == 0) {
			ans += x * cnt[i] * (a[i] / x);
			cnt[i] *= x;
			a[i] = a[i] / x;
		}
		else break;
	}

	cout << ans << endl;
	return;
}
signed main() {
	IOS;
	int T; cin >> T;
	while (T--)
	solve();
	return 0;
}