Bowling

No attempts yetTime limit1sMemory limit256 MB

Problem

Byteasar likes bowling and statistics. He wrote down the results of a few bowling games he played, but some characters in the notes are blurred and unreadable. Write a program that counts the distinct games consistent with his notes.

Rules of bowling

A game has nn frames: n1n - 1 simple frames and one final frame. A typical game has n=10n = 10. At the start of every frame 10 pins stand at the end of the lane, and the player rolls the ball to knock down as many pins as possible. A simple frame gives at most two shots and the final frame at most three. A simple frame is written with two characters, the final frame with three.

The basic points of a shot are the number of pins it knocks down. The basic points of a frame are the sum of the basic points of its shots. Knocking down all 10 pins in a simple frame earns 10 basic points plus bonus points.

A simple frame follows these rules.

  • The first shot knocks down all 10 pins: this is a strike and the frame ends. The bonus points are the sum of the basic points of the next two shots. A strike is written x-.
  • The two shots of the frame together knock down all 10 pins: this is a spare. The bonus points are the basic points of the next shot. A spare is written A/, where A is the one-digit number of pins knocked down by the first shot.
  • The two shots knock down 9 pins or fewer: the player earns only basic points, and the frame is written AB, where A is the number of pins from the first shot, B the number from the second, and A+B<10A + B < 10.

Bonus points count toward the frame where the strike or the spare happened, even though their value comes from shots in later frames.

The final frame follows these rules.

  • The player starts with two shots. If those two shots knock down 9 pins or fewer, the frame ends. If the two shots make a spare, or the first shot is a strike, the player takes a third shot. Whenever a shot knocks down every standing pin, 10 pins are set up again for the next shot. The score of the final frame is the total number of pins it knocks down, and strikes and spares earn no bonus points there.
  • The final frame has seven possible forms. A and B are one-digit numbers.
NotationDescriptionFrame score
xxxthree strikes3030
xxAtwo strikes and a shot knocking down AA pins20+A20 + A
xA/a strike and a spare whose first shot knocks down AA pins2020
xABa strike and two shots knocking down AA and BB pins (A+B<10A + B < 10)10+A+B10 + A + B
A/xa spare whose first shot knocks down AA pins, then a strike2020
A/Ba spare whose first shot knocks down AA pins, then a last shot knocking down BB pins10+B10 + B
AB-two shots knocking down AA and BB pins (A+B<10A + B < 10)A+BA + B

A game is written as a sequence of 2n+12n + 1 characters. The running total after each frame follows from that sequence. For example, the game 08x-7/2/x-x-23441/0/x with n=10n = 10 scores like this.

FrameNotationBasic pointsBonus pointsFrame scoreTotal
1080+80 + 8008888
2x-10107+37 + 320202828
37/7+37 + 32212124040
42/2+82 + 8101020206060
5x-101010+210 + 222228282
6x-10102+32 + 315159797
7232+32 + 30055102102
8444+44 + 40088110110
91/1+91 + 9001010120120
final0/x0+10+100 + 10 + 10002020140140

Input

The first line has one integer qq (1q251 \le q \le 25), the number of test cases. Each test case takes three lines.

The first line of a test case has one integer nn (2n102 \le n \le 10), the number of frames. The second line has the 2n+12n + 1 characters that describe the game in Byteasar's notes, with every blurred character replaced by ?. The third line has nn integers separated by spaces, the running total after each frame. In each of these numbers either every digit is readable or every digit is blurred, and a number whose digits are all blurred is replaced by -1.

Output

For each test case print one line with the number of distinct games that agree with it, in the order of the input.

Two games differ when at least one shot differs, that is, when their 2n+12n + 1 character descriptions differ. At least one game agrees with each test case in the input. Every answer fits in a signed 64-bit integer.

Hint

The first example input has two test cases.

In the first one, frame 5 can only be x-, since - is the one character that can follow x. Frame 8 scores 8 points, so its two shots are 0+80 + 8, 1+71 + 7, ..., 8+08 + 0, nine possibilities in all. Frame 9 earns 0 bonus points, so the first shot of the final frame knocks down no pins. The only way to score 20 with the remaining two shots is a spare followed by a strike. Nine games therefore agree with the notes.

In the second test case, every digit from 0 to 9 fits the blurred position.