Three Pin Bowling

No attempts yetTime limit1sMemory limit128 MB

Problem

Bowling in ACM Town uses only 3 pins. Write a program that computes the score of such a bowling game.

A game consists of 10 frames, and the game score is the sum of the scores of every frame. In each frame except the last (the 10th), you may throw at most twice. However, if your first throw knocks down all 3 pins (a strike), that frame uses only 1 throw.

  • Strike: the first throw knocks down all 3 pins.
  • Spare: the first throw does not clear all pins, but the second throw knocks down the remaining pins.
  • Miss: after two throws the 3 pins are still not all knocked down.

If the 10th frame is a miss, it uses only 2 throws. But if the 10th frame is a strike or a spare, it uses 3 throws in total, including the bonus throw(s).

The frame scoring rules are:

  • Miss: the frame score is the number of pins knocked down in the two throws.
  • Spare: the frame score is the number of pins knocked down in the two throws (always 3) plus the number of pins knocked down in the very next single throw.
  • Strike: the frame score is the number of pins knocked down in the one throw (always 3) plus the number of pins knocked down in the next two throws.

Because a spare needs the next 1 throw and a strike needs the next 2 throws, the 10th frame may take up to 3 throws when it is a strike or a spare.

For example, if in one game the 18 throws knock down, in order, 1, 2, 1, 0, 3, 1, 1, 3, 3, 2, 1, 1, 2, 2, 1, 3, 1, 1, then the frames split as 1,2 / 1,0 / 3 / 1,1 / 3 / 3 / 2,1 / 1,2 / 2,1 / 3,1,1, the per-frame scores are 4 / 1 / 5 / 2 / 8 / 6 / 4 / 5 / 6 / 5, and the game score is 46.

Your program reads the number of pins knocked down by each throw of one game and computes the game's score.

Note that the input may be invalid. For example, a game that starts with 2, 2 is an error: the first throw knocked down only 2 of the 3 pins, so just 1 pin remains standing, and it is impossible for the second throw to knock down 2 pins. Any input that knocks down more pins than are standing at that moment is treated as an error. The same rule applies to the bonus throws of the 10th frame (when a bonus throw clears all pins, the pins are reset).

Input

The program reads from standard input. The input consists of TT test cases with 0<T<1000 < T < 100. The first line contains TT.

Each test case consists of two lines. The first line gives kk, the number of integers on the second line, with 11k2111 \le k \le 21. The second line contains those kk integers: the number of pins knocked down by each throw, each an integer between 0 and 3 inclusive.

Output

For each test case, print the bowling score on its own line, so the number of output lines is exactly TT. If a test case's input does not follow the rules, print error on that line instead.