Under construction

Still being built. Things may change

Senior, Please Buy Me Dinner!

Time limit1sMemory limit1024 MB

Summary
Given each senior's promised week, day, and dinner cost plus their money, find the longest run of consecutive days with a promise the senior can afford to pay.
Level

Easy3 of 10

Topics
Sorting, Hash map, Implementation
Solved
No attempts yet

Problem

Jeongmin, a freshman in the class of 2024, writes down every dinner promise his seniors made in a notepad. Each line of the notepad records a senior's name SS, the promise week WW, the day DD, and the cost PP of the dinner. Names are strings, and the rest are integers. Also, no senior buys dinner more than once, and all senior names are different.

Being a computer science major, Jeongmin writes the day as an integer between 00 and 66. For example, Monday is 00 and Thursday is 33.

Kind seniors cannot back out of a dinner promise to their cute junior if they have enough money to pay for it. Looking at Jeongmin's records and the money each senior has, find the maximum number of consecutive days on which Jeongmin can get a free dinner.

Input

The first line contains the number of records NN. (0≤N≤1000 \leq N \leq 100)

From the second line, the next NN lines each contain record information in the form SS WW DD PP, which means senior SS made a dinner promise on day DD of week WW that costs PP won. Each senior name appears exactly once in the records. (1≤W≤101 \leq W \leq 10; 0≤D≤60 \leq D \leq 6; 0≤P≤100 0000 \leq P \leq 100\,000)

Then, the next NN lines each contain a senior name and the money MM that senior has, separated by a space. The order of the names here may differ from the order in the records. (0≤M≤100 0000 \leq M \leq 100\,000)

Each senior name is a string of up to 12 English letters, uppercase or lowercase.

Output

Print the maximum number of consecutive days on which Jeongmin can get a free dinner.

Hint

By international standard, the week starts on Monday.

Examples1

  1. Example 1

    Input
    3
    OhIkjun 1 6 10000
    Hyukjun 1 5 5000
    Junseong 2 0 8000
    Hyukjun 4000
    OhIkjun 12000
    Junseong 20000
    
    Expected output
    2