SPOJ Problem Set (classical)
85. Dice Contest
Problem code: DICE1

Everyone loves gambling in the Dicent City. Every Saturday the whole community
meets to attend a dice contest. They started a few years ago with a classic
sixsided die with 1 to 6 dots displayed on the sides and had a lot of fun.
However they soon got bored and that's why more sophisticated dice are in use
nowadays. They put a sticker on each side and write a positive integer on each
sticker.
The contest is run on a strip divided into squares in a chessboardlike manner.
The strip is 4 squares wide and infinite to the left and to the right (is
anyone going to say it can't exist in the real world, huh?). The rows of the
strip are numbered from 1 to 4 from the bottom to the top and the columns are
numbered by consecutive integers from the left to the right. Each square is
identified by a pair (x,y) where x is a column number and y is a row number.
The game begins with a die placed on a square chosen be a contest committee
with onedot side on the top and twodots side facing the player. To move the
die the player must roll the die over an edge to an adjacent (either
horizontally or vertically) square. The number displayed on the top of the die
after a roll is the cost of the move. The goal of the game is to roll the die
from the starting square to the selected target square so that the sum of costs
of all moves is minimal.
Task
Write a program that:

reads the description of a die, a starting square and a target square,

computes the minimal cost of rolling the die from the starting square to the
target square,

writes the result.
Note: all teams participating in the contest received dice from the organisers.
Input
The input begins with the integer t, the number of test cases. Then t test
cases follow.
For each test case the first line of the input contains six integers l_{1}, l_{2},
l_{3}, l_{4}, l_{5}, l_{6} (1 < = l_{i}
< = 50) separated by single spaces. Integer l_{i} is the number
written on a side having originally i dots. The second line of the input
contains four integers x_{1}, y_{1}, x_{2}, y_{2}
( 10^{9} < = x_{1}, x_{2} < = 10^{9}, 1
<= y_{1}, y_{2} < = 4) separated by single spaces.
Integers x_{1}, y_{1} are the column and the row number of the
starting square respectively. Integers x_{2}, y_{2} are the
column and the row number of the target square respectively.
Output
For each test case the first and the only line of the output should contain the
minimal cost of rolling the die from the starting square to the target square.
Example
Sample input:
1
1 2 8 3 1 4
1 1 0 2
Sample output:
7