Bowling#699

Bowling

1 second
64 megabytes
Very easy

It is well known that programmers enjoy bowling. Bowling is a competitive sport in which a player (the bowler) rolls a bowling ball down a wooden or synthetic lane with the goal of scoring points by knocking down as many pins as possible.

Scoring in bowling can seem confusing to beginners, but it is not as difficult as it appears.

Most bowling centers are equipped with scoring computers, so usually you do not have to keep score yourself. However, if one day the computer breaks down, or you are asked to correct a mistake, knowing the scoring rules becomes important.

The most challenging part of scoring in bowling is when a strike or a spare is made, because in these cases, the score is not updated immediately on the scorecard.

A game consists of frames. At the start of each frame, a full set of pins stands. In each frame, you have up to two throws to knock down as many pins as possible.

If you knock down all ten pins with your first throw, it is called a strike. The score is not added immediately; instead, the values of your next two throws are added as a bonus to the strike. For example, if you get a strike in the first frame and then and in the second frame, you would score 18\ (10 + 7 + 1) for the first frame, and for the second frame, making a total of after two frames.

If you knock down some pins with the first throw and the rest with the second throw, it is called a spare. Again, the score is not added immediately, because for a spare you get the value of your next throw as a bonus. For example, if you get a spare in the first frame, say and , and then and in the second frame, you would score 18\ (6 + 4 + 8) for the first frame, and for the second frame, for a total of points after two frames.

The scoring in the final (tenth) frame is a bit different. If you get a strike or a spare, you are given extra throws as bonuses. If you score a strike on the first throw, you may have up to two additional throws, for a total of three throws in the final frame. If you get strikes on all three final throws, the score for the last frame is 30\ (10 + 10 + 10). If you get a spare (such as and ), you get one more throw as a bonus. For example, a spare followed by a strike gives 20\ (9 + 1 + 10).

Write a program to calculate the score for a player's game given the information about how many pins were knocked down after each throw.

Input

The first line contains the number of test cases (1 \leq t \leq 1000). Each of the following lines describes a test case. Each test case consists of several integers 0 \leq a \leq 10—the number of pins knocked down after each throw. Each test case describes a full game for one player. All games in the input are valid.

Output

For each test case, print the number of points the player scores in the game on a separate line.

Examples

Example 1

Loading
Loading