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.

Difficulty: MEDIUM
Tags: Constructive algorithms

Implementation

Complete search

Mathematics

Ad-hoc
Skill types: Complete search

Starter code:

Solutions:
t = int(input())
while t > 0:
	s = int(input())
	l = list(map(int, input().split()))
	k = []
	m = 0
	ans = 0
	for i in range(s):
		l1 = list(map(int, input().split()))
		k.append(l1)
	for i in range(s):
		m = m + sum(k[i])
		m = m - k[i][0]
	for i in range(s):
		ans = ans + l[i] * (k[i][0] - 1)
	ans = m - ans
	print(ans)
	t = t - 1


for _ in range(int(input())):
	s = int(input())
	q = list(map(int, input().split()))
	ans = 0
	for i in range(s):
		e = list(map(int, input().split()))
		ans = ans + sum(e[1:])
		ans = ans - (len(e) - 2) * q[i]
	print(ans)


t = int(input())
for z in range(t):
	s = int(input())
	song = list(map(int, input().split()))
	tot = 0
	for i in range(s):
		e = list(map(int, input().split()))
		tot = tot + sum(e) - (len(e) - 2) * song[i] - e[0]
	print(tot)


for _ in range(int(input())):
	S = int(input())
	op = list(map(int, input().split()))
	ans = 0
	for s in range(S):
		ep = list(map(int, input().split()))
		E = ep.pop(0)
		ans += sum(ep)
		ans -= (E - 1) * op[s]
	print(ans)


t = int(input())
for i in range(t):
	total_time = 0
	S = int(input())
	Q_list = list(map(int, input().split()))
	for season in range(S):
		information_list = list(map(int, input().split()))
		Ei = information_list[0]
		list_of_lengths_of_episodes = information_list[1:]
		total_time += sum(list_of_lengths_of_episodes) - (Ei - 1) * Q_list[season]
	print(total_time)


t = int(input())
for _ in range(t):
	suma = 0
	n = int(input())
	intro = list(map(int, input().split()))
	if len(intro) == 1:
		for i in range(n):
			flag = 0
			list1 = list(map(int, input().split()))
			for j in range(1, len(list1)):
				if list1[j] <= intro[0]:
					suma = suma + list1[j]
				elif list1[j] > intro[0] and flag == 0:
					suma = suma + list1[j]
					flag = 1
				elif list1[j] > intro[0] and flag == 1:
					suma = suma + list1[j] - intro[0]
	else:
		for i in range(n):
			flag = 0
			list1 = list(map(int, input().split()))
			for j in range(1, len(list1)):
				if list1[j] <= intro[i]:
					suma = suma + list1[j]
				elif list1[j] > intro[i] and flag == 0:
					suma = suma + list1[j]
					flag = 1
				elif list1[j] > intro[i] and flag == 1:
					suma = suma + list1[j] - intro[i]
	print(suma)


for _ in range(int(input())):
	S = int(input())
	Q = list(map(int, input().split()))
	tot = sum(Q)
	for i in range(S):
		E = list(map(int, input().split()))
		tot += sum(E, start=-E[0]) - (len(E) - 1) * Q[i]
	print(tot)


T = int(input())
for i in range(T):
	s = int(input())
	q = list(map(int, input().split()))
	intro_time = sum(q)
	time = 0
	for i in range(s):
		li = list(map(int, input().split()))
		li1 = li[1:]
		time += sum(li1) - q[i] * (len(li1) - 1)
	print(time)


cases = int(input())
for i in range(cases):
	seasons = int(input())
	intro_duration = list(map(int, input().split()))
	time_spent = 0
	for j in range(seasons):
		time_pass = intro_duration[j]
		x = list(map(int, input().split()))
		time_spent = time_spent + x[1]
		for k in range(2, len(x)):
			time_spent = time_spent + (x[k] - time_pass)
	print(time_spent)


t = int(input())
while t:
	n = int(input())
	li = list(map(int, input().split()))
	final = 0
	for x in range(0, n):
		m = list(map(int, input().split()))
		ans = sum(m[1:])
		ans -= len(m[2:]) * li[x]
		final += ans
	print(final)
	t -= 1


