On the other hand, the commander of the Rogue Two is very capricious so he wants to destroy the towers in a given order. Knowing the information of the communication towers in Geonosis and the order in which each tower will be destroyed, can you tell what is the power needed for the Rogue Two to complete it's mission?
The first line of the input contains an integer t, the number of test cases.
Each test case begins with a line containing an integer n (1 ≤ n ≤ 500) the number of towers in the fortress. Next n lines contain n integers each, the j-th number in the i-th line wij (1 ≤ wij ≤ 10^4, wii = 0) represents the amount of energy that cost to send a message from tower i to tower j.
Each test case ends with a line containing n distinct integers: x1, x2, . . . , xn (0 ≤ xi < n), the order in which the towers will be destroyed.
For each test case print the amount of energy that Rogue Two will need to destroy the Geonosis fortress. Explanation: In the first test case, the energy needed to destroy the tower 0 is 89, to destroy the tower 1 is 7, to destroy the tower 2 is 0. So, the total energy needed by Rogue Two is 96.