Theme Park (Small)

Groups board a k-seat coaster in queue order without skipping, riders rejoin the back, and total earnings over R runs are computed.

Easy3SimulationQueueInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

The roller coaster holds kk people at once. People queue for it in groups, and a group only boards if all of its members can ride together.

Boarding goes group by group from the front of the queue. If the next group fits in the remaining seats, it boards and the next group is considered. If the next group does not fit, boarding stops there, and a smaller group further back never jumps ahead. Once no group is left or the next group does not fit, the coaster departs even with empty seats.

Everyone who rides wants to ride again, so the passengers rejoin the back of the queue in the same order they boarded. The coaster runs RR times a day and a ride costs 1 Euro per person. Compute how much money the coaster makes in a day.

Take R=4R = 4, k=6k = 6, and four groups of sizes 1, 4, 2, 1 in that order. On the first run the first two groups, 1 and 4, ride and one seat stays empty: the group of 2 does not fit, and the group of 1 behind it cannot jump ahead. The queue is now 2, 1, 1, 4. On the second run the groups 2, 1, 1 ride, carrying 4 people, and the queue becomes 4, 2, 1, 1. On the third run the groups 4 and 2 ride, carrying 6 people, and the queue becomes 1, 1, 4, 2. On the last run the groups 1, 1, 4 ride, carrying 6 people. The coaster makes 21 Euros that day.

Input

The first line contains the number of test cases TT.

Each test case consists of two lines. The first line contains three space separated integers RR, kk, and NN. The second line contains NN space separated integers g0,g1,,gN1g_0, g_1, \dots, g_{N-1}, where gig_i is the size of the group standing i+1i+1 from the front of the queue.

Limits

  • 1T501 \le T \le 50
  • 1R10001 \le R \le 1000
  • 1k1001 \le k \le 100
  • 1N101 \le N \le 10
  • 1gi101 \le g_i \le 10
  • gikg_i \le k

Output

For each test case, print one line in the form "Case #x: y", where xx is the test case number starting from 1 and yy is the number of Euros the coaster makes.