In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].

If you would like to talk about tasks, solutions or technical problems, please visit our Discord servers. They are moderated by the community, but members of the support team are also active there.

Every non-empty sequence of elements and is called a binary word. A word equation is an equation of the form , where and are binary digits ( or ) or variables i.e. small letters of English alphabet. For every variable there is a fixed length of the binary words that can be substituted for this variable. This length is called a length of variable. In order to solve a word equation we have to assign binary words of appropriate length to all variables (the length of the word assigned to the variable x has to be equal to the length of this variable) in such a way that if we substitute words for variables then both sides of the equation (which are binary words after substitution) become equal.

For a given equation compute how many distinct solutions it has.

Let be variables and let be their lengths ( is the length of , is the length of etc.). Consider the equation: This equation has distinct solutions.

Write a program, that:

- reads the number of equations and their descriptions from the standard input;
- finds the number of solutions of every equation;
- writes the results to the standard output.

In the first line of the standard input there is an integer , , which denotes the number of equations. The following lines contain descriptions of equations. Each description consists of lines. There are no empty lines between descriptions. Each equation is described in the following way:

In the first line of the description there is an integer , , which denotes the number of distinct variables in the equation. We assume that variables are the first small letters of English alphabet. In the second line there is a sequence of positive integers separated by single spaces. These numbers denote the lengths of variables from the equation (the first number is the length of , the second — etc.).

There is an integer in the third line of the description, which is the length of the left size of equation, i.e. the length of the word built of digits or and variables (single letters). The left side of the equation is written in the next line as a sequence of digits and variables with no spaces between them. The next two lines contain the description of the right side of the equation. The first of these lines contains a positive integer , which is the length of the right side of the equation. The second line contains the right side of the equation which is encoded in the same way as the left side. The number of digits plus sum of the lengths of variables (we count all appearances of variables) on each side of the equation is not greater than .

For every , , your program should write the number of distinct solutions of the -th equation to the -th line of the standard output.

For the input data:

1 5 4 2 4 4 2 5 1bad1 4 acbe

the correct result is:

16

*Task author: Wojciech Rytter.*