Alice builds machines that generates strings. Alice’s
machines each consist of states, numbered from to , and a set of directed edges
between these states, each labelled with a character from a
fixed set. A subset of the states are “final” states. The
machine generates strings by starting at state , following a path that terminates
at a final state, and concatenating the characters of the edge
labels together in the order that the edges are traversed. The
path is allowed to visit the same state more than once and can
traverse the same edge more than once. The path can pass
through final states before eventually terminating at a final
state. Self loops are allowed and having two or more edges to
and/or from the same state labelled with the same letter is
also allowed.
Bob has a favorite string . Carol has a favorite string
. Alice wonders if she
can build a machine that can generate exactly the strings that
have an equal number of occurrences of and as substrings. That is, the
machine should generate every string that has an equal number
of occurrences of and
as substrings and it
should not generate any strings that do not satisfy this
property. Occurrences may overlap. For example, the string
banana has two occurrences of the
substring ana. Help Alice determine
if it is possible to complete the task for Bob and Carol’s
favorite strings.
Figure 1 gives an example machine for the first case in
the sample input. The square states represent the final
states.
Input
The first line of input contains a single positive integer
, the number of
test cases. This is followed by lines each containing three
strings , , . The first string, , is the fixed set of characters
used in the machine. The characters in are distinct lowercase english
letters. The second string, , is Bob’s favorite string. The
third string, , is
Carol’s favorite string. The lengths of and satisfy . It is
guaranteed that the distinct characters in and are a subset of those in
.
Output
Output one line for each test case. Output if Alice can build a machine as
described. Otherwise, output .
Sample Input 1 |
Sample Output 1 |
3
ab ab ba
abc ab ba
cz cczz zzcc
|
1
0
0
|