Under construction

Still being built. Things may change

Wildest Dreams

Time limit1sMemory limit1024 MB

Summary
For each CD and day, compute the seconds Arup spends playing Anya's favorite track, given the track lengths and her driving segments.
Level

Medium4 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Problem

Arup secretly loves Taylor Swift's music. To cover for it, he tells people that his three-year-old daughter Anya makes him play the CD in the car whenever she is in it. In fact, he also listens to the CD when Anya is not in the car.

Anya usually obsesses over one song and asks Arup to play it on infinite repeat while she is in the car. When she gets in, Arup starts that song from the beginning, and it repeats until she gets out. After she gets out, Arup plays the CD in its natural order, and after the last track it returns to track 1. If the favorite track is playing when Anya gets out, that track plays to its end before the CD moves on. If the song ends exactly when she gets out, the next track plays, and the favorite song does not start over.

Given the track lengths for each CD, compute for each day the total time Arup listens to Anya's favorite track.

Input

The first line contains nn (1≤n≤501 \le n \le 50), the number of CDs.

For each CD, the first line contains tt (1≤t≤201 \le t \le 20), the number of tracks, and kk (1≤k≤t1 \le k \le t), the track Anya is obsessed with. The second line contains tt positive integers, the length of each track in seconds, in order. No CD is longer than 86400 seconds.

The next line contains dd (1≤d≤1001 \le d \le 100), the number of days. Each of the next dd lines starts with sis_i (1≤si≤201 \le s_i \le 20), the number of driving segments, followed by sis_i positive integers, the lengths of the segments in seconds. Anya is in the car during the odd-numbered segments (first, third, and so on). The segments of each day sum to at most 86400.

Output

For each CD, print a line "CD #c:", where c is the CD number starting at 1. Then print one line for each of its dd days with the number of seconds Arup spent listening to Anya's favorite song that day. Print a blank line after each CD's output.

Examples1

  1. Example 1

    Input
    2
    13 9
    212 231 231 235 193 219 207 211 220 247 250 195 270
    4
    3 1000 900 1000
    3 10000 10000 10000
    1 2000
    2 500 600
    2 2
    100 200
    5
    1 70
    5 300 277 131 10000 58
    2 200 50
    2 201 50
    2 199 50
    
    Expected output
    CD #1:
    2100
    20780
    2000
    660
    
    CD #2:
    70
    7335
    200
    251
    200