#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair < int , int > Point;
typedef pair < Point,int > iii;
vector < iii > Comb;
int main()
{
freopen("this.in","r+",stdin);
freopen("that.txt","w+",stdout);
int T;
scanf("%d",&T);
for(int e=1;e<=T;++e)
{
cout<<"Case #"<<e<<": ";
Comb.clear();
int J , P , S , K , i , k , l ;
cin>>J>>P>>S>>K;
for(i=1;i<=J;++i)
for(k=1;k<=P;++k)
for(l=1;l<=S;++l)
Comb.push_back({{i,k},l});
int high = 0 , idx = 0;
for(i=1;i<(1LL<<Comb.size());++i)
{
if(__builtin_popcount(i)<=high)
continue;
int M1[4][4]={0};
int M2[4][4]={0};
int M3[4][4]={0};
int flag = 0;
for(int j=0;j<(int)Comb.size();++j)
{
if((i>>j)&1)
{
M1[Comb[j].first.first][Comb[j].first.second]++; // 1 2
M2[Comb[j].first.first][Comb[j].second]++; // 1 3
M3[Comb[j].first.second][Comb[j].second]++; // 2 3
if(M1[Comb[j].first.first][Comb[j].first.second]>K || M2[Comb[j].first.first][Comb[j].second]>K || M3[Comb[j].first.second][Comb[j].second]>K)
{
flag =1;
break;
}
}
}
if(flag)continue;
high = __builtin_popcount(i);
idx = i;
}
cout<<high<<'\n';
i = idx;
for(int j=0;j<(int)Comb.size();++j)
{
if((i>>j)&1)
cout<<Comb[j].first.first<<" "<<Comb[j].first.second<<" "<<Comb[j].second<<"\n";
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBsb25nIGxvbmcgTEw7CnR5cGVkZWYgcGFpciA8IGludCAsIGludCA+IFBvaW50Owp0eXBlZGVmIHBhaXIgPCBQb2ludCxpbnQgPiBpaWk7CnZlY3RvciA8IGlpaSA+IENvbWI7CgoKaW50IG1haW4oKQp7CiAgICBmcmVvcGVuKCJ0aGlzLmluIiwicisiLHN0ZGluKTsKICAgIGZyZW9wZW4oInRoYXQudHh0IiwidysiLHN0ZG91dCk7CiAgICBpbnQgVDsKICAgIHNjYW5mKCIlZCIsJlQpOwogICAgZm9yKGludCBlPTE7ZTw9VDsrK2UpCiAgICB7CiAgICAgICAgY291dDw8IkNhc2UgIyI8PGU8PCI6ICI7CiAgICAgICAgQ29tYi5jbGVhcigpOwogICAgICAgIGludCBKICwgUCAsIFMgLCBLICwgaSAsIGsgLCBsIDsKICAgICAgICBjaW4+Pko+PlA+PlM+Pks7CgogICAgICAgIGZvcihpPTE7aTw9SjsrK2kpCiAgICAgICAgICAgIGZvcihrPTE7azw9UDsrK2spCiAgICAgICAgICAgICAgICBmb3IobD0xO2w8PVM7KytsKQogICAgICAgICAgICAgICAgICAgIENvbWIucHVzaF9iYWNrKHt7aSxrfSxsfSk7CgogICAgICAgIGludCBoaWdoID0gMCAsIGlkeCA9IDA7CgogICAgICAgIGZvcihpPTE7aTwoMUxMPDxDb21iLnNpemUoKSk7KytpKQogICAgICAgIHsKICAgICAgICAgICAgaWYoX19idWlsdGluX3BvcGNvdW50KGkpPD1oaWdoKQogICAgICAgICAgICAgICAgY29udGludWU7CgogICAgICAgICAgICBpbnQgTTFbNF1bNF09ezB9OwogICAgICAgICAgICBpbnQgTTJbNF1bNF09ezB9OwogICAgICAgICAgICBpbnQgTTNbNF1bNF09ezB9OwoKICAgICAgICAgICAgaW50IGZsYWcgPSAwOwogICAgICAgICAgICBmb3IoaW50IGo9MDtqPChpbnQpQ29tYi5zaXplKCk7KytqKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZigoaT4+aikmMSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBNMVtDb21iW2pdLmZpcnN0LmZpcnN0XVtDb21iW2pdLmZpcnN0LnNlY29uZF0rKzsgLy8gMSAyCiAgICAgICAgICAgICAgICAgICAgTTJbQ29tYltqXS5maXJzdC5maXJzdF1bQ29tYltqXS5zZWNvbmRdKys7ICAgICAgIC8vIDEgMwogICAgICAgICAgICAgICAgICAgIE0zW0NvbWJbal0uZmlyc3Quc2Vjb25kXVtDb21iW2pdLnNlY29uZF0rKzsgLy8gMiAzCiAgICAgICAgICAgICAgICAgICAgaWYoTTFbQ29tYltqXS5maXJzdC5maXJzdF1bQ29tYltqXS5maXJzdC5zZWNvbmRdPksgIHx8IE0yW0NvbWJbal0uZmlyc3QuZmlyc3RdW0NvbWJbal0uc2Vjb25kXT5LIHx8IE0zW0NvbWJbal0uZmlyc3Quc2Vjb25kXVtDb21iW2pdLnNlY29uZF0+SykKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGZsYWcgPTE7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZihmbGFnKWNvbnRpbnVlOwogICAgICAgICAgICBoaWdoID0gX19idWlsdGluX3BvcGNvdW50KGkpOwogICAgICAgICAgICBpZHggID0gaTsKICAgICAgICB9CgoKICAgICAgICBjb3V0PDxoaWdoPDwnXG4nOwogICAgICAgIGkgPSBpZHg7CiAgICAgICAgZm9yKGludCBqPTA7ajwoaW50KUNvbWIuc2l6ZSgpOysraikKICAgICAgICB7CiAgICAgICAgICAgIGlmKChpPj5qKSYxKQogICAgICAgICAgICAgICAgY291dDw8Q29tYltqXS5maXJzdC5maXJzdDw8IiAiPDxDb21iW2pdLmZpcnN0LnNlY29uZDw8IiAiPDxDb21iW2pdLnNlY29uZDw8IlxuIjsKICAgICAgICB9CgogICAgfQogICAgcmV0dXJuIDA7Cn0K