#P11412. [EPXLQ2024 fall round] 风吹起了从前

[EPXLQ2024 fall round] 风吹起了从前

Background

About the past, Wen Zhaoxue has fragmented memories.

Problem Description

Specifically, she has nn memories. Each memory is a string of length at most 100100. The ii-th memory SiS_i lies at depth rir_i in her mind and has value viv_i to her.

Wen Zhaoxue is trying to recover the beauty in her memories, but there are simply too many and they are too complex. She can only think of a prefix segment QQ of her memories and the deepest position dd she can reach. Only memories that satisfy: the corresponding QQ is a prefix of SiS_i and ridr_i \le d, can be recalled by Wen Zhaoxue.

Now, Wen Zhaoxue makes mm attempts. She wants to know, for each attempt, what the sum of values of all memories she can obtain is.

Input Format

The first line contains n,mn, m.

The next nn lines each contain two integers ri,vir_i, v_i and a string SiS_i, separated by spaces.

The next mm lines each contain an integer dd and a string QQ, separated by spaces. It is guaranteed that QQ is a prefix of at least one SiS_i, but it is not guaranteed that there exists a memory that can actually be recalled.

Output Format

Output mm lines. Each line represents, for each attempt, the sum of values of all memories she can obtain.

5 6
5 6 abcab
7 10 abcba
4 3 abc
3 6 ade
2 4 cde
2 abc
4 abc
5 abc
6 a
7 c
8 ab
0
3
9
15
4
19

Hint

Hint

In this problem, the definition of a prefix is as follows: for strings S,PS, P, if PS|P| \le |S| and for all 1iP1 \le i \le |P| we have Si=PiS_i = P_i, then PP is called a prefix of SS.

Scale and Constraints

This problem uses bundled testdata.

Subtask\text{Subtask} nn \le m m \le Special property Score
00 100100 1111
11 10410^4 10510^5 d=109d = 10^9 1313
22 String length is at most 22 99
33 10310^3 2626
44 10510^5 4141

For all testdata, it is guaranteed that 1Si,Q1001 \le |S_i|, |Q| \le 100, 0ri,vi,d1090 \le r_i, v_i, d \le 10^9, and all strings consist only of lowercase letters.

Please pay attention to the impact of constant factors in time complexity.

Please consider using faster input/output methods.

Translated by ChatGPT 5