t = int(input())
for i in range(t):
	s = int(input())
	Q_list = list(map(int, input().split()))
	total_time = 0
	for j in range(s):
		E_list = list(map(int, input().split()))
		total_time += sum(E_list[1:]) - E_list[0] * Q_list[j] + Q_list[j]
	print(total_time)


t = int(input())
for i in range(t):
	s = int(input())
	q = list(map(int, input().split()))
	total_time = 0
	for j in range(s):
		(e, *l) = list(map(int, input().split()))
		total_time += sum(l) - e * q[j] + q[j]
	print(total_time)


for _ in range(int(input())):
	sd = int(input())
	qd = list(map(int, input().split()))
	ld = []
	for i in range(len(qd)):
		kd = list(map(int, input().split()))
		ld.append(kd)
	sum = 0
	for i in range(len(ld)):
		for j in range(1, len(ld[i])):
			if j == 1:
				sum += ld[i][j]
			else:
				sum += ld[i][j] - qd[i]
	print(sum)


for _ in range(int(input())):
	s = int(input())
	q = list(map(int, input().split()))
	l = []
	for i in range(len(q)):
		k = list(map(int, input().split()))
		l.append(k)
	sum = 0
	for i in range(len(l)):
		for j in range(1, len(l[i])):
			if j == 1:
				sum += l[i][j]
			else:
				sum += l[i][j] - q[i]
	print(sum)


for _ in range(int(input())):
	s = int(input())
	intro = list(map(int, input().split()))
	watch_time = sum(intro)
	for i in range(s):
		ep = list(map(int, input().split()))
		watch_time += sum((x - intro[i] for x in ep[1:]))
	print(watch_time)


for _ in range(int(input())):
	s = int(input())
	q = list(map(int, input().split(' ')))
	count = 0
	for i in range(s):
		times = list(map(int, input().split(' ')))
		count += sum(times) - times[0] - (len(times) - 2) * q[i]
	print(count)


tests = int(input())
for i in range(tests):
	seasons = int(input())
	intro_list = list(map(int, input().split()))
	ans = 0
	for j in range(seasons):
		(num, *episodes) = list(map(int, input().split()))
		ans += sum(episodes) - num * intro_list[j] + intro_list[j]
	print(ans)


def soln(s, q, epis):
	res = 0
	for i in range(s):
		res += sum(epis[i][1:]) - (epis[i][0] - 1) * q[i]
	return res
for _ in range(int(input())):
	s = int(input())
	q = list(map(int, input().split()))
	epis = []
	for i in range(s):
		epis.append(list(map(int, input().split())))
	print(soln(s, q, epis))


class SolveQuestion:

	def solve(self):
		for t in range(int(input())):
			S = int(input())
			Q = list(map(int, input().split()))
			E = []
			for e in range(S):
				E.append(list(map(int, input().split())))
			Ans = 0
			for i in range(S):
				e = E[i]
				elen = e[0]
				enew = e[1:]
				Ans += sum(enew) - (elen - 1) * Q[i]
			print(Ans)
OBJ = SolveQuestion()
OBJ.solve()


for _ in range(int(input())):
	s = int(input())
	op_l = list(map(int, input().split()))
	total_t = 0
	for i in range(s):
		ep_l = list(map(int, input().split()))
		if ep_l[0] == 1:
			total_t += ep_l[1]
		else:
			total_t += ep_l[1] + sum(ep_l[2:]) - (ep_l[0] - 1) * op_l[i]
	print(total_t)


t = int(input())
out = []
for i in range(t):
	s = int(input())
	q = [int(j) for j in input().split()]
	S = 0
	for k in range(s):
		l = [int(j) for j in input().split()]
		n = l[0]
		a = sum(l) - n - q[k] * (n - 1)
		S += a
	out.append(S)
for i in out:
	print(i)


