Under construction

Still being built. Things may change

Parliamentary Rankings

Time limit2sMemory limit512 MB

Summary
Given members and weekly action codes with point values, print the highest and lowest scores with the matching names in id order.
Level

Easy3 of 10

Topics
Hash map, Sorting, Implementation
Solved
No attempts yet

Problem

A group of journalists decided to rank how the members of parliament performed each week, and asked you to write the program that produces the ranking.

The journalists picked a code for every action they consider noteworthy and attached a number of points to it. Some values are positive and some are negative. The table below lists the actions in force now. More will be added if the scheme catches on, but only these seven appear in the input.

CodeActionPoints
SMade a speech lasting at least 5 minutes+10
QAsked a question during question time+5
AAnswered a question during question time+7
LSpent less than an hour in the chamber-8
FMade a funny remark that caused laughter in the chamber+4
DMade a derisory comment about another party-5
EWas asked to leave the chamber-10

Input

The input holds the data for one week.

The first line contains NN, the number of members who attended the chamber that week (0<N≤1200 < N \le 120).

Each of the next NN lines describes one member: a unique identifying number II (0<I≤1200 < I \le 120), one space, then the member's name. The identifying numbers are distinct and a name is at most 20 characters long. A name may contain spaces.

The next line contains AA, the number of recorded actions (0<A<2000 < A < 200).

Each of the following AA lines describes one action: the member's identifying number, one space, then one letter code from the table above. The number always belongs to a member who attended.

A member's score for the week is the sum of the points of the actions recorded for that member. A member with no recorded action scores 0.

Output

On the first line print the highest score and the names of the members who scored it. On the second line print the lowest score and the names of the members who scored it.

Each line starts with the score, followed by the matching names in increasing order of identifying number, separated by one space. When the highest and the lowest score are equal, the two lines have the same content.

Hint

If every member has the same score, all of them are both the best scoring and the worst scoring. Both lines are still printed, so the same line appears twice.

Examples2

  1. Example 1

    Input
    3
    1 Bill Bloggs
    3 Sara Quentin
    2 Jo Jones
    4
    1 S
    2 D
    1 L
    3 Q
    
    Expected output
    5 Sara Quentin
    -5 Jo Jones
    
  2. Example 2

    Input
    2
    1 Joe Smith
    2 Sally Baynes
    2
    1 S
    2 S
    
    Expected output
    10 Joe Smith Sally Baynes
    10 Joe Smith Sally Baynes