"AyB"and the replacement rules
{"A"->"ab", "Ay"->"cdy", "B"->"w", "B"->"x"} .G generates the language
L = {"AyB", "Ayw", "Ayx", "abyB", "abyw", "abyx", "cdyB", "cdyw", "cdyx"}
Given a UW grammar G, compute how many different strings there are in the language generated by G.
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The first line of input contains the initial string. The second and subsequent lines contain the replacement rules, one per line, terminated by end-of-file. There are at most 100 replacement rules. Each input string contains between 0 and 10 upper and lower case letters, and is enclosed in quotes. There are no spaces in the input.
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
Output consists of a single integer, the number of distinct strings in the language generated by G. If there are more than 1000 distinct strings, print "Too many." instead.
1 "AyB" "A"->"ab" "Ay"->"cdy" "B"->"w" "B"->"x"
9