Soundex is a phonetic algorithm that turns a string into a code. A code is always one letter followed by three digits. Strings that are spelled differently but sound alike get the same code. The rules are these.
b, f, p, v become 1c, g, j, k, q, s, x, z become 2d, t become 3l becomes 4m, n become 5r becomes 6h and w are ignored. The vowels a, e, i, o, u and y produce no digit, but they separate the letters around them.h or w also become a single digit. Two letters with the same digit separated by a vowel give that digit twice.The first letter only supplies the letter of the code. It never merges with the digits that follow it.
Some transformations:
robert and rupert both become R163.baawwwww becomes B000.hopp becomes H100. The first letter of the string always becomes the first letter of the code, even when it is a vowel or h or w.ratatata becomes R333, because the vowel between each pair of t (3) forces the digit to repeat.yhhhwthwhtwhthwhwth becomes Y300. Every h and w is ignored, so a single 3 is left.bbpb becomes B100. The first b gives the letter of the code, and the three remaining letters all carry digit 1 and stand next to each other, so they collapse into one digit.Many different words share one code. For example rhhhbm, rubeno, rpowam, robnew and 73908 further strings of 6 letters or fewer all become R150. Given a code and a maximum length, count the strings of that length or shorter that produce the code. Case does not matter, so AA, Aa and aa are the same string and are counted once.
The first line contains an integer T, the number of test cases. Each of the next T lines contains a string S and an integer L separated by a space. S is a soundex code, one uppercase letter followed by three digits. L is the largest allowed length of the original string.
a to z.For each test case print one line with the number of strings of length L or less whose soundex code is S. This number can be large, so print it modulo 1000000007.