Mate

For each query, count subsequences of S of length D whose last two characters are the given pair XY, modulo 1e9+7.

Medium7CombinatoricsDynamic programmingPrefix sumMathNo attempts yetTime limit2sMemory limit128 MB

Problem

Mate got a string of lowercase English letters from his parents. To get at least some use out of the present, he decided to find rhymes in it for the song he is writing next.

A rhyme is a word of length DD whose next to last letter is XX and whose last letter is YY. Mate builds a word by crossing out some letters of the given string and joining the letters he did not cross out in their original order. For each rhyme, count how many ways there are to cross out letters so that the remaining word meets the condition.

Two ways count as different when the sets of crossed-out positions differ.

Input

The first line contains a string SS of lowercase English letters. (2S20002 \le |S| \le 2000)

The second line contains the integer QQ, the number of rhymes Mate needs. (1Q5000001 \le Q \le 500\,000)

Each of the next QQ lines contains an integer DD and a string XYXY of two lowercase English letters, separated by a space. (2DS2 \le D \le |S|)

Output

Print QQ lines. The iith line contains the number of ways for the iith rhyme. The number can get very large, so print it modulo 10000000071\,000\,000\,007.

Note

Take SS = banana with D=2D = 2 and XYXY = na. Number the letters from 1. Exactly three sets of kept positions work: (3,4)(3, 4), (3,6)(3, 6) and (5,6)(5, 6). In each of them the kept letters spell na, and every other letter is crossed out.