Stage One Confusion
View as PDFParsa is taking a multiple-choice exam with questions. He looks at the questions in order,
answers some of them, and never goes back.
Parsa's answer sheet contains answers. The
-th answer says that he chose option
for
question
. The question numbers on the sheet are strictly increasing.
Parsa may have made at most one copying mistake on the sheet. Such a mistake could have happened
to one consecutive block of his answers: while copying that block onto the sheet, every intended
question number in the block was changed by the same nonzero offset. More precisely, starting from
Parsa's intended answer sheet, one copying mistake consists of choosing one block and one nonzero integer
, then changing every question number in that block from
to
.
Both the intended question numbers and the written question numbers must be strictly increasing and between
and
. You may also decide that no mistake was made.
Given the correct answer key and Parsa's answer sheet as written, find the largest possible number of answers Parsa could have gotten correct before the copying mistake was made.

In the picture, one block is shifted by while being copied onto the sheet.
Input
The first line contains an integer , the number of test cases.
Each test case has the following format:
- The first line contains two integers
and
.
- The second line contains a string
of length
. The
-th character is the correct answer for question
.
- The next
lines each contain an integer
and a character
, meaning Parsa wrote answer
for question
.
If , it is guaranteed that
.
Output
For each test case, print one integer: the maximum possible number of correct answers on Parsa's intended answer sheet, before any copying mistake.
Constraints
- The sum of
over all test cases is
.
- Each character of
and each
is one of
A,B,C,D, orE.
Example 1
Input
5
5 4
AECDB
1 A
2 A
4 C
5 B
7 2
AAACDAA
1 C
3 D
7 2
AAACDAA
1 C
2 D
10 5
ABECCDBAEA
3 B
4 E
6 C
8 A
10 E
4 0
ABCD
Output
3
1
2
4
0
Explanation
In the first test case, the score on the sheet as written is . Parsa could have originally
written that answer for question
, then copied it as question
by mistake. Before that
mistake, his score would have been
.
Comments