Hitting Books
Your final exam is tomorrow and you need to study, fast! Sadly, you don't know if the book you've borrowed from the library is worth reading.
To figure this out, you'll search for some number of keywords in your book. Each keyword has some amount of "utility". For example, if your book has the content:
ACCORDINGTOALLKNOWNLAWSOFAVIATIONTHEREISNOWAYABEESHOULDBEABLETOFLY
and your keywords are:
- CORD:
utility
- TO:
utility
- KNOW:
utility
- NOW:
utility
- SNOW:
utility
then your book has a total "utility" of , because:
- CORD appears
time
- TO appears
times
- KNOWN appears
time
- NOW appears
times
- SNOW appears
time
Note that keywords can overlap. Given a book's content, what is the total amount of "utility" that book has?
Input
The first line of each test case contains
, the content of your book, as one long string of uppercase characters.
The next line of each test case contains an integer
, the number of keywords you have to search for in your book.
The next lines of each input are in the form
, indicating that keyword
has
utility.
Note: Each keyword is guaranteed to be unique.
Output
Output the total utility of your book.
Example
Input
ACCORDINGTOALLKNOWNLAWSOFAVIATIONTHEREISNOWAYABEESHOULDBEABLETOFLY
5
CORD 10
TO 5
KNOW 20
NOW 10
SNOW 30
Output
90
See above for this example.
Input
AAABABABAAABAA
4
AA 5
AAB 10
BABA 30
AAA 5
Output
115
- AA appears in the book
times.
- AAB appears in the book
times.
- BABA appears in the book
times.
- AAA appears in the book
times.
Therefore, the total utility of the book is .
Comments