#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        int n, x;
        ll sum = 0;
        cin >> n >> x;
        int a[n];
        int b[n];
        for (int i = 0; i < n; i++)
        {
            int y;
            cin >> y;
            a[i] = y;
            b[i] = 1;
            sum += y;
        }
        bool flag = true;
        while (flag)
        {
            for (int i = 0; i < n; i++)
            {
                if (a[i] % x == 0)
                    {sum += a[i] * b[i]; a[i] /= x; b[i] = b[i] * x;}
                else {flag = false; i = n;}
            }
        }
        cout << sum << endl;
    }
}
