In the not-so-distant future, space engineers can invent a new technology for traveling through space and name it as "warp-drive". This warp-drive can make a spaceship travel faster than light speed. It works by bending an amount of distance in space and make a ship travel through that bended space in a single "hop". To travel through space, a spaceship (that is equipped with this warp-drive) may have to perform more than one hop between the beginning and the ending points. The amount of energy/power to hop depends on the current state/configuration of the warp-drive. And some amount of energy is also consumed in order to prepare or switch to any warp-drive state.
Goal
Suppose that you are an engineer on a battle spaceship. Your duty is to control/configure the warp-drive so that it will consume as little energy as possible for any traveling. For each traveling, you will be provided with a sequence of hops, which you have to find a corresponding sequence of warp-drive configurations that use the lowest energy.
In order to accomplish your duty, you have to build a computer program to find the lowest energy of warp-drive configuration sequence for any given hop sequence based on two tables of warp-driver energy consumptions. The first table shows the energy for switching between any two states. The second table shows the energy consumption related to warp-drive states and hops.
Input is a standard input which consists of parts, separated by a blank line.
The first part defines the sizes of warp-drive states and hop types.
It contains only one line. This line contains numbers separated by a space. These number are
Size of warp-drive states (), which is between and .
The state id is starting from to N-1.
The first state (id ) is the idle state. At this and only this state, the warpdrive can't perform any hop. (It is the default starting and ending state for any output state sequence.)
Size of different types of hops (), which is between and .
The hop id from to H-1.
The second part is a table showing the energy for switching warp-drive states. The table size is rows and columns, which contains N^2 energy values.
There are lines. Each line contains N energy values. These values are between and .
Each energy value can be indexed by its row and column. The row and column indexes are the state ids of the current and next warp-drive states respectively. Please be notified that these two indexes are starting from .
For example, the value at (row index, column index) = (,) is the energy for switching warp-drive state from number to . (This value is the first number in the sixth line.)
The third part is a table showing the energy consumption in performing hops for each state drive.
The table size is rows and columns, which contains by values.
There are lines. Each line contains energy values. These values are between and .
Each energy value can be indexed by its row and its column. The row index is the id of the current warp-drive state. The column index is the id of the hop. Please be notified that these two indexes are starting from .
For example, the value at (row index, column index) = (,) is the energy for warp-drive at state to perform hop . (This value is the tenth number in the second line.)
Please be notified that in the very first line of this part, which corresponds to the state or the idle state, all numbers are zeros. This rather means that it can't perform any hop in this state.
The fourth part is a set of hop sequences. The number of sequences in this set is between and .
The number of lines is up to .
Each line in this part contains one hop sequences.
The number of hops in a sequence is between and .
Each hop is a number (starting from to H-1) and is separated with space.
The blank line after the fourth part is the termination of the input.
For each hop sequence in the fourth part of the input, write parts of output as follows
In the first line, write the total amount of energy which must be minimum.
In the following line, write the solution. The solution contains a sequence of warp-drive states corresponds to the given hop sequence. (The size of input and output sequence must be equal.) If there are two or more possible solutions which consumes the same amount of energy, write only the sequence which contains the lowest states comparing from left to right.
More Explanations
There are lines in the sample input and lines in the sample output.
In the first sample output (solution for input line ), the minimum total energy is . The warpdrive state sequence is or [0-\ 3\ 2\ -0]. There are state switchings from → , → , and → , which consumes 1 + 1 + 2 = 4. And two hops ( and ) at state and respectively, which consumes 4 +1 = 5. So the total energy is 4 + 5 = 9.
In the last sample output, the minimum energy is . The solution is [0-\ 1\ 1\ 2\ 3\ -0] which draws energy in total.
Example 1