Evolution

Time limit1sMemory limit128 MB

Summary
Given N DNA strings linked in an unknown parent-child order, compute each creature's probability of being the original ancestor.
Level

Medium7 of 10

Topics
Probability, Bit manipulation, Dynamic programming
Solved
No attempts yet

Problem

Dr. Beverly studies an unusual organism whose DNA is a single string of kk letters drawn from an alphabet of size dd. One hour after it is born, an individual produces exactly one offspring, and then lives on for a short while longer. This repeats generation after generation, forming a single line of descent.

When an offspring is produced, its DNA is copied from its parent, but each of the kk letters may mutate independently. With probability pp a given letter mutates: it is replaced by a letter chosen uniformly at random from the size-dd alphabet (which may, by chance, be the same letter). With probability 1−p1 - p the letter is left unchanged. Consequently, for one parent-to-child step, a fixed letter stays equal to a given target letter with probability (1−p)+p/d(1 - p) + p/d, and becomes a specific different letter with probability p/dp/d; the transition probability of a whole string is the product over its kk positions.

Dr. Beverly began an experiment with a single individual, then got distracted and forgot about it. Returning later, she found the remains of NN creatures — the original individual together with its unbroken chain of descendants — but she no longer knows their birth order. She sampled all NN DNA strings. For each sampled creature, compute the probability that it was the original individual with which the experiment began.

Assume that, a priori, each of the NN creatures is equally likely to have been the original, and that the NN strings are presented in a random order.

Input

The first line contains a single integer TT, the number of test cases. Each test case has the following format:

  • One line with three integers NN, kk, dd and one real number pp, separated by single spaces, where 1≤N≤151 \le N \le 15, 1≤k≤81 \le k \le 8, 1≤d≤41 \le d \le 4, and 0.2≤p≤0.50.2 \le p \le 0.5.
  • NN following lines, each containing a string of length kk over a fixed alphabet of size dd (a subset of the uppercase letters A–Z, the same alphabet for all NN strings). The string on the ii-th line is the DNA of the ii-th creature.

Output

For each test case, output NN lines. The ii-th line contains the probability that the ii-th creature given in the input was the original individual, rounded to exactly six digits after the decimal point.

Examples4

  1. Example 1

    Input
    2
    3 1 2 0.25
    A
    C
    C
    4 4 4 0.50
    GTTG
    TGTG
    TTTG
    GTGT
    
    Expected output
    0.466667
    0.266667
    0.266667
    0.046602
    0.393710
    0.083333
    0.476354
    
  2. Example 2

    Input
    1
    1 3 2 0.30
    AAA
    
    Expected output
    1.000000
    
  3. Example 3

    Input
    1
    2 2 2 0.40
    AB
    AB
    
    Expected output
    0.500000
    0.500000
    
  4. Example 4

    Input
    1
    3 1 3 0.30
    A
    B
    C
    
    Expected output
    0.333333
    0.333333
    0.333333