#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
void solve() {
ll n , k;
cin >> n >> k;
vector<ll> v(n + 1);
for (int i = 1 ; i <= n ; i++)
cin >> v[i];
vector<vector<ll>> dp(n + 1, vector<ll> (45));
for (int i = 1 ; i <= n ; i++) {
dp[i][0] = dp[i-1][0] - k + v[i];
for (int j = 1 ; j < 40 ; j++) {
dp[i][j] = max(dp[i-1][j] - k + (v[i] / (1ll << j)) ,
dp[i-1][j-1] + (v[i] / (1ll << j)));
}
}
cout << *max_element(dp[n].begin(), dp[n].end()) << endl;
}
int32_t main() {
int t = 1;
cin >> t;
while(t--) {
solve();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBzb2x2ZSgpIHsKICAgIGxsIG4gLCBrOwogICAgY2luID4+IG4gPj4gazsKICAgIHZlY3RvcjxsbD4gdihuICsgMSk7CiAgICBmb3IgKGludCBpID0gMSA7IGkgPD0gbiA7IGkrKykKICAgICAgICBjaW4gPj4gdltpXTsKICAgIHZlY3Rvcjx2ZWN0b3I8bGw+PiBkcChuICsgMSwgdmVjdG9yPGxsPiAoNDUpKTsKCiAgICBmb3IgKGludCBpID0gMSA7IGkgPD0gbiA7IGkrKykgewogICAgICAgIGRwW2ldWzBdID0gZHBbaS0xXVswXSAtIGsgKyB2W2ldOwogICAgICAgIGZvciAoaW50IGogPSAxIDsgaiA8IDQwIDsgaisrKSB7CiAgICAgICAgICAgIGRwW2ldW2pdID0gbWF4KGRwW2ktMV1bal0gLSBrICsgKHZbaV0gLyAoMWxsIDw8IGopKSAsIAogICAgICAgICAgICAgICAgICAgICAgICAgICBkcFtpLTFdW2otMV0gKyAodltpXSAvICgxbGwgPDwgaikpKTsKICAgICAgICB9CiAgICB9CgogICAgY291dCA8PCAqbWF4X2VsZW1lbnQoZHBbbl0uYmVnaW4oKSwgZHBbbl0uZW5kKCkpIDw8IGVuZGw7Cn0KCmludDMyX3QgbWFpbigpIHsKICAgIGludCB0ID0gMTsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUodC0tKSB7CiAgICAgICAgc29sdmUoKTsKICAgIH0KfQ==