C. 猜字母王 Hexguesser

Memory limit: 256 MiB Time limit: 1000 ms Input file: stdin Output file: stdout
Problem type: traditional Judging mode: text compare

Description

愛麗絲和鮑伯正在玩一個叫「猜字母王」的新遊戲:

在遊戲開始時,鮑伯會想 6 個從 af可能重覆的字母,例如 \texttt{abbcde} \texttt{ddefff} ,字母是沒有順序的。然後,愛麗絲會開始猜測鮑伯的字母。愛麗絲每次會猜測 6 個字母,而鮑伯會回應,以 o 標示正確猜測的字母和以 x 標示錯誤猜測的字母。注意字母的順序不需考慮,猜測中的字母會被標示為正確,當且僅當該字母在鮑伯的字母中出現。

例如,如果鮑伯的字母為 \texttt{eeefff} 和愛麗絲的猜測為 \texttt{deedee} ,那鮑伯其中一個可能的回應為 \texttt{xooxox} ,因為其中 3 個字母 e 為正確猜測,而其他 3 個字母是不正確的。另外,鮑伯也可以回應 \texttt{xxoxoo}

愛麗絲和鮑伯以重覆猜測 N 次,因而產生 N 個猜測字符串和 N 個回應字符串。這 N 組字符串以 1, 2, \cdots, N 表示。現在,有 Q 個其他玩家進入。第 i 個玩家只能看到第 L_i 至第 R_i 組字符串。

愛麗絲和鮑伯想知道,每個玩家可以從給定的資訊中推斷出多少個正確的字母。你可以假設玩家們可以足夠聰明地有效運用給定資訊。


Alice and Bob play a brand-new game called \textit{hexguesser}:

At the beginning of the game, Bob thinks of 6 possibly repeated letters from a to f, such as \texttt{abbcde} or \texttt{ddefff} , with no specific order. After that, Alice can make guesses on Bob's letters. Alice guesses 6 letters each time and Bob will give a response, marking the letters guessed correctly as o and the letters guessed incorrectly as x. Note that the order of the letters does not matter, the guess is considered correct if that letter appears in Bob's letters.

For example, if Bob's letters are \texttt{eeefff} and Alice's guess is \texttt{deedee} , then one of the possible responses by Bob is \texttt{xooxox} , since 3 of the letter e are correct guesses, while the other 3 letters are incorrect. Alternatively, \texttt{xxoxoo} is also a possible response by Bob.

Alice and Bob have repeated this guessing N times, resulting in N guess strings and N response strings. These N pairs of strings are numbered from 1, 2, \cdots, N . Now, Q other players come in. The i -th player can only see the L_i -th to the R_i -th pair of strings.

Alice and Bob wonder how many correct letters each player can deduce with only the given information. You may assume the players are smart enough to utilize the given information.

Input

輸入第一行含有兩個整數 N Q

接下來 2N 行包含 N 對愛麗絲和鮑伯之間的猜測和回應字符串。回應字符串緊接於對應的猜測字符串。

接下來 Q 行各含有兩個正整數 L_i R_i


The first line of input contains two integers N and Q .

The next 2N lines of input consist of the N guess-response pairs between Alice and Bob, with the response string immediately following its corresponding guess string.

The next Q lines each contains two integers L_i and R_i .

Output

輸出 Q 行。第 i 行含有一個整數,代表第 i 個玩家可以推斷出的正確字母數量。如果沒有任何字符串符合第 i 個玩家看到的猜測和回應(即第 i 個玩家發現資訊有誤),輸出整數 -1


Output Q lines. The i -th line contains a single integer, the number of correct letters the i -th player can deduce. If there are no possible strings that satisfy all guess-response pairs seen by the i -th player (equivalently, the i -th player can deduce that there is wrong information), output the integer -1 .

Sample

Sample 1 input

2 2
aabbcc
oxoxox
dddeee
ooxxxx
1 1
1 2

Sample 1 output

3
6

Sample 2 input

2 1
abcdef
oooooo
abcdee
oooooo
1 2

Sample 2 output

-1

Sample explanation

對於樣例測試一的第一個詢問,該玩家可以推斷出鮑伯的字母中包含 \texttt{abc} 。他沒有足夠資訊推斷餘下三個字母中的任何一個。

對於樣例測試一的第二個詢問,該玩家可以推斷出鮑伯的字母是 \texttt{abcddf}

For the first query in sample test 1, the player can only deduce that Bob's letters contain \texttt{abc} . There is insufficient information to deduce any of the other 3 letters.

For the second query in sample test 1, the player can deduce that the letters are \texttt{abcddf} .

Constraint and hint

對於全部測試數據,滿足: 1 \leq N, Q \leq 10^5 1 \leq L_i \leq R_i \leq N 。 所有輸入字母皆在 af 之間。

子任務 分數 附加限制
1 8 L_i = R_i ,所有猜測只包含字母 a, bc
2 10 L_i = R_i
3 19 R_i - L_i \leq 1 ,所有猜測只包含字母 a, bc
4 6 R_i - L_i \leq 1
5 30 所有猜測只包含字母 a, bc
6 27 無附加限制

For all test cases, 1 \leq N, Q \leq 10^5 , 1 \leq L_i \leq R_i \leq N .

All input letters are within a to f.

Subtask Score Additional Constraints
1 8 L_i = R_i , all guesses only contain the letters a, b and c
2 10 L_i = R_i
3 19 R_i - L_i \leq 1 , all guesses only contain the letters a, b and c
4 6 R_i - L_i \leq 1
5 30 All guesses only contain the letters a, b and c
6 27 No additional constraints