ICC problem

0

0 votes
Problem

ICC is conducting a tournament in which 5 teams compete in one group in Round Robin format meaning that each team will play against all 4 other teams once. Thus each pair of teams will compete against each other once and only once. The top two teams at the end of the round will qualify for the knock out round. There will be a total of 10 matches and each team will play 4 matches. All matches have to be won by a single team and 2 points are awarded for a win. There is no possibility of a tie or no result. In case two teams end up with same points in the table, their position will depend upon the net run rate. In that case many teams (infact all 5 teams) can have a chance to qualify for next round at the end of current round. Our target is to make team "India" qualify for the next round by placing it in top 2. Take as input from user the current position of the scorecard and the remaining matches to be played and as an output print on screen the maximum number of matches out of the remaining matches India can afford to lose while still standing with a chance to qualify for the next round.

Input

The first line contains an integer T denoting the number of testcases. T testcases follow.
The first 5 lines of the input will contain the points table entries for the 5 teams in order India (I), Australia (A), Pakistan (P), England (E) and Bangladesh (B) respectively.
Each line contains 2 space seperated numbers with first one representing number of matches played and second, number of matches won out of those played matches.
Table data entered by the user must be consistent.
The next n(<=10) lines of the input contains the remaining matches in the tournament. Each line represents an upcoming match.
For example "PI" or "IP" both represent an upcoming match between India and Pakistan.

Output

The output is a single number which represents maximum number of matches India can lose out of the remaining matches while still having a chance to qualify for the next round. If India just can't qualify, print -1 as output.

Constraints

1 ≤ T ≤ 1000
Time Limit: 3
Memory Limit: 256
Source Limit:
Editor Image

?