// nEro
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll inf = 1e18;
const int N = 2 * 1e5 + 10;
ll n, k;
ll res;
ll a[N];
ll dp[N];
string s[N];
vector<string> v;
long long segtree[4 * N];
void build(int l , int r , int node){
if(l == r){
segtree[node] = s[l].size();
}
else{
int mid = l + r >> 1;
build(l , mid , node + node);
build(mid + 1 , r , node + node + 1);
ll can = min(segtree[node + node], segtree[node + node + 1]);
ll ans = 0;
for(int i = 0; i < can; ++i){
if(s[mid][i] != s[mid + 1][i]) break;
++ans;
}
segtree[node] = ans;
}
}
long long query(int l , int r , int node , int ql , int qr){
if(l > qr || r < ql){
return inf;
}
if(l >= ql && r <= qr){
return segtree[node];
}
int mid = l + r >> 1;
ll xx = query(l , mid , node + node , ql , qr);
ll yy = query(mid + 1 , r , node + node + 1 , ql , qr);
if(xx == inf) return yy;
if(yy == inf) return xx;
ll can = min(xx, yy);
ll ans = 0;
for(int i = 0; i < can; ++i){
if(s[mid][i] != s[mid + 1][i]) break;
++ans;
}
return ans;
}
bool ok(ll m, ll n){
for(int i = 1; i <= n; ++i){
if(i + k - 1 <= n){
if(query(1, n, 1, i, i + k - 1) >= m) return true;
}
}
return false;
}
void solve(){
ll ans = 0, gg = 0;
cin >> n >> k;
for(int i = 1; i <= n; ++i){
cin >> s[i];
gg = max(gg, (ll)s[i].size());
}
sort(s + 1, s + n + 1);
ll cur = n;
while(cur){
ll lo = -1, hi = gg + 1;
build(1, cur, 1);
while(lo + 1 < hi){
ll m = lo + hi >> 1LL;
if(ok(m, cur)) lo = m;
else hi = m;
}
if(lo <= 0) break;
for(int i = 1; i <= cur; ++i){
if(i + k - 1 <= cur){
if(query(1, cur, 1, i, i + k - 1) == lo){
ans += lo;
i += (k - 1);
}
else v.push_back(s[i]);
}
else v.push_back(s[i]);
}
for(int i = 1; i <= v.size(); ++i){
s[i] = v[i - 1];
}
cur = v.size();
v.clear();
gg = lo - 1;
}
cout << "Case #" << ++res << ": " << ans << "\n";
}
int main(int argc, char const *argv[]){
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
ll t = 1;
cin >> t;
while(t--){
solve();
v.clear();
}
}
// orEn
Ly8gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbkVybwojaW5jbHVkZSAiYml0cy9zdGRjKysuaCIKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAp1c2luZyBsbCA9IGxvbmcgbG9uZzsKIApjb25zdCBsbCBpbmYgPSAxZTE4Owpjb25zdCBpbnQgTiA9IDIgKiAxZTUgKyAxMDsKCmxsIG4sIGs7CmxsIHJlczsKbGwgYVtOXTsKbGwgZHBbTl07CgpzdHJpbmcgc1tOXTsKCnZlY3RvcjxzdHJpbmc+IHY7Cgpsb25nIGxvbmcgc2VndHJlZVs0ICogTl07Cgp2b2lkIGJ1aWxkKGludCBsICwgaW50IHIgLCBpbnQgbm9kZSl7CiAgICBpZihsID09IHIpewogICAgICAgIHNlZ3RyZWVbbm9kZV0gPSBzW2xdLnNpemUoKTsKICAgIH0KICAgIGVsc2V7CiAgICAgICAgaW50IG1pZCA9IGwgKyByID4+IDE7CiAgICAgICAgYnVpbGQobCAsIG1pZCAsIG5vZGUgKyBub2RlKTsKICAgICAgICBidWlsZChtaWQgKyAxICwgciAsIG5vZGUgKyBub2RlICsgMSk7CiAgICAgICAgbGwgY2FuID0gbWluKHNlZ3RyZWVbbm9kZSArIG5vZGVdLCBzZWd0cmVlW25vZGUgKyBub2RlICsgMV0pOwogICAgICAgIGxsIGFucyA9IDA7CiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IGNhbjsgKytpKXsKICAgICAgICAgICAgaWYoc1ttaWRdW2ldICE9IHNbbWlkICsgMV1baV0pICAgIGJyZWFrOwogICAgICAgICAgICArK2FuczsKICAgICAgICB9CiAgICAgICAgc2VndHJlZVtub2RlXSA9IGFuczsKICAgIH0KfQoKbG9uZyBsb25nIHF1ZXJ5KGludCBsICwgaW50IHIgLCBpbnQgbm9kZSAsIGludCBxbCAsIGludCBxcil7CiAgICBpZihsID4gcXIgfHwgciA8IHFsKXsKICAgICAgICByZXR1cm4gaW5mOwogICAgfQogICAgaWYobCA+PSBxbCAmJiByIDw9IHFyKXsKICAgICAgICByZXR1cm4gc2VndHJlZVtub2RlXTsKICAgIH0KICAgIGludCBtaWQgPSBsICsgciA+PiAxOwogICAgbGwgeHggPSBxdWVyeShsICwgbWlkICwgbm9kZSArIG5vZGUgLCBxbCAsIHFyKTsKICAgIGxsIHl5ID0gcXVlcnkobWlkICsgMSAsIHIgLCBub2RlICsgbm9kZSArIDEgLCBxbCAsIHFyKTsKICAgIGlmKHh4ID09IGluZikgICByZXR1cm4geXk7CiAgICBpZih5eSA9PSBpbmYpICAgcmV0dXJuIHh4OwogICAgbGwgY2FuID0gbWluKHh4LCB5eSk7CiAgICBsbCBhbnMgPSAwOwogICAgZm9yKGludCBpID0gMDsgaSA8IGNhbjsgKytpKXsKICAgICAgICBpZihzW21pZF1baV0gIT0gc1ttaWQgKyAxXVtpXSkgICAgYnJlYWs7CiAgICAgICAgKythbnM7CiAgICB9CiAgICByZXR1cm4gYW5zOwp9Cgpib29sIG9rKGxsIG0sIGxsIG4pewogICAgZm9yKGludCBpID0gMTsgaSA8PSBuOyArK2kpewogICAgICAgIGlmKGkgKyBrIC0gMSA8PSBuKXsKICAgICAgICAgICAgaWYocXVlcnkoMSwgbiwgMSwgaSwgaSArIGsgLSAxKSA+PSBtKSAgIHJldHVybiB0cnVlOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBmYWxzZTsKfQoKdm9pZCBzb2x2ZSgpewogICAgbGwgYW5zID0gMCwgZ2cgPSAwOwogICAgY2luID4+IG4gPj4gazsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgKytpKXsKICAgICAgICBjaW4gPj4gc1tpXTsKICAgICAgICBnZyA9IG1heChnZywgKGxsKXNbaV0uc2l6ZSgpKTsKICAgIH0KICAgIHNvcnQocyArIDEsIHMgKyBuICsgMSk7CiAgICBsbCBjdXIgPSBuOwogICAgd2hpbGUoY3VyKXsKICAgICAgICBsbCBsbyA9IC0xLCBoaSA9IGdnICsgMTsKICAgICAgICBidWlsZCgxLCBjdXIsIDEpOwogICAgICAgIHdoaWxlKGxvICsgMSA8IGhpKXsKICAgICAgICAgICAgbGwgbSA9IGxvICsgaGkgPj4gMUxMOwogICAgICAgICAgICBpZihvayhtLCBjdXIpKSAgIGxvID0gbTsKICAgICAgICAgICAgZWxzZSAgICBoaSA9IG07CiAgICAgICAgfQogICAgICAgIGlmKGxvIDw9IDApIGJyZWFrOyAgIAogICAgICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gY3VyOyArK2kpewogICAgICAgICAgICBpZihpICsgayAtIDEgPD0gY3VyKXsKICAgICAgICAgICAgICAgIGlmKHF1ZXJ5KDEsIGN1ciwgMSwgaSwgaSArIGsgLSAxKSA9PSBsbyl7CiAgICAgICAgICAgICAgICAgICAgYW5zICs9IGxvOwogICAgICAgICAgICAgICAgICAgIGkgKz0gKGsgLSAxKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgICAgdi5wdXNoX2JhY2soc1tpXSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSAgICB2LnB1c2hfYmFjayhzW2ldKTsKICAgICAgICB9CiAgICAgICAgZm9yKGludCBpID0gMTsgaSA8PSB2LnNpemUoKTsgKytpKXsKICAgICAgICAgICAgc1tpXSA9IHZbaSAtIDFdOwogICAgICAgIH0KICAgICAgICBjdXIgPSB2LnNpemUoKTsKICAgICAgICB2LmNsZWFyKCk7CiAgICAgICAgZ2cgPSBsbyAtIDE7CiAgICB9CgogICAgY291dCA8PCAiQ2FzZSAjIiA8PCArK3JlcyA8PCAiOiAiIDw8IGFucyA8PCAiXG4iOwp9CiAKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgY29uc3QgKmFyZ3ZbXSl7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApO2Npbi50aWUoTlVMTCk7Y291dC50aWUoTlVMTCk7CiAgICBsbCB0ID0gMTsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUodC0tKXsKICAgICAgICBzb2x2ZSgpOwogICAgICAgIHYuY2xlYXIoKTsKICAgIH0KfQovLyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvckVu