In a chemist's lab, there are several types of chemicals in tubes. The chemist wants to mix all this chemicals together, two chemicals at a time. Whenever two chemicals are mixed, some heat is generated and released to the air and the mixed chemical is a known Chemi Cal of possibly other type than the original two. The resulting chemical type and the amount of heats emitted can looked up in the chemical table.
For example, in the above chemical mixture table, there are three types of chemicals:1, 2, and 3. If You mix Chemicals 1 and 3, they produce +3000 units of heat and turn into chemical 3. Sometimes, the heat generated can be negative. For instance, can mix 2 and 3 and they turn to chemical 1 and in the meantime, cool down the lab by units of Hea T. Since the chemist lacks funding to buy necessary equipments to protect himself from the heat, it is generated IM Portant to find a way to mix all the chemicals together which produces the least total heat, regardless of the final chemic The AL type. For example, suppose the lab has four tubes containing chemicals of types 1, 2, 2, and 3. If The chemicals are mixed in the parenthesize Order of ((1 2) (2 3)), it would produce (–10) + ( -500) + (3000) = 2490 units O F Heat. However, if the chemicals are mixed in the (2 (1 (2 3))) order, it would produce ( -500) +0+ ( -10) = -510 units of heat, which is also the least total heat possible.
Input
The a single number denoting the number of the "a" of input file consists of the the number of test cases in the file. There is a single line containing a '/' character separating two consecutive test cases. The end of the file is marked with a line containing a '. ' For the all test case, the the ' The ' the ' contains ' an integer number M (1≤m≤6) denoting the number chemical types. Therefore, the chemicals are indexed from 1 up to at most 6. The next MXM lines give the chemical mixture table entries in the Row-major order. Each line contains the new resulting chemical type and the amount's energy emitted. After the table entries are a line with a single number k (2≤k≤10) denoting number of tubes in the lab. The next line then contains k integers in the range of 1 and m, separated by blank spaces, denoting the types of chemicals In those K tubes.
Output
For each test case, output the least total heat possible on a single line.