You receive every throw in order and the pieces left on the board, and you decide whether the game rules can produce that board.
Hard8BacktrackingSimulationImplementationNo attempts yetTime limit10sMemory limit512 MBYut Nori is a Korean folk board game. Players throw four half moon shaped sticks and move their pieces by the result. Two teams throw in turn, and the team that gets all of its pieces past the finish point wins. The rules differ from region to region, so only the rules below apply here.
Do): move one space forward.Gae): move two spaces forward.Gul): move three spaces forward.Yut): move four spaces forward, then throw again.Mo): move five spaces forward, then throw again.On every throw the team picks one movable piece and moves it by that many spaces. A piece that is still off the board counts as movable: it enters the board that many spaces along. The throws must be used in the order they came, so if Mo comes before Gul, the five space move happens first and the three space move second.
When your piece lands on a space held by another of your own pieces, the two are stacked and always move together from the next move on. When your piece lands on a space held by pieces of the other team, every opposing piece on that space is captured and the capturing team throws again. A captured piece starts over from off the board. Capturing with Yut or Mo still grants one extra throw, not two.
Every piece starts off the board and cannot be captured before it enters the board. A piece counts as finished only when it moves completely past the finish point. A finished piece is never used again, and the moment one team has all of its pieces finished that team wins and the game stops. Even a win on a Yut or a Mo grants no further throw once the game has stopped.

The spaces are numbered 0 through 28. The outer ring runs 0, 1, 2, 3 up to 19 in order, and 0 follows 19. Spaces 5, 10 and 22 (the center) are the ones marked with a red dot in the figure: a piece that stops exactly on one of them takes the shortcut from its next move on. A piece that only passes over such a space does not enter the shortcut. The forward route from each space is listed below. "Finish" in the table means moving past the finish point and leaving the board, and a move longer than the remaining route finishes the piece.
| Starting space | Forward route |
|---|---|
| off the board | 1, 2, 3, 4, 5 |
| 0 | finish |
| 1, 2, 3, 4 | the next number through 19, then 0, then finish |
| 5 | 20, 21, 22, 23, 24, 15, 16, 17, 18, 19, 0, finish |
| 6, 7, 8, 9 | the next number through 19, then 0, then finish |
| 10 | 25, 26, 22, 27, 28, 0, finish |
| 11 through 19 | the next number through 19, then 0, then finish |
| 20 | 21, 22, 23, 24, 15, 16, 17, 18, 19, 0, finish |
| 21 | 22, 23, 24, 15, 16, 17, 18, 19, 0, finish |
| 22 | 27, 28, 0, finish |
| 23 | 24, 15, 16, 17, 18, 19, 0, finish |
| 24 | 15, 16, 17, 18, 19, 0, finish |
| 25 | 26, 22, 27, 28, 0, finish |
| 26 | 22, 27, 28, 0, finish |
| 27 | 28, 0, finish |
| 28 | 0, finish |
A piece on 19 that moves one space stands on 0. It has not finished yet, so the other team can still capture it. A piece on 0 finishes on any move.
Yong's family split into team A and team B for a holiday game of Yut Nori. Team A went first, and they wrote down every throw in order on a sheet of paper. Around dinner time, with the game either still running or just over, the puppy Puppy walked across the board and scattered the pieces. Pieces that had not started and pieces that had already finished were left alone. Puppy also chewed off the part of the sheet that recorded which team made each throw. Yong rebuilt the board from memory but cannot tell whether the arrangement fits the list of throws left on the paper.
You are given the full list of throws and the rebuilt board. The listed throws are every throw that was actually made, in the order they were made. Decide whether the rebuilt board is an arrangement that can really occur.
The first line holds the number of test cases T. Each test case is four lines.
U N A B
throw_1 ... throw_N
posA_1 ... posA_A
posB_1 ... posB_B
The first line holds four integers U, N, A and B separated by spaces. U is the number of pieces each team plays with, N is the number of throws in the list, A is the number of team A pieces left on the board, and B is the number of team B pieces left on the board.
The second line holds the N throws in the order they were made, separated by spaces. Each value is one of Do, Gae, Gul, Yut, Mo.
The third line holds the A positions of team A pieces and the fourth line holds the B positions of team B pieces, separated by spaces. A line with a count of 0 is empty. Stacked pieces repeat the number of their space once per piece. Positions of pieces that have not started and of pieces that already finished are not given.
For each test case print one line in the form Case #x: y. x is the test case number starting from 1, and y is the verdict: print YES if the given list of throws can produce that board, and NO otherwise.