Gap

Time limit3sMemory limit128 MB

Problem

Consider a card game called Gap.

There are 28 cards, each labeled with a two-digit number. The first digit (1 to 4) is the card's suit, and the second digit (1 to 7) is its value.

Shuffle the cards and deal them face up into four rows of seven cards, leaving one empty space at the far left of each row. The figure below shows one possible initial deal.

initial deal

Next, remove the four cards whose value is 1 and slide them into the empty space at the left end of the rows: "11" goes to the top row, "21" to the second row, "31" to the third, and "41" to the bottom row. Each card you remove leaves behind an empty space, called a gap.

You now have 28 cards and four gaps arranged in four rows and eight columns, and you start moving cards from this layout.

after the value-1 cards are placed

On each move you choose one of the four gaps and fill it with the successor of the card immediately to its left. The successor of a card is the next card of the same suit; for example the successor of "42" is "43", while "27" has no successor. In the layout above you could move "43" into the gap to the right of "42", or "36" into the gap to the right of "35". If you move "43", a new gap appears where "43" used to be (to the right of "16"). You may not fill a gap that sits to the right of a card whose value is 7, nor a gap that sits to the right of another gap.

By choosing your moves well, the goal is to build four ascending sequences, one suit per row, as shown below.

goal layout

Your task is to find the minimum number of moves needed to reach the goal layout.

Input

The first line contains the number of deals that follow.

Each deal is given on five lines: one blank line followed by four lines describing the four rows. Each of those four lines lists the seven two-digit cards of one row, from left to right.

Output

For each deal, print one line containing the minimum number of moves needed to reach the goal layout. This count must not include the four initial moves that slide the value-1 cards into place. If the goal cannot be reached from the given deal, print -1.