#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
typedef std::pair<int, ll> pii;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = (ll)1e18;
const double INFD = 1e30;
const double EPS = 1e-9;
const double PI = std::acos(-1);
const int MOD = 1e9 + 7;
template <typename T>
inline T read() {
    T X = 0, w = 0;
    char ch = 0;
    while (!isdigit(ch)) {
        w |= ch == '-';
        ch = getchar();
    }
    while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
const int MAXN = 100005 * 64;
const int MAXV = (1 << 17) + 5;
int n, m, k;
int dr[4] = {0, 1, 0, -1};
int dc[4] = {-1, 0, 1, 0};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
pii A[MAXN];
char S[MAXN];
void solve() {
    scanf("%d%d", &n, &m);
    int end = n;
    ll sum = 0;
    for (int i = 1; i <= n; i++) {
        A[i].first = read<int>();
        A[i].second = 1;
        sum += A[i].first;
    }
    for (int i = 1; i <= end; i++) {
        if (A[i].first % m == 0) {
            A[++end] = {A[i].first / m, (ll)A[i].second * m};
            sum += (ll)A[i].first * A[i].second;
        } else {
            break;
        }
    }
    printf("%lld\n", sum);
}
int main() {
#ifdef LOCALLL
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif
    int T = read<int>();
    while (T--) {
        solve();
    }
    return 0;
}