2015
09-17

# Heroes of Might and Magic

《Heroes of Might and Magic III: The Restoration of Erathia》 is one of the most classic SLG game in recent years. Its powerful strategies and tactics to attract the players is enduring. Everyone controls his own combat arms to battle. Decide the outcome of a battle, in addition to a hero’s magic, level, character action good or bad, the layout of soldiers to walk, the most important thing is one’s own quantity and quality of all kinds of arms.
Jerry404 likes this game so much and later, in order to improve efficiency, his favorite is the automatic battle function inside.
Tom200 knows that and wonders what algorithm the system is through to calculate its automatically fight.
After a long time of YY, Tom200 finally speculated the automatic fighting rules:

1. Each hero can take up to seven arms, and the hero’s attack power, defense power bonus to each of the upper arms. For example, the hero’s attack power and defense power are respectively Ha and Hd, and the arms’ original attack power and defense power are Aa and Ad. After addition, Aa = Aa+Ha , Ad =Ad+Hd, and Aa is greater than 0.

2. Hero’s magic value equals its intelligence number * 10.
Hero’s magic injury equals its strength number * 50.
Heroes’ single cast magic consumption is fixed at 12, during the battle the hero cannot be attacked.
3. Each arms have five attributes: attack power, defense power, the value of life, the number of units and attack speed. Assume that A arms attack B arms, A arms of the attribute were Aa, Ad, Ab, Am, As and correspondingly B arms’ five attributes were Ba, Bd, Bb, Bm, Bs. Therefore, the total injury of A arms equals Am*Aa. At that time, B arms will bear that hurt unit by unit:
if Am*Aa is not greater than Bd, then one unit of B arms will deduct one value of life;
if Am*Aa is greater than Bd, then the excess part of the attack force will be considered as the deduction of current unit’s value of life of B arms;
if one unit’s current life of value cannot afford, then it dies and we will take the remainder of the attack force into the next unit settlement. Also at this time, you should calculate if attack power is greater than defense power.
if all units of the current arms are dead, injury will no longer be calculated.
For example, assume Aa = 5，Am = 2，Bd = 3，Bb = 4，Bm = 2, and A’s total injury is 10, greater than Bd by 7, and Bb is 4 which cannot afford. Then one unit dies and the left attack power is 3, not greater than Bd, so the second unit deduct one value of life. Finally, the end result is that B arms left 1 unit whose life is 3.

4. For brevity, the arms in left and right attack each other in turn every round (the arms in the left attacks first. Hero A is always on the left, B right, each round can only attack once per arms, if the current round of a party has no arms to attack, then the side bye). Attack speed determines the arms of the side order of attack and the arms appear first move first in case of the same attack speed. Heroes always turn to attack at the beginning of each round and its attack effects can be calculated in the same way as (3) describes. In every round, the hero cannot attack if it has no magic value and in this case it is seen as an attack without injury. A single round ends when heroes and arms of both sides attacked. The fourth rule can also look at the following schematic attack per round：
A hero magic attack -> B hero magic attack -> A fastest attack speed arms that are alive -> B fastest attack speed arms that are alive -> A second fastest attack speed arms that are alive -> B fastest attack speed arms that are alive…

5. Assuming that each arms’ attack range if full screen and can be attacked by every other arms.

6. The hero can equip treatment tent, which can get back 20 points of life value in each round. The treatment tent will choose the arms in which an individual unit has the maximum difference between remaining life value and original life value. In case of the same maximum difference, the tent will choose the arms that appear first (The amount of one unit’s life value will not exceed its original value and the tent cannot bring back those who are already dead.)

7. Every round all the arms (including the hero) will attack the enemy arms which has the most number of units. In case of the same number, it will attack the arms that appear first.

8. Every round each arms will have a chance of counter attack and it will counter attack immediately if it is able to. (If attacked by a hero, it cannot counter attack.)

9. A round ends after all the arms’ movement.

10. All the numbers are integers and the single number value is no greater than 100 and no less than 0. Calculations are integer calculations (rounded down is all right.)

11. The game comes to end when every single unit of all the arms on one side are dead.

Tom200 wants you to tell the final ending according to these crazy rules.

The first line contains a number T denoting the number of the test cases.
The fisrt line of each case are six integers Aa,Ad,Ae,As,An(1<=An<=7),Azp, indicating one’s own hero’s attack power, defense power, intelligence, strength, the number of arms the hero takes. If Azp=1 ,it means there is a treatment tent; else if Azp=0, there is no tent.
Five lines follow and each line contains An integers , respectively indicating An arms’ attack power, defense power, the value of life, the number of units and attack speed.
The next line is the corresponding data of the enemy hero :Ba,Bd,Be,Bs,Bn,Bzp.
Five lines follow and each line contains Bn integers , respectively indicating Bn arms’ attack power, defense power, the value of life, the number of units and attack speed.

The first line contains a number T denoting the number of the test cases.
The fisrt line of each case are six integers Aa,Ad,Ae,As,An(1<=An<=7),Azp, indicating one’s own hero’s attack power, defense power, intelligence, strength, the number of arms the hero takes. If Azp=1 ,it means there is a treatment tent; else if Azp=0, there is no tent.
Five lines follow and each line contains An integers , respectively indicating An arms’ attack power, defense power, the value of life, the number of units and attack speed.
The next line is the corresponding data of the enemy hero :Ba,Bd,Be,Bs,Bn,Bzp.
Five lines follow and each line contains Bn integers , respectively indicating Bn arms’ attack power, defense power, the value of life, the number of units and attack speed.

1
1 1 4 3 5 1
5 4 3 2 1
3 2 1 0 0
4 3 2 2 1
2 3 4 5 6
5 3 2 1 1
2 1 2 1 4 0
3 3 2 4
4 3 5 2
2 1 1 2
4 3 2 1
3 2 4 5

Win!
0 0 0 2 6
4 3 2 1