#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int t;
cin>>t;
int u=1;
while(t--)
{
int n,k,maxm=0;
cin>>n>>k;
cout<<"Case "<<u++<<":"<<endl;
unordered_map<string ,int> m;
queue<string> q;
set<string> v[n+1];
string s,l,p,prev;
for(int i=0;i<n;i++)
{
cin>>s;
if(q.size()>=k)
{
p=q.front();
q.pop();
v[m[p]].erase(p);
m[p]--;
if(maxm==m[p]+1)
{
if(v[maxm].size()==0)
maxm=m[p];
}
v[m[p]].insert(p);
}
q.push(s);
m[s]++;
v[m[s]].insert(s);
if(m[s]>maxm)
{
l=s;
maxm=m[s];
}
if(m[p]>maxm)
{
maxm=m[p];
l=p;
}
set<string>::iterator it=v[maxm].begin();
cout<<*it<<" "<<maxm<<endl;
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKaW50IG1haW4oKSB7Cglpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKCWludCB0OwoJY2luPj50OwoJaW50IHU9MTsKCXdoaWxlKHQtLSkKCXsKCSAgICBpbnQgbixrLG1heG09MDsKCSAgICBjaW4+Pm4+Pms7CgkgICAgY291dDw8IkNhc2UgIjw8dSsrPDwiOiI8PGVuZGw7CgkgICAgdW5vcmRlcmVkX21hcDxzdHJpbmcgLGludD4gbTsKCSAgICBxdWV1ZTxzdHJpbmc+IHE7CgkgICAgc2V0PHN0cmluZz4gdltuKzFdOwoJICAgIHN0cmluZyBzLGwscCxwcmV2OwoJICAgIGZvcihpbnQgaT0wO2k8bjtpKyspCgkgICAgewoJICAgIAljaW4+PnM7CgkgICAgICAgIGlmKHEuc2l6ZSgpPj1rKQoJICAgICAgICB7CgkgICAgICAgIAlwPXEuZnJvbnQoKTsKCSAgICAgICAgICAgIHEucG9wKCk7CgkgICAgICAgICAgICB2W21bcF1dLmVyYXNlKHApOwoJICAgICAgICAgICAgbVtwXS0tOwoJICAgICAgICAgICAgaWYobWF4bT09bVtwXSsxKQoJICAgICAgICAgICAgewoJICAgICAgICAgICAgCWlmKHZbbWF4bV0uc2l6ZSgpPT0wKQoJICAgICAgICAgICAgCW1heG09bVtwXTsKCSAgICAgICAgICAgIH0KCSAgICAgICAgICAgIAoJICAgICAgICAgICAgdlttW3BdXS5pbnNlcnQocCk7CgkgICAgICAgICAgICAKCSAgICAgICAgfQoJICAgICAgICBxLnB1c2gocyk7CgkgICAgICAgIG1bc10rKzsKCSAgICAgICAgdlttW3NdXS5pbnNlcnQocyk7CgkgICAgICAgIGlmKG1bc10+bWF4bSkKCSAgICAgICAgewoJICAgICAgICAJbD1zOwoJICAgICAgICAgICAgbWF4bT1tW3NdOwoJICAgICAgICB9CgkgICAgICAgIGlmKG1bcF0+bWF4bSkKCSAgICAgICAgewoJICAgICAgICAJbWF4bT1tW3BdOwoJICAgICAgICAgICAgbD1wOwoJICAgICAgICB9CgkgICAgIAoJICAgICAgICBzZXQ8c3RyaW5nPjo6aXRlcmF0b3IgaXQ9dlttYXhtXS5iZWdpbigpOwoJICAgICAgICBjb3V0PDwqaXQ8PCIgIjw8bWF4bTw8ZW5kbDsKCSAgICB9Cgl9CglyZXR1cm4gMDsKfSA=