#C3112. Popularity Score Calculation

    ID: 46504 Type: Default 1000ms 256MiB

Popularity Score Calculation

Popularity Score Calculation

Given a list of hashtags along with their corresponding weights and several posts consisting of hashtags, your task is to compute the popularity score for each post. The popularity score of a post is defined as the sum of the weights of the unique hashtags present in the post. If a hashtag appears multiple times in a post, its weight is counted only once.

The formula can be represented in LaTeX as follows: \(Score = \sum_{h \in S} w(h)\), where \(S\) is the set of unique hashtags in the post.

inputFormat

The input is read from standard input (stdin).

The first line contains an integer (n) representing the number of hashtags. The next (n) lines each contain a hashtag (a string) and its corresponding weight (an integer), separated by a space.

The following line contains an integer (m) representing the number of posts. Each of the next (m) lines starts with an integer (k) (the number of hashtags in that post), followed by (k) space-separated strings representing the hashtags in the post.

outputFormat

For each post, output the computed popularity score on a separate line to standard output (stdout).## sample

5
#food 5
#travel 8
#photography 7
#foodie 6
#nature 4
3
2 #food #travel
3 #photography #nature #travel
3 #food #foodie #nature
13

19 15

</p>