This page is still under construction.

Parts of this page are still being built. What you see may change.

Alphabetomials (Small)

Time limit5sMemory limit512 MB

Summary
Given a degree-4 polynomial and a dictionary, sum its value over all phrases of up to K dictionary words, modulo 10009.
Level

Medium6 of 10

Topics
Dynamic programming, Math, Combinatorics, Implementation
Solved
No attempts yet

Problem

Polynomials of degree 4 and polynomials of degree 5 behave very differently. The absence of a closed formula for the roots of a general degree 5 polynomial is what produced Galois theory, which has nothing to do with this problem.

Here we only consider multi-variable polynomials of degree at most 4 over 26 variables, one variable per lowercase English letter. Here is one such polynomial:

aber+aab+c

Given a string SS, evaluate the polynomial on it: substitute each variable with the number of times that letter appears in SS. The result is p(S)p(S).

For example, take the polynomial above and let SS be abracadabra edgar. It has 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

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 every SiS_i is a word of the dictionary. That is, SS is dd dictionary words joined by single spaces. The same word may be used more than once, and two phrases that place the words in a different order count as different.

You are given an integer KK. For every dd with 1≤d≤K1 \le d \le K, compute the sum of p(S)p(S) over all dd-phrases. The answers can be large, so print each of them modulo 10009.

Input

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

  • One line with the polynomial pp, then a space, then an integer KK.
  • One line with the number of words in the dictionary, nn.
  • Then nn lines, each with one word made of lowercase letters only. No word appears twice in the same test case.

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

Limits

  • 1≤T≤1001 \le T \le 100
  • pp is one or more terms joined by +. It does not start or end 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, consists of lowercase English letters only, and is at most 50 characters long.
  • 1≤n≤201 \le n \le 20
  • 1≤K≤51 \le K \le 5

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 number is the sum of p(S)p(S) over all ii-phrases, taken modulo 10009.

Examples2

  1. Example 1

    Input
    2
    ehw+hwww 5
    6
    where
    when
    what
    whether
    who
    whose
    a+e+i+o+u 3
    4
    apple
    orange
    watermelon
    banana
    
    Expected output
    Case #1: 15 1032 7522 6864 253
    Case #2: 12 96 576
    
  2. Example 2

    Input
    1
    aaaa 5
    1
    aaaa
    
    Expected output
    Case #1: 256 4096 718 5482 9865