for T in range(int(input())):
	S = int(input())
	Q = list(map(int, input().split()))
	s = []
	for i in range(S):
		E = list(map(int, input().split()))
		s.append(sum(E) - E[0] - Q[i] * (E[0] - 1))
	print(sum(s))


t = int(input())
for _ in range(t):
	s = int(input())
	q = list(map(int, input().split()))
	e = []
	mi = 0
	for i in range(s):
		l = list(map(int, input().split()))[1:]
		e.append(l)
	for j in range(len(e)):
		for k in range(len(e[j])):
			if k == 0:
				mi += e[j][k]
			elif k != 0:
				mi += e[j][k] - q[j]
	print(mi)


for _ in range(int(input())):
	s = int(input())
	qi = list(map(int, input().split()))[:s]
	ans = 0
	for i in range(s):
		episodes = list(map(int, input().split()))
		(episode, minutes) = (episodes[0], episodes[1:])
		ans += minutes[0] + sum(minutes[1:]) - qi[i] * (len(minutes) - 1)
	print(ans)


for i in range(int(input())):
	S = int(input())
	time = 0
	intros = list(map(int, input().split()))
	for i in range(S):
		(E, *episodes) = list(map(int, input().split()))
		time += sum(episodes) - (E - 1) * intros[i]
	print(time)


for _ in range(int(input())):
	n = int(input())
	t = list(map(int, input().lstrip().split()))
	w = sum(t)
	tot = 0
	for i in range(n):
		arr = list(map(int, input().lstrip().split()))
		noofepi = arr[0]
		for j in range(1, len(arr)):
			tot += arr[j] - t[i]
	print(w + tot)


import math
for _ in range(int(input())):
	s = int(input())
	q = list(map(int, input().split()))
	e = []
	for _ in range(s):
		e.append(list(map(int, input().split())))
	ans = 0
	for i in range(s):
		for j in range(2, e[i][0] + 1):
			ans += e[i][j] - q[i]
		ans += e[i][1]
	print(ans)


import math
from collections import Counter
for i in range(int(input())):
	S = int(input())
	int_song = list(map(int, input().split()))
	a = 0
	for i in range(S):
		seas = list(map(int, input().split()))
		if len(seas) > 2:
			a = a + (seas[1] + (sum(seas[2:]) - int_song[i] * len(seas[2:])))
		else:
			a = a + seas[1]
		seas.clear()
	print(a)
	a = 0


for _ in range(int(input())):
	s = int(input())
	q = list(map(int, input().split()))
	a = []
	for i in range(s):
		d = list(map(int, input().split()))
		a.append(d)
	time = 0
	for j in range(len(a)):
		if a[j][0] == 1:
			time += a[j][1]
			continue
		else:
			time += sum(a[j]) - a[j][0]
			time -= q[j] * (a[j][0] - 1)
	print(time)


T = int(input())
for i in range(T):
	s = int(input())
	q = list(map(int, input().split(' ')))
	time = 0
	for i in range(s):
		arr = list(map(int, input().split(' ')))
		if arr[0] == 1:
			time += sum(arr[1:])
		else:
			time += sum(arr[1:]) - q[i] * (arr[0] - 1)
	print(time)


for i in range(int(input())):
	s = int(input())
	q = [int(x) for x in input().split()]
	k = sum(q)
	for j in range(s):
		l = input().split()
		h = l.pop(0)
		k += sum([int(int(y) - q[j]) for y in l])
	print(int(k))


for _ in range(int(input())):
	s = int(input())
	intro = list(map(int, input().split(' ')))
	ep = []
	ep_no = []
	for i in range(s):
		ei = list(map(int, input().split(' ')))
		ep_no.append(ei[0])
		ep.append(ei[1:])
	time = 0
	for i in range(s):
		for j in range(ep_no[i]):
			if j == 0:
				time = time + ep[i][j]
			else:
				time = time + ep[i][j] - intro[i]
	print(time)


T = int(input())
for t in range(T):
	S = int(input())
	intro = list(map(int, input().split()))
	minutes = 0
	for s in range(S):
		E = list(map(int, input().split()))
		minutes = sum(E) - E[0] - intro[s] * (len(E) - 2) + minutes
	print(minutes)


