A row of N+1 cells is numbered from 0 to N, and cell i holds the value i. The game starts on cell 0 with the sum S=0. On each turn you roll a die, move that many cells to the right, and add the value of the cell you land on to S. The game ends the moment you land on cell N. A roll that would carry you past cell N is discarded and you stay where you are. On cell N−1, for example, you have to wait for a 1 before you can make the last move.
Instead of a real die you get a generator, a sequence that uses each of the numbers 1 to 6 once. Repeat that sequence as many times as you need, and the result is the order of the rolls. If the generator is 2, 4, 6, 1, 3, 5, then the sixth roll is 5, the seventh is 2 again, then 4, then 6. A discarded roll still consumes one value from the generator.
Your score is the value of S when the game ends. Given N and the generator, compute the score.
The first line contains the number of games T (1≤T≤100). Each game is given on two lines. The first line contains N (1≤N≤1000). The second line contains the generator, six integers that are the numbers 1 to 6 in some order.
For each game, print its final score on its own line.