gRanks (Small)

Rank every athlete by summing only their M highest weighted place points, breaking ties alphabetically with skipped ranks.

Easy3SortingHash mapSimulationInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

The world has many strong athletes, and it is hard to say who is the best at a sport when different athletes win different competitions. Here is one way to rank them.

  1. Fix the number PP of finishing places that are worth points, and the number of points SiS_i awarded for place ii. For example, with P=3P = 3 you might award 1000 points for 1st place, 500 for 2nd, 300 for 3rd, and 0 for anything below that. No competition has ties inside it.
  2. Competitions are not equally important, so give each competition a weight WiW_i. The score an athlete gains from a competition is the place points from step 1 multiplied by that competition's weight. If the Olympics has weight 5, then in the example above its winner gains 5×1000=50005 \times 1000 = 5000 points.
  3. So that entering many competitions is not by itself an advantage, add up only the MM largest scores an athlete earned, and call that the athlete's total. With M=2M = 2, an athlete who earned 1000×51000 \times 5, 500×1500 \times 1 and 300×3300 \times 3 in three competitions gets only 5000 plus 900.

You are given the points per place, the weight of each competition, and the results of the competitions. Rank every athlete who appears in the input.

Input

The first line holds the number of test cases TT. TT test cases follow, each of them made of:

  1. One line with the number PP of top places that are worth points.
  2. One line with PP integers S1,S2,,SPS_1, S_2, \dots, S_P, the points for 1st place down to PP-th place, in that order.
  3. One line with the number of competitions NN.
  4. NN lines, one per competition. Each line starts with that competition's weight WiW_i and continues with the PP names of the athletes who took the top PP places, listed from 1st place downward.
  5. One line with MM, the largest number of competitions that count toward one athlete's total.

Output

For each test case, first print one line holding Case #x:, where xx is the test case number starting from 1. Then print every athlete that appears in the input, one per line, in the format r: name, where rr is the athlete's rank and namename is the athlete's name.

An athlete's rank is one plus the number of athletes with a strictly larger total. Athletes with equal totals therefore share a rank, and the next rank after them skips by the size of the tied group. Print higher ranks first, and print athletes of the same rank in alphabetical order of their names. Do not print a blank line between test cases.

Constraints

  • 1T101 \le T \le 10
  • 1P101 \le P \le 10
  • 1Si10001 \le S_i \le 1000
  • Si>Si+1S_i > S_{i+1}
  • 1N101 \le N \le 10
  • 1Wi10001 \le W_i \le 1000
  • 1M101 \le M \le 10
  • Each name uses only the characters A through Z and is at most 10 characters long.

Hint

In the first example BOLT scored 7000 across his two competitions and ranks 1st. GAY would have 8500 if all four competitions counted, but only the top two count, so GAY has 6500 and ranks 2nd. PEIMENG and TIANBING both have 1500, so they share rank 3 and are listed alphabetically. Two athletes share rank 3, so the next rank is 5 rather than 4, and LARRY with 1000 points ranks 5th.