#include <iostream>
#include <string>
using namespace std;
static int n;
static int* sumBloodArr;
static int (*manBloodArr)[5];
const string manName[5] = {"dragon","ninja","iceman","lion","wolf"};
class Red {
public:
int redNo = 0;
int manCount[5] = {0,0,0,0,0};
int manBlood[5] = { 2,3,4,1,0 };
int sumBlood;
int startMan=0;
int t;
Red(int n):sumBlood(n) {}
int makeMan(int nown,string str) {
int i = 0;
for (i;i < 5;++i) {
t = startMan % 5;
if (sumBlood < manBloodArr[nown][manBlood[t]]) {
++startMan;
continue;
}
++startMan;
++redNo;
++manCount[t];
sumBlood -= manBloodArr[nown][manBlood[t]];
cout << str << " red " << manName[manBlood[t]] << " " << redNo << " born with strength " << manBloodArr[nown][manBlood[t]] << "," << manCount[t] << " " << manName[manBlood[t]] << " in red headquarter" << endl;
return 1;
}
if (i >= 5) {
cout << str << " red headquarter stops making warriors" << endl;
return 0;
}
}
};
class Blue {
public:
int blueNo =0;
int manCount[5] = { 0,0,0,0,0 };
int manBlood[5] = { 3,0,1,2,4 };
int sumBlood;
int startMan = 0;
int t;
Blue(int n):sumBlood(n) {}
int makeMan(int nown, string str) {
int i = 0;
for (i;i < 5;++i) {
t = startMan % 5;
if (sumBlood < manBloodArr[nown][manBlood[t]]) {
++startMan;
continue;
}
++startMan;
++blueNo;
++manCount[t];
sumBlood -= manBloodArr[nown][manBlood[t]];
cout << str << " blue " << manName[manBlood[t]] << " " << blueNo << " born with strength " << manBloodArr[nown][manBlood[t]] << "," << manCount[t] << " " << manName[manBlood[t]] << " in blue headquarter" << endl;
return 1;
}
if (i >= 5) {
cout << str << " blue headquarter stops making warriors" << endl;
return 0;
}
}
};
string getTime(int n) {
if (n == 0) return "000";
string res = "";
res = to_string(n);
if (res.length() == 1) res = "00" + res;
if (res.length() == 2) res = "0" + res;
return res;
}
int main() {
cin >> n;
sumBloodArr = new int[n];
manBloodArr = new int[n][5];
for (int i = 0; i < n ; ++i) {
cin >> sumBloodArr[i];
for (int j =0;j < 5;++j) {
cin>> manBloodArr[i][j];
}
}
for (int i = 0;i < n;++i) {
cout << "Case:"<< i+1 << endl;
int j = 0;
Red redTeam(sumBloodArr[i]);
Blue blueTeam(sumBloodArr[i]);
bool f1 = true, f2 = true;
while(f1 || f2){
if (f1) {
f1 = redTeam.makeMan( i, getTime(j));
}
if (f2) {
f2 = blueTeam.makeMan(i, getTime(j));
}
j++;
}
}
delete[] sumBloodArr;
delete[] manBloodArr;
}