t = int(input())
for i in range(t):
	seasons = int(input())
	intro_song = list(map(int, input().split()))
	time = 0
	for j in range(seasons):
		episodes = list(map(int, input().split()))
		time += episodes[1]
		for k in range(2, len(episodes)):
			time += episodes[k] - intro_song[j]
	print(time)


t = int(input())
for i in range(t):
	n = int(input())
	songDur = list(map(int, input().split()))
	totDur = 0
	for j in range(n):
		epi = list(map(int, input().split()))
		totDur += epi[1]
		if len(epi) > 2:
			for k in range(2, len(epi)):
				totDur = totDur + (epi[k] - songDur[j])
	print(totDur)


t = int(input())
for _ in range(t):
	s = int(input())
	q = input().split()
	q = list(map(int, q))
	e = []
	for i in range(s):
		a = input().split()
		a = list(map(int, a))
		e.append(a)
	time = 0
	for i in range(s):
		for j in range(1, e[i][0] + 1):
			if j == 1:
				time += e[i][j]
			else:
				time += e[i][j] - q[i]
	print(time)


for _ in range(int(input())):
	sn = int(input())
	qi = list(map(int, input().split()))
	T = 0
	for si in range(sn):
		l = list(map(int, input().split()))
		en = l[0] - 1
		ei = l[1:].copy()
		t = ei[0]
		i = 1
		while en != 0:
			t += ei[i] - qi[si]
			i += 1
			en -= 1
		T += t
	print(T)


T = int(input())
for i in range(0, T):
	s = int(input())
	q = input().split()
	ans = 0
	for j in range(0, s):
		a = input().split()
		d = int(a[0])
		for k in range(1, d + 1):
			ans += int(a[k])
		ans = ans - (d - 1) * int(q[j])
	print(ans)


for _ in range(int(input())):
	s = int(input())
	intro_song_per_season = list(map(int, input().split()))
	time_escaped = 0
	for i in range(s):
		no_episodes_duration_episodes = list(map(int, input().split()))
		no_episodes_duration_episodes.reverse()
		no_episodes_duration_episodes.pop()
		introsong = intro_song_per_season[i]
		for epi_song in no_episodes_duration_episodes:
			time_escaped += epi_song - introsong
		time_escaped += introsong
	print(time_escaped)


for i in range(int(input())):
	s = int(input())
	q = list(map(int, input().split()))
	t = 0
	for i in range(s):
		intro = q[i]
		e = list(map(int, input().split()))
		t += e[1]
		for i in range(2, len(e)):
			t += e[i] - intro
	print(t)


for _ in range(int(input())):
	x = int(input())
	q = list(map(int, input().split()))
	s = 0
	for i in range(x):
		l = list(map(int, input().split()))
		if l[0] == 1:
			s += l[1]
		else:
			for j in range(1, l[0] + 1):
				if j == 1:
					s += l[1]
				else:
					s += l[j] - q[i]
	print(s)


import functools
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	su = 0
	for i in range(1, n + 1):
		b = list(map(int, input().split()))
		if len(b) >= 3:
			c = b[1]
			b = b[2:]
			b = list(map(lambda x: x - a[i - 1], b))
			su = c + functools.reduce(lambda a, c: a + c, b) + su
		else:
			su = su + b[1]
	print(su)


t = int(input())
for i in range(t):
	total = 0
	s = int(input())
	q = list(map(int, input().split(' ')))
	for j in range(s):
		e = list(map(int, input().split(' ')))
		total += sum(e) - e[0] - q[j] * (len(e) - 2)
	print(total)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		p = list(map(int, input().split()))
		a = p[0]
		ans += p[1]
		for j in range(2, a + 1):
			ans += abs(p[j] - l[i])
	print(ans)


t = int(input())
for _ in range(t):
	s = int(input())
	songs = list(map(int, input().split(' ')))
	durations = []
	for i in range(s):
		episodes = list(map(int, input().split(' ')))
		durations.append(songs[i] + sum(episodes[1:]) - episodes[0] * songs[i])
	print(sum(durations))


