Problem: Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.
The building consists of floors, each with rooms each. Let represent the -th room on the -th floor. Additionally, there are ladders installed. The -th ladder allows Ram to travel from to , but not in the other direction. Ram also gains health points if he uses the ladder . It is guaranteed for all ladders.
If Ram is on the -th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from to , he loses health points.
Ram enters the building at while his helicopter is waiting at . What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
Input Format: The first line of input contains () — the number of test cases.
The first line of each test case consists of integers (; ) — the number of floors, the number of rooms on each floor and the number of ladders respectively.
The second line of a test case consists of integers ().
The next lines describe the ladders. Ladder is denoted by (; ; ) — the rooms it connects and the health points gained from using it.
It is guaranteed for all ladders and there is at most one ladder between any 2 rooms in the building.
The sum of , the sum of , and the sum of over all test cases do not exceed .
Output Format: Output the minimum health Ram loses on the optimal path from to . If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
Note: The figure for the first test case is in the statement. There are only possible paths to :
- Ram travels to , takes the ladder to , travels to , takes the ladder to , travels to where he finally escapes via helicopter. The health lost would be \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*}
- Ram travels to , takes the ladder to , travels to , takes the ladder to , travels to where he finally escapes via helicopter. The health lost would be \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*}
In the second test case, there is no path to .
In the third case case, Ram travels to and takes the only ladder to . He loses health points and gains health points. Therefore the total loss is (negative implies he gains health after the path).