eolymp
bolt
Try our new interface for solving problems

Dream

Curious student Peter loves to program. Once he is so carried away by this thing that just fell asleep at the computer! Pete dreamed that he was in an alternate reality. Alternate Reality is a rectangular maze, which can be represented in a table size \textbf{R}×\textbf{C} cells. In order not to miss school, Pete must find a way out of the labyrinth. Start position Petit denoted '\textbf{S}', beginning from an alternate reality -- '\textbf{E}'. In one move, Peter moves into one of four adjacent cells (left, right, up, down). If the cell is occupied by the wall (the symbol '\textbf{X}'), then Peter go into it can not. In some cells, there are doors with locks one of four colors ('\textbf{R}', '\textbf{G}', '\textbf{B}', '\textbf{Y}'). For the passage in this box, you must have the key of color. Since multiple keys, one key can open any number of its corresponding locks. The Lord of the alternative reality invites Pete to buy the keys to get to the exit. Our hero's very little money with him, so he wants to spend as little money and still get to the exit. Help him determine the minimum amount of money you'd spend on the keys. \InputFile The first line of the input file contains a number of tests. Each test starts with two integers \textbf{R} and \textbf{C} (\textbf{1 }<=\textbf{ R}, \textbf{C} <= \textbf{50}). The second line of the test consists of \textbf{4} integers \textbf{P_i}, the cost of purchasing key '\textbf{R}', '\textbf{G}', '\textbf{B}' and '\textbf{Y}' respectively (\textbf{P_i} <= \textbf{100}). Followed by \textbf{R} rows, each containing \textbf{C} characters, describing the maze. Each maze has only one symbol '\textbf{S}' and one character '\textbf{E}'. \OutputFile For each test on a separate line, print out a minimum amount of money needed to purchase a set of keys, allowing to pass to the exit. If the path to the exit from an alternate reality does not exist, and Petya doomed to oversleep lessons, display "\textbf{Sleep}".
Time limit 1 second
Memory limit 64 MiB
Input example #1
2
3 7
1 1 1 1
XXXXXXX
XS.X.EX
XXXXXXX
6 6
1 5 3 1
XXXXXX
XS.X.X
X..R.X
X.XXBX
X.G.EX
XXXXXX
Output example #1
Sleep
4