Digital Root

아직 제출이 없습니다시간 제한12초메모리 제한512 MB

문제

Chiaki has a BB-based digital string ss of length nn. She has prepared mm queries for  the string. 

In the ii-th query, she would like to know the number of substring s_l..rs\_{l..r} (1lrn1 \le l \le r \le n) of ss such that after changing at most one digit in s_l..rs\_{l..r} to some digit in the set A_iA\_i, the digital root of s_l..rs\_{l..r} equals to x_ix\_i.

We should remind you that a digital root d(x)d(x) of the BB-based digital string xx (xx may have some leading zeros) is the sum s(x)s(x) of all the digits of this number, if s(x)B1s(x) \le B - 1, otherwise it is d(s(x))d(s(x)). For example, a digital root of the number 6543_106543\_{10} is calculated as follows: d(6543_10)=d(6_10+5_10+4_10+3_10)=d(18_10)=9_10d(6543\_{10}) = d(6\_{10} + 5\_{10} + 4\_{10} + 3\_{10}) = d(18\_{10}) = 9\_{10}, d(abcd_16)=d(2e_16)=d(10_16)=1_16d(abcd\_{16})=d(2e\_{16})=d(10\_{16})=1\_{16}.

Note that in this problem we will use the lowercase English letters from 'a' to 'f' to represent the digits with values from 1010 to 1515.

입력

The first line contains three integers nn, mm and BB (1n,m220,2B161 \le n, m \le 2^{20}, 2 \le B \le 16) -- the length of the string, the number of queries and the base of the number.

The second line contains a BB-based digital string ss of length nn.

Each of the following mm lines contains a character x_ix\_i and a BB-based string a_ia\_i (1a_iB1 \le |a\_i| \le B)-- the expected value of digital root and the set A_iA\_i. All characters in a_ia\_i are distinct.

출력

For each query, output an integer denoting the number of substrings.