Hostel Nights

Time limit1sMemory limit128 MB

Summary
For each week, apply five nights of elimination rules to 20 rooms and list the students who survive the most nights, ordered by room number.
Level

Easy3 of 10

Topics
Implementation, Simulation, Array, Sorting
Solved
No attempts yet

Problem

The students living on the first floor of a hostel are known for being a bit too noisy. Over the 5 nights of one week, the warden investigates the noise reports. The other students will not name a floor-mate directly, but they help by ruling out rooms whose student was not noisy on a given night. On each night a room is eliminated if any one of three conditions holds:

  1. Room-number parity: E eliminates even-numbered rooms, O eliminates odd-numbered rooms.
  2. The room number is a multiple of a given number nn.
  3. The student's name begins with a given letter.

For each of the 5 nights the warden is given these three conditions. After the 5 nights he can identify the noisiest students: those who were not eliminated on the greatest number of nights. Because this floor is notorious, there is always at least one such student.

Input

The first line contains a single integer WW, the number of weeks of data (each week is 5 nights).

The data for each week begins with 20 lines, each containing a room number and a student's name separated by a space. Rooms are numbered from 101 to 120. Each student is a single name.

These 20 lines are followed by 5 lines, one per night. Each line contains a letter, a number, and a letter, separated by spaces. The first letter is E or O and says whether even (E) or odd (O) numbered rooms are eliminated. The number eliminates every room whose number is a multiple of it. The second letter eliminates every student whose name begins with that letter.

Output

For each week, first print the week number as Week k, where the first week is 1. Then print the noisiest students — those not eliminated on the greatest number of nights — one name per line, in increasing room-number order.

Examples3

  1. Example 1

    Input
    1
    101 Fred
    102 Gregory
    103 Susan
    104 Rewi
    105 Albert
    106 Georgina
    107 Peter
    108 Bethany
    109 Sarah
    110 Justine
    111 Barry
    112 Matthew
    113 Francis
    114 Chris
    115 Devina
    116 Yong
    117 William
    118 Edward
    119 Ruth
    120 Luckylast
    O 5 G
    E 3 F
    E 5 S
    E 1 A
    O 4 C
    
    Expected output
    Week 1
    Peter
    Edward
    Ruth
    
  2. Example 2

    Input
    1
    101 Aaron
    102 Abby
    103 Adam
    104 Aria
    105 Alan
    106 Amy
    107 Zed
    108 Ava
    109 Axel
    110 Anna
    111 Alex
    112 Ada
    113 Andy
    114 Aiden
    115 Aden
    116 Asha
    117 Aron
    118 Aya
    119 Ari
    120 Ana
    E 3 A
    E 3 A
    E 3 A
    E 3 A
    E 3 A
    
    Expected output
    Week 1
    Zed
    
  3. Example 3

    Input
    1
    101 Fred
    102 Gregory
    103 Susan
    104 Rewi
    105 Albert
    106 Georgina
    107 Peter
    108 Bethany
    109 Sarah
    110 Justine
    111 Barry
    112 Matthew
    113 Francis
    114 Chris
    115 Devina
    116 Yong
    117 William
    118 Edward
    119 Ruth
    120 Luckylast
    O 200 Z
    O 200 Z
    O 200 Z
    O 200 Z
    O 200 Z
    
    Expected output
    Week 1
    Gregory
    Rewi
    Georgina
    Bethany
    Justine
    Matthew
    Chris
    Yong
    Edward
    Luckylast