Alphabetomials (Large)

No attempts yetTime limit5sMemory limit512 MB

Problem

We work only with multi-variable polynomials of degree at most 4 over 26 variables, one variable for each lowercase English letter. Here is one such polynomial:

aber+aab+c

Given a string SS, evaluate the polynomial on it. Every variable is replaced by the number of times that letter appears in SS, and the result is called p(S)p(S).

For example, take the polynomial above and let SS be abracadabra edgar. It contains six a's, two b's, one c, one e, and three r's, so

p(S)=6×2×1×3+6×6×2+1=109.p(S) = 6 \times 2 \times 1 \times 3 + 6 \times 6 \times 2 + 1 = 109.

You are given a dictionary of distinct words made of lowercase letters only. A string SS is a dd-phrase if

S = "S1 S2 S3 ... Sd"

where each SiS_i is a word from the dictionary. That is, SS is dd dictionary words joined by single spaces, and the same word may be used more than once. Given KK with K10K \le 10, compute the sum of p(S)p(S) over all dd-phrases, for every dd with 1dK1 \le d \le K. The sums get large, so report each one modulo 10009.

Input

The first line contains the number of test cases TT. Each test case has this format:

  • a line with an expression pp for the polynomial, then one space, then an integer KK;
  • a line with an integer nn, the number of words in the dictionary;
  • nn lines, each holding one word made of lowercase letters only. No word repeats inside one test case.

A polynomial is always written as a sum of terms, and each term is a product of variables. ata^t is written as tt copies of the letter a concatenated, so a2ba^2b is written as aab. The variables inside a term are always in non-decreasing lexicographic order.

Constraints

  • 1T1001 \le T \le 100
  • pp is one or more terms joined by +, and it neither starts nor ends with +. Each pp has at most 5 terms. Each term has at least 1 and at most 4 lowercase letters in non-decreasing order. No two terms of one polynomial are equal.
  • Each word is non-empty, uses only lowercase English letters, and is at most 50 characters long. No word repeats inside one dictionary.
  • 1n1001 \le n \le 100
  • 1K101 \le K \le 10

Output

For each test case, print a single line in the form

Case #X: sum1 sum2 ... sumK

where XX is the case number starting from 1, and the ii-th value is the sum of p(S)p(S) over all ii-phrases, taken modulo 10009.