testcases = int(input())
for testcase in range(testcases):
	seasons = int(input())
	minutes = list(map(int, input().split(' ')))
	answer = 0
	for season in range(seasons):
		episodes = list(map(int, input().split(' ')))
		for (count, episode_time) in enumerate(episodes):
			if count == 0:
				pass
			elif count == 1:
				answer += episode_time
			else:
				answer += episode_time - minutes[season]
	print(answer)


testcases = int(input())
for testcase in range(testcases):
	seasons = int(input())
	minutes = list(map(int, input().split(' ')))
	answer = 0
	for season in range(seasons):
		episodes = list(map(int, input().split(' ')))
		for episode in range(1, episodes[0] + 1):
			if episode == 1:
				answer += episodes[episode]
			else:
				answer += episodes[episode] - minutes[season]
	print(answer)


for _ in range(int(input())):
	s = int(input())
	Is = list(map(int, input().split()))
	cnt = 0
	x = 0
	for i in range(s):
		Es = list(map(int, input().split()))
		cnt += Es[1]
		for j in range(len(Es)):
			if j > 1:
				cnt += Es[j] - Is[x]
		x += 1
	print(cnt)


for _ in range(int(input())):
	s = int(input())
	q = list(map(int, input().split()))
	td = 0
	for k in range(s):
		ep = list(map(int, input().split()))
		td += sum(ep[1:]) - q[k] * (ep[0] - 1)
	print(td)


t = int(input())
for _ in range(t):
	summer = 0
	s = int(input())
	q = list(map(int, input().split()))
	for i in range(s):
		data = list(map(int, input().split()))
		j = 0
		for l in range(data[0]):
			if j == 0:
				summer = summer + data[1 + j]
			else:
				summer = summer + data[1 + j] - q[i]
			j += 1
	print(summer)


t = int(input())
for o in range(t):
	s = int(input())
	q = [k for k in map(int, input().split())]
	time = 0
	for i in range(s):
		arr = [j for j in map(int, input().split())]
		epi = arr[0]
		arr = arr[1:]
		summ = sum(arr)
		time += summ - q[i] * (epi - 1)
	print(time)


t = int(input())
for i in range(t):
	s = int(input())
	inso = list(map(int, input().split(' ')))
	sum = 0
	for j in range(s):
		q = list(map(int, input().split(' ')))
		r = q.pop(0)
		for k in range(r):
			if k == 0:
				sum += q[k]
			else:
				w = q[k] - inso[j]
				sum += w
	print(sum)


t = int(input())
for case in range(t):
	s = int(input())
	introLength = list(map(int, input().split()))
	ans = 0
	for i in range(s):
		epLength = list(map(int, input().split()))
		ans += epLength[1]
		ans += sum(epLength[2:])
		ans -= (epLength[0] - 1) * introLength[i]
	print(ans)


for _ in range(int(input())):
	s = int(input())
	q = list(map(int, input().split()))
	total_watch_time = 0
	for i in range(s):
		episodes = list(map(int, input().split()))
		season_watch_time = sum(episodes[1:]) - q[i] * (episodes[0] - 1)
		total_watch_time += season_watch_time
	print(total_watch_time)


for _ in range(int(input())):
	s = int(input())
	q = list(map(int, input().split()))
	e = []
	minutes = 0
	for i in range(s):
		e.append(list(map(int, input().split())))
		minutes += sum(e[i]) - (e[i][0] + (e[i][0] - 1) * q[i])
	print(minutes)


T = int(input())
for tc in range(T):
	s = int(input())
	q = list(map(int, input().split(' ')))
	ans = 0
	for i in range(s):
		l = list(map(int, input().split(' ')))
		ans += l[1]
		if l[0] > 1:
			ans += sum(l[2:]) - q[i] * (l[0] - 1)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	introdur = [int(x) for x in input().split()]
	time = 0
	i = 0
	while n:
		episode = [int(x) for x in input().split()]
		lens = len(episode) - 2
		sums = sum(episode)
		sums = sums - episode[1] - episode[0]
		time += episode[1] + (sums - lens * introdur[i])
		i += 1
		n -= 1
	print(time)


