#include <bits/stdc++.h>
// #include "bits/stdc++.h"
#ifndef bhupixb
#define var(...)
#define stl(...)
#endif
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = a; i <= (int)b; ++i)
int w, n;
void solve() {
cin >> w >> n;
map<int,int> mp;
rep(i,1,w) {
int x;
cin >> x;
mp[x]++;
}
vector<array<int,2>> a;
for (auto it: mp) {
a.push_back({it.first, it.second});
}
int N = a.size();
const int inf = 1e15;
vector<int> dp(N, inf);
vector<int> pref_sum(N), cnt(N);
for (int i = 0; i < N; ++i) {
pref_sum[i] = a[i][0] * a[i][1];
cnt[i] = a[i][1];
if (i > 0) {
cnt[i] += cnt[i-1];
pref_sum[i] += pref_sum[i - 1];
}
}
auto getLR = [&] (int l, int r, vector<int> &pref_sum) {
if (l > r) return 0LL;
if (!(l >= 0 and r < N)) {
var(l, r);
exit(0);
}
if (!l) {
return pref_sum[r];
}
return pref_sum[r] - pref_sum[l-1];
};
int res = 1e18;
for (int i = 0; i < N; ++i) {
int ans = 0;
{
int lo = 0, hi = i - 1;
int lastJ = i;
while (lo <= hi) {
int mid = (lo + hi) / 2;
int j = mid;
int diff = a[i][0] - a[j][0];
int cnt1 = a[j][1];
int seedha = diff * a[j][1];
int ulta = ((n - a[i][0]) + (a[j][0])) * cnt1;
if (seedha <= ulta) {
hi = mid - 1;
lastJ = mid;
}
else {
lo = mid + 1;
}
}
if (lastJ != i) {
int ct = getLR(lastJ, i-1, cnt);
int sum = getLR(lastJ, i - 1, pref_sum);
int ok = a[i][0] * ct - sum;
ans += ok;
}
if (lastJ > 0) {
int j = lastJ - 1;
int ct = getLR(0, j, cnt);
int ok = 0;
ok += ct * (n - a[i][0]);
ok += getLR(0, j, pref_sum);
var(ok);
ans += ok;
}
}
{
int lo = i + 1, hi = N - 1;
int lastJ = i;
while (lo <= hi) {
int mid = (lo + hi) / 2;
int j = mid;
int diff = a[j][0] - a[i][0];
int seedha = diff * a[j][1];
int ulta = (n - a[j][0]) * a[j][1] + (a[i][0]) * a[j][1];
if (seedha <= ulta) {
lastJ = mid;
lo = mid + 1;
}
else {
hi = mid - 1;
}
}
if (lastJ != i) {
int ct = getLR(i+1, lastJ, cnt);
int sum = getLR(i+1, lastJ, pref_sum);
int ok = sum - ct * a[i][0];
ans += ok;
}
if (lastJ < N - 1) {
int j = lastJ + 1;
int ct = getLR(j, N - 1, cnt);
int sum = getLR(j, N - 1, pref_sum);
int ok = 0;
ok += n * ct - sum + ct * a[i][0];
ans += ok;
}
}
res = min(ans, res);
}
cout << res << '\n';
}
// #define single_test
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
#ifndef single_test
cin >> t;
#endif
for (int i = 1; i <= t; ++i) {
cout << "Case #" << i << ": ";
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Ci8vICNpbmNsdWRlICJiaXRzL3N0ZGMrKy5oIgoKI2lmbmRlZiBiaHVwaXhiCiAgICAjZGVmaW5lIHZhciguLi4pCiAgICAjZGVmaW5lIHN0bCguLi4pCiNlbmRpZgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgaW50IGxvbmcgbG9uZwoKI2RlZmluZSByZXAoaSwgYSwgYikgZm9yIChpbnQgaSA9IGE7IGkgPD0gKGludCliOyArK2kpCgppbnQgdywgbjsKCnZvaWQgc29sdmUoKSB7CiAgICBjaW4gPj4gdyA+PiBuOwogICAgbWFwPGludCxpbnQ+IG1wOwogICAgcmVwKGksMSx3KSB7CiAgICAgICAgaW50IHg7CiAgICAgICAgY2luID4+IHg7CiAgICAgICAgbXBbeF0rKzsKICAgIH0KICAgIHZlY3RvcjxhcnJheTxpbnQsMj4+IGE7CiAgICBmb3IgKGF1dG8gaXQ6IG1wKSB7CiAgICAgICAgYS5wdXNoX2JhY2soe2l0LmZpcnN0LCBpdC5zZWNvbmR9KTsKICAgIH0KICAgIGludCBOID0gYS5zaXplKCk7CiAgICBjb25zdCBpbnQgaW5mID0gMWUxNTsKICAgIHZlY3RvcjxpbnQ+IGRwKE4sIGluZik7CiAgICB2ZWN0b3I8aW50PiBwcmVmX3N1bShOKSwgY250KE4pOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBOOyArK2kpIHsKICAgICAgICBwcmVmX3N1bVtpXSA9IGFbaV1bMF0gKiBhW2ldWzFdOwogICAgICAgIGNudFtpXSA9IGFbaV1bMV07CiAgICAgICAgaWYgKGkgPiAwKSB7CiAgICAgICAgICAgIGNudFtpXSArPSBjbnRbaS0xXTsKICAgICAgICAgICAgcHJlZl9zdW1baV0gKz0gcHJlZl9zdW1baSAtIDFdOwogICAgICAgfQoKICAgIH0KICAgIGF1dG8gZ2V0TFIgPSBbJl0gKGludCBsLCBpbnQgciwgdmVjdG9yPGludD4gJnByZWZfc3VtKSB7CiAgICAgICAgaWYgKGwgPiByKSByZXR1cm4gMExMOwogICAgICAgIGlmICghKGwgPj0gMCBhbmQgciA8IE4pKSB7CiAgICAgICAgICAgIHZhcihsLCByKTsKICAgICAgICAgICAgZXhpdCgwKTsKICAgICAgICB9CiAgICAgICAgaWYgKCFsKSB7CiAgICAgICAgICAgIHJldHVybiBwcmVmX3N1bVtyXTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHByZWZfc3VtW3JdIC0gcHJlZl9zdW1bbC0xXTsKICAgIH07CiAgICBpbnQgcmVzID0gMWUxODsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgTjsgKytpKSB7CiAgICAgICAgaW50IGFucyA9IDA7CiAgICAgICAgewogICAgICAgICAgICBpbnQgbG8gPSAwLCBoaSA9IGkgLSAxOwogICAgICAgICAgICBpbnQgbGFzdEogPSBpOwogICAgICAgICAgICB3aGlsZSAobG8gPD0gaGkpIHsKICAgICAgICAgICAgICAgIGludCBtaWQgPSAobG8gKyBoaSkgLyAyOwogICAgICAgICAgICAgICAgaW50IGogPSBtaWQ7CiAgICAgICAgICAgICAgICBpbnQgZGlmZiA9IGFbaV1bMF0gLSBhW2pdWzBdOwogICAgICAgICAgICAgICAgaW50IGNudDEgPSBhW2pdWzFdOwogICAgICAgICAgICAgICAgaW50IHNlZWRoYSA9IGRpZmYgKiBhW2pdWzFdOwogICAgICAgICAgICAgICAgaW50IHVsdGEgPSAoKG4gLSBhW2ldWzBdKSArIChhW2pdWzBdKSkgKiBjbnQxOwogICAgICAgICAgICAgICAgaWYgKHNlZWRoYSA8PSB1bHRhKSB7CiAgICAgICAgICAgICAgICAgICAgaGkgPSBtaWQgLSAxOwogICAgICAgICAgICAgICAgICAgIGxhc3RKID0gbWlkOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgbG8gPSBtaWQgKyAxOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChsYXN0SiAhPSBpKSB7CiAgICAgICAgICAgICAgICBpbnQgY3QgPSBnZXRMUihsYXN0SiwgaS0xLCBjbnQpOwogICAgICAgICAgICAgICAgaW50IHN1bSA9IGdldExSKGxhc3RKLCBpIC0gMSwgcHJlZl9zdW0pOwogICAgICAgICAgICAgICAgaW50IG9rID0gYVtpXVswXSAqIGN0IC0gc3VtOwogICAgICAgICAgICAgICAgYW5zICs9IG9rOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChsYXN0SiA+IDApIHsKICAgICAgICAgICAgICAgIGludCBqID0gbGFzdEogLSAxOwogICAgICAgICAgICAgICAgaW50IGN0ID0gZ2V0TFIoMCwgaiwgY250KTsKICAgICAgICAgICAgICAgIGludCBvayA9IDA7CiAgICAgICAgICAgICAgICBvayArPSBjdCAqIChuIC0gYVtpXVswXSk7CiAgICAgICAgICAgICAgICBvayArPSBnZXRMUigwLCBqLCBwcmVmX3N1bSk7CiAgICAgICAgICAgICAgICB2YXIob2spOwogICAgICAgICAgICAgICAgYW5zICs9IG9rOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHsKICAgICAgICAgICAgaW50IGxvID0gaSArIDEsIGhpID0gTiAtIDE7CiAgICAgICAgICAgIGludCBsYXN0SiA9IGk7CiAgICAgICAgICAgIHdoaWxlIChsbyA8PSBoaSkgewogICAgICAgICAgICAgICAgaW50IG1pZCA9IChsbyArIGhpKSAvIDI7CiAgICAgICAgICAgICAgICBpbnQgaiA9IG1pZDsKICAgICAgICAgICAgICAgIGludCBkaWZmID0gYVtqXVswXSAtIGFbaV1bMF07CiAgICAgICAgICAgICAgICBpbnQgc2VlZGhhID0gZGlmZiAqIGFbal1bMV07CiAgICAgICAgICAgICAgICBpbnQgdWx0YSA9IChuIC0gYVtqXVswXSkgKiBhW2pdWzFdICsgKGFbaV1bMF0pICogYVtqXVsxXTsKICAgICAgICAgICAgICAgIGlmIChzZWVkaGEgPD0gdWx0YSkgewogICAgICAgICAgICAgICAgICAgIGxhc3RKID0gbWlkOwogICAgICAgICAgICAgICAgICAgIGxvID0gbWlkICsgMTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgICAgIGhpID0gbWlkIC0gMTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAobGFzdEogIT0gaSkgewogICAgICAgICAgICAgICBpbnQgY3QgPSBnZXRMUihpKzEsIGxhc3RKLCBjbnQpOwogICAgICAgICAgICAgICBpbnQgc3VtID0gZ2V0TFIoaSsxLCBsYXN0SiwgcHJlZl9zdW0pOwogICAgICAgICAgICAgICBpbnQgb2sgPSBzdW0gLSBjdCAqIGFbaV1bMF07CiAgICAgICAgICAgICAgIGFucyArPSBvazsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAobGFzdEogPCBOIC0gMSkgewogICAgICAgICAgICAgICAgaW50IGogPSBsYXN0SiArIDE7CiAgICAgICAgICAgICAgICBpbnQgY3QgPSBnZXRMUihqLCBOIC0gMSwgY250KTsKICAgICAgICAgICAgICAgIGludCBzdW0gPSBnZXRMUihqLCBOIC0gMSwgcHJlZl9zdW0pOwogICAgICAgICAgICAgICAgaW50IG9rID0gMDsKICAgICAgICAgICAgICAgIG9rICs9IG4gKiBjdCAtIHN1bSArIGN0ICogYVtpXVswXTsKICAgICAgICAgICAgICAgIGFucyArPSBvazsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXMgPSBtaW4oYW5zLCByZXMpOwogICAgfQogICAgY291dCA8PCByZXMgPDwgJ1xuJzsKfQoKLy8gI2RlZmluZSBzaW5nbGVfdGVzdAoKc2lnbmVkIG1haW4oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKICAgIGludCB0ID0gMTsKICAgICNpZm5kZWYgc2luZ2xlX3Rlc3QKICAgICAgICBjaW4gPj4gdDsKICAgICNlbmRpZgogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gdDsgKytpKSB7CiAgICAgICAgY291dCA8PCAiQ2FzZSAjIiA8PCBpIDw8ICI6ICI7CiAgICAgICAgc29sdmUoKTsKICAgIH0KICAgIHJldHVybiAwOwp9