Theme Park Roller Coaster

No attempts yetTime limit5sMemory limit512 MB

Problem

The roller coaster at the theme park has a line all day. Some people come alone. Others come as one group and will board only if the whole group rides together. Everyone who rides gets back in line afterwards, and a ride costs 1 Euro per person. Compute how much money the roller coaster takes in today.

The roller coaster seats at most kk people per ride. People queue in groups. Groups board one at a time from the front of the queue until no groups are left or the next group does not fit in the remaining seats, and then the coaster departs even if seats are empty. A smaller group standing further back never skips ahead. When the ride ends, the groups that rode return to the back of the queue in the order they boarded. The coaster runs RR times in a day.

Take R=4R = 4, k=6k = 6 and group sizes 1, 4, 2, 1 from the front. On the first ride the first two groups [1, 4] board and one seat stays empty. The group of 2 does not fit, and the group of 1 behind it cannot skip ahead. The queue is now 2, 1, 1, 4. The second ride carries [2, 1, 1], which is 4 people, and the queue becomes 4, 2, 1, 1. The third ride carries [4, 2], which is 6 people, and the queue becomes 1, 1, 4, 2. The fourth ride carries [1, 1, 4], which is 6 people. The day earns 21 Euros in total.

Input

The first line contains the number of test cases TT. Each test case then takes 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 ii-th group in queue order.

Limits

  • 1T501 \le T \le 50
  • 1R1081 \le R \le 10^8
  • 1k1091 \le k \le 10^9
  • 1N10001 \le N \le 1000
  • 1gi1071 \le g_i \le 10^7
  • gikg_i \le k for every ii

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 roller coaster earned that day.