T = int(input())
for _ in range(T):
	seasons = int(input())
	intro_songs_duration = list(map(int, input().split()))
	total_time_watched = 0
	for song in intro_songs_duration:
		episode_arr = list(map(int, input().split()))
		number_of_episodes = episode_arr[0]
		song_timer_deduction = song * (number_of_episodes - 1)
		sum_of_length_of_all_episodes = sum(list(episode_arr[1:]))
		total_time_watched = total_time_watched + sum_of_length_of_all_episodes - song_timer_deduction
	print(total_time_watched)


t = int(input())
while t:
	t -= 1
	season = int(input())
	intro_length = [int(x) for x in input().split()]
	time = 0
	i = 0
	while season:
		episode = [int(x) for x in input().split()]
		time += sum(episode[1:])
		time = time - intro_length[i] * (episode[0] - 1)
		season -= 1
		i += 1
	print(time)


for _ in range(int(input())):
	ans = 0
	no_of_season = int(input())
	intro = list(map(int, input().split()))
	for i in range(no_of_season):
		x = list(map(int, input().split()))
		ans += x[1]
		if x[0] > 1:
			ans += sum(x[2:]) - intro[i] * (x[0] - 1)
	print(ans)


for _ in range(int(input())):
	S = int(input())
	Intro = list(map(int, input().split()))
	Time = 0
	for i in range(S):
		E = list(map(int, input().split()))
		Time += sum(E[1:]) - (E[0] - 1) * Intro[i]
	print(Time)


t = int(input())
for i in range(t):
	s = int(input())
	q = list(map(int, input().split()))[:s]
	time = 0
	for k in range(s):
		l = list(map(int, input().split()))
		l.reverse()
		e = l.pop()
		e1 = l.pop()
		time += e1
		l = [l[n] - q[k] for n in range(len(l))]
		time += sum(l)
	print(time)


for test_cases in range(int(input())):
	seasons = int(input())
	intros = list(map(int, input().split()))
	total_time = 0
	total_time += sum(intros)
	for i in range(seasons):
		episodes = list(map(int, input().split()))
		total_time += sum(episodes[1:]) - episodes[0] * intros[i]
	print(total_time)


for _ in range(int(input())):
	n = int(input())
	q = list(map(int, input().split(' ')))
	count = 0
	for i in range(n):
		intro = q[i]
		epi = list(map(int, input().split(' ')))
		total = sum(epi[1:])
		epi_from_2nd = epi[2:]
		total -= len(epi_from_2nd) * intro
		count += total
	print(count)


test = int(input())
for i in range(test):
	seasons = int(input())
	song_input = input().split(' ')
	song_dur = list(map(int, song_input))
	total_dur = 0
	for j in range(seasons):
		epi_input = input().split(' ')
		epi_data = list(map(int, epi_input))
		total_episodes = epi_data.pop(0)
		total_dur += sum(epi_data) - (total_episodes - 1) * song_dur[j]
	print(total_dur)


for _ in range(int(input())):
	s = int(input())
	q = list(map(int, input().split()))
	E = []
	S1 = []
	ans = 0
	for i in range(s):
		e = list(map(int, input().split()))
		l = e[1:]
		ans += sum(l) - (e[0] - 1) * q[i]
	print(ans)


for _ in range(int(input())):
	s = int(input())
	q = list(map(int, input().split(' ')))
	time = 0
	for i in range(s):
		e = list(map(int, input().split(' ')))
		time += e[1]
		for elm in e[2:]:
			time += elm - q[i]
	print(time)


for k in range(int(input())):
	s = int(input())
	q = list(map(int, input().split(' ')))
	t = 0
	for i in range(s):
		el = list(map(int, input().split(' ')))
		su = 0
		if el[0] == 1:
			su = sum(el) - el[0]
			t = t + su
		else:
			su = sum(el) - el[0] * q[i] + q[i] - el[0]
			t = t + su
	print(t)