Under construction

Still being built. Things may change

gRanks

Time limit5sMemory limit512 MB

Summary
Rank athletes by summing each athlete's top M weighted placement scores and ordering by total, breaking ties alphabetically.
Level

Easy3 of 10

Topics
Sorting, Hash map, Implementation
Solved
No attempts yet

Problem

The world has many great athletes. When different athletes win different competitions, it is hard to say who is the best. 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 could award 1000 points for first place, 500 for second place, 300 for third place, and 0 for anything below that. There are no ties within a competition.
  2. Competitions are not equally important, so competition ii gets a weight WiW_i. The score an athlete gains in that competition is the points from step 1 multiplied by the weight of the competition. For example, if the Olympics has weight 5, then in the example above the winner of the Olympics gains 5×1000=50005 \times 1000 = 5000 points.
  3. So that an athlete does not gain simply by entering many competitions, only the MM highest scores an athlete earned across all competitions are added up. For example, if M=2M = 2 and an athlete earned 1000×51000 \times 5, 500×1500 \times 1 and 300×3300 \times 3 points in three competitions, only 5000 and 900 are added, for a total of 5900.

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 competitions.

Input

The first line contains the number of test cases TT. Then TT test cases follow. Each test case consists of the following.

  1. One line with the number PP of top places that are worth points.
  2. One line with the PP scores S1,S2,…,SPS_1, S_2, \dots, S_P, starting with first place and going down.
  3. One line with the number NN of competitions.
  4. NN lines, each holding the result of one competition. A line begins with the weight WiW_i of that competition and continues with the names of the athletes who finished in the top PP places, listed from first place downward.
  5. One line with the maximum number MM of competitions counted toward an athlete's score.

Output

For each test case, first print Case #x:, where xx is the test case number starting from 1. Then print every athlete who appears in the input, from the highest rank to the lowest, one per line in the format r: name, where rr is the rank of the athlete and name is the name.

Athletes with the same total share the same rank. The rank of an athlete is one plus the number of athletes with a strictly higher total. Athletes with the same rank are printed in alphabetical order of their names.

Constraints

  • 1≤T≤101 \le T \le 10
  • 1≤P≤1001 \le P \le 100
  • 1≤N≤1001 \le N \le 100
  • 1≤M≤1001 \le M \le 100
  • 1≤Si≤10001 \le S_i \le 1000
  • Si>Si+1S_i > S_{i+1}
  • 1≤Wi≤10001 \le W_i \le 1000
  • Each name uses only the uppercase letters A through Z and is at most 10 characters long.
  • No name appears twice in the result of one competition.

Hint

In the first example BOLT earned 7000 points in total from two competitions. GAY would have 8500 points if all four competitions counted, but only the top 2 count, so GAY has 6500 points and second place. PEIMENG and TIANBING both have 1500 points, so they share rank 3 and are printed in alphabetical order. LARRY has only 1000 points and comes last.

Examples5

  1. Example 1

    Input
    1
    2
    1000 500
    6
    5 BOLT GAY
    4 GAY BOLT
    1 GAY TIANBING
    1 GAY PEIMENG
    1 TIANBING LARRY
    1 PEIMENG LARRY
    2
    
    Expected output
    Case #1:
    1: BOLT
    2: GAY
    3: PEIMENG
    3: TIANBING
    5: LARRY
    
  2. Example 2

    Input
    1
    1
    1
    1
    1 A
    1
    
    Expected output
    Case #1:
    1: A
    
  3. Example 3

    Input
    1
    3
    1000 500 300
    3
    1 ANNA BORIS CHLOE
    2 BORIS CHLOE ANNA
    3 CHLOE ANNA BORIS
    1
    
    Expected output
    Case #1:
    1: CHLOE
    2: BORIS
    3: ANNA
    
  4. Example 4

    Input
    1
    2
    1000 500
    2
    1 X Y
    1 Y X
    100
    
    Expected output
    Case #1:
    1: X
    1: Y
    
  5. Example 5

    Input
    3
    1
    7
    2
    2 KIM
    3 LEE
    1
    2
    1000 999
    3
    1000 P Q
    1000 Q P
    1 R P
    2
    1
    1000
    1
    1 SOLO
    1
    
    Expected output
    Case #1:
    1: LEE
    2: KIM
    Case #2:
    1: P
    1: Q
    3: R
    Case #3:
    1: SOLO