P22451 [CodeChef] COLGLF2
普及
题目描述

Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

Chef has just started watching Game of Thrones, and he wants to first calculate the exact time (in minutes) that it'll take him to complete the series.

The series has $S$ seasons, and the $i^{th}$ season has $E_{i}$ episodes, each of which are $L_{i,1}, L_{i,2}, \ldots, L_{i,E_{i}}$ minutes long. Note that these $L_{i,j}$ include the duration of the beginning intro song in each episode. The streaming service that he uses, allows Chef to skip the intro song. The intro song changes slightly each season, and so he wants to watch the intro song in the first episode of each season, but he'll skip it in all other episodes of that season (yes, we know, a sacrilege!). You know that the intro song lasts for $Q_{i}$ minutes in the $i^{th}$ season.

Find the total time in minutes, that he has to watch.

------ Input: ------

First line will contain a single integer, $T$, denoting the number of testcases. Then the testcases follow.
The first line of each testcase will contain a single integer $S$, denoting the total number of seasons.
The second line contains $S$ space separated integers, $Q_{1}, Q_{2}, \ldots, Q_{S}$, where $Q_{i}$ denotes the duration of the intro song in the $i^{th}$ season.
The $i^{th}$ of the next $S$ lines contains $E_{i} + 1$ space separated integers, where the first integer is $E_{i}$, denoting the number of episodes in the $i^{th}$ season. That is followed by the duration of each of the $E_{i}$ episodes, $L_{i,1}, L_{i,2}, \ldots, L_{i,E_{i}}$.

------ Output: ------
For each testcase, output the answer in a single line.

------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ S ≤ 10^{5}$
$2 ≤ L_{i,j} ≤ 10^{5}$
$1 ≤ E_{i}$
Sum of all $E_{i}$ in a single testcase is at most $10^{5}$
$1 ≤ Q_{i} < L_{i,j}$, for all valid $j$.

----- Sample Input 1 ------
1
2
1 2
1 2
2 3 4
----- Sample Output 1 ------
7
----- explanation 1 ------
1 in the beginning denotes there is only $1$ test case.

Testcase 1:

There are $2$ seasons. The intro song in each of the first season episodes lasts for $1$ minute, and the intro song in the second season episodes lasts for $2$ minutes each.

For the first season, since there is only $1$ episode, Chef will be watching it completely - $2$ minutes.

For the second season, Chef will be watching the first episode completely ($3$ minutes) and will skip the intro song of the second episode ($4 - 2 = 2$ minutes).

So, the total time spent is $2 + (3 + (4 - 2)) = 7$ minutes.

----- Sample Input 2 ------
2
1
10
5 11 11 11 11 11
5
10 10 10 10 10
1 11
1 11
1 11
1 11
1 11
----- Sample Output 2 ------
15
55
----- explanation 2 ------
2 in the beginning denotes there are $2$ test cases.

Testcase 1:

There is only $1$ season having intro song for $10$ minutes.

Chef will have to watch the entire first episode including the intro song and will be skipping the same in further seasons.

So, the total time spent is $(11 + (11 - 10) * 4) = 15$ minutes.

Testcase 2:

There are total $5$ seasons. The intro song in every season lasts for $10$ minutes.

For each of the five seasons, since there is only $1$ episode, Chef will be watching all of them completely - $11$ minutes each.

So, the total time spent is $11 * 5 = 55$ minutes.

输入格式

见题面

输出格式

见题面

样例输入
1 2 1 2 1 2 2 3 4
样例输出
7