Under construction

Still being built. Things may change

Imputation

Interview

Time limit2sMemory limit512 MB

Summary
Assign A, T, C, or G in place of each '?' on tree leaves to minimize total transition costs along edges, summed over all string positions independently.
Level

Medium7 of 10

Topics
Dynamic programming, Tree, Hash map, Implementation
Solved
No attempts yet

Problem

Leila is a bioinformatician who studies bacterial evolution. In one experiment on a special type of bacteria, she started from a single bacterium, put it on a plate, and monitored the bacterial division until she obtained a population of k bacteria. During the process, she carefully recorded the evolutionary relations between bacteria. Precisely, for each bacterium she recorded its parent bacterium.

In the next step, she extracted DNA sequences of the k bacteria in the final population using NGS technology. Each DNA sequence is represented as a string of length m over the alphabet {A, T, C, G}.

NGS technology has a drawback: it produces many missing values. So there are many unknown characters indicated by '?' in the extracted sequences. Considering the evolutionary relationship between bacteria, Leila wants to impute the missing values. Among all possible imputations, she wants to find the minimum cost imputation from an evolutionary perspective.

The problem is defined as follows. A rooted tree T is given, and for each leaf v of T, a string bv of length m over the character set {A, T, C, G, ?} is given. A transition cost matrix ∆ is also given, where ∆(x, y) (x, y ∈ {A, T, C, G}) represents the cost of a transition from an x character to a y character, from a parent to its child.

A feasible imputation assigns a string su of length m over the character set {A, T, C, G} to each vertex u, where for each leaf v of T, sv equals bv except for the '?' characters in bv. The evolutionary cost of an imputation is defined as the sum of the evolutionary costs of all edges. The evolutionary cost of an edge between parent u and child w is defined as Σmi=1 ∆(su[i], sw[i]), where su[i] is the i-th character of su.

Leila wants to find a feasible imputation for T that has the minimum evolutionary cost among all feasible imputations. The tree T, the transition cost matrix ∆, and a string bv for each leaf v are given. You should write a program to compute the minimum evolutionary cost of feasible imputations.

Input

The first line of the input contains an integer n (2 ⩽ n ⩽ 10, 000) denoting the number of vertices of T. The vertices of T are numbered from 1 to n. The root of the tree is numbered 1. The root is never considered a leaf, even if it has only one child. The next n − 1 lines describe the edges of T; each line contains the two endpoints of an edge separated by spaces. In the next four lines, the evolutionary cost matrix ∆ is given; each line is one row of ∆. Rows (corresponding to a parent) and columns (corresponding to a child) of ∆ are ordered to represent the characters A, T, C and G respectively. All entries of ∆ are non-negative integers not more than 106. The next line just contains k, the number of leaves. Finally, each leaf v (its number) and its bv, which is a string of size m (1 ≤ m ≤ 200), appear in one line.

Output

In one line, print the minimum evolutionary cost of feasible imputations.

Examples1

  1. Example 1

    Input
    3
    1 2
    1 3
    0 3 4 4
    4 0 4 4
    4 4 2 4
    1 1 1 0
    2
    2 AAC
    3 T?C
    
    Expected output
    4