#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
const int N = 2e5 + 20;
int t, w, n, a[N];
typedef tree <
pair <int, int>, null_type,
less<pair <int, int>>, rb_tree_tag, tree_order_statistics_node_update >
ordered_set;
ordered_set s;
int main()
{
ios::sync_with_stdio(false);
cin >> t;
for(int tc = 1; tc <= t; tc++)
{
cin >> w >> n;
for(int i = 0; i < w; i++) cin >> a[i], a[i]--;
sort(a, a + w);
for(int i = w; i < 2 * w; i++) a[i] = a[i - w] + n;
int k = w;
w *= 2;
s.clear();
for(int i = 0; i < k; i++) s.insert({a[i], i});
ll old_m = (*s.find_by_order((k + 1) / 2 - 1)).first;
ll d = 0;
for(int i = 0; i < k; i++) d += abs(a[i] - old_m);
ll ans = d;
for(int i = k; i < w; i++)
{
s.erase(s.find_by_order(s.order_of_key({a[i - k], i - k})));
s.insert({a[i], i});
ll m = (*s.find_by_order((k + 1) / 2 - 1)).first;
d = d + abs(m - a[i]) - abs(old_m - a[i - k]);
if(k % 2 == 0) d -= (m - old_m);
old_m = m;
ans = min(ans, d);
}
cout << "Case #" << tc << ": " << ans << "\n";
}
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KdXNpbmcgbmFtZXNwYWNlIF9fZ251X3BiZHM7CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGxsIGxvbmcgbG9uZwoKY29uc3QgaW50IE4gPSAyZTUgKyAyMDsKCmludCB0LCB3LCBuLCBhW05dOwoKdHlwZWRlZiB0cmVlIDwKcGFpciA8aW50LCBpbnQ+LCBudWxsX3R5cGUsCiAgICAgbGVzczxwYWlyIDxpbnQsIGludD4+LCByYl90cmVlX3RhZywgdHJlZV9vcmRlcl9zdGF0aXN0aWNzX25vZGVfdXBkYXRlID4KICAgICBvcmRlcmVkX3NldDsKb3JkZXJlZF9zZXQgczsKCmludCBtYWluKCkKewogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwoKICAgIGNpbiA+PiB0OwogICAgZm9yKGludCB0YyA9IDE7IHRjIDw9IHQ7IHRjKyspCiAgICB7CiAgICAgICAgY2luID4+IHcgPj4gbjsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgdzsgaSsrKSBjaW4gPj4gYVtpXSwgYVtpXS0tOwogICAgICAgIHNvcnQoYSwgYSArIHcpOwogICAgICAgIGZvcihpbnQgaSA9IHc7IGkgPCAyICogdzsgaSsrKSBhW2ldID0gYVtpIC0gd10gKyBuOwoKICAgICAgICBpbnQgayA9IHc7CiAgICAgICAgdyAqPSAyOwogICAgICAgIHMuY2xlYXIoKTsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgazsgaSsrKSBzLmluc2VydCh7YVtpXSwgaX0pOwoKICAgICAgICBsbCBvbGRfbSA9ICgqcy5maW5kX2J5X29yZGVyKChrICsgMSkgLyAyIC0gMSkpLmZpcnN0OwogICAgICAgIGxsIGQgPSAwOwogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBrOyBpKyspIGQgKz0gYWJzKGFbaV0gLSBvbGRfbSk7CgogICAgICAgIGxsIGFucyA9IGQ7CiAgICAgICAgZm9yKGludCBpID0gazsgaSA8IHc7IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIHMuZXJhc2Uocy5maW5kX2J5X29yZGVyKHMub3JkZXJfb2Zfa2V5KHthW2kgLSBrXSwgaSAtIGt9KSkpOwogICAgICAgICAgICBzLmluc2VydCh7YVtpXSwgaX0pOwoKCiAgICAgICAgICAgIGxsIG0gPSAoKnMuZmluZF9ieV9vcmRlcigoayArIDEpIC8gMiAtIDEpKS5maXJzdDsKICAgICAgICAgICAgZCA9IGQgKyBhYnMobSAtIGFbaV0pIC0gYWJzKG9sZF9tIC0gYVtpIC0ga10pOwogICAgICAgICAgICBpZihrICUgMiA9PSAwKSBkIC09IChtIC0gb2xkX20pOwoKICAgICAgICAgICAgb2xkX20gPSBtOwogICAgICAgICAgICBhbnMgPSBtaW4oYW5zLCBkKTsKICAgICAgICB9CgogICAgICAgIGNvdXQgPDwgIkNhc2UgIyIgPDwgdGMgPDwgIjogIiA8PCBhbnMgPDwgIlxuIjsKICAgIH0KfQ==