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

During a fight with the Joker, Batman's eyes lose the capability to distinguish between some pairs of colors.

Each color has an integer ID from 1 to N. There are M lists where each color belongs to exactly one list. Batman can distinguish colors belonging to different lists, but he cannot distinguish colors belonging to the same list.

Given a strip of L colors, find the different number of segments Batman will see as a result of his disability. Two positions of the strip are said to belong to the same segment if they are adjacent on the strip and Batman cannot distinguish their colors. See the sample explanation for clarity.

------ Input Format ------ 

- The first line contains an integer T, the number of test cases. Then the test cases follow. 
- The first line contain three integers N, M, and L - the number of colors, the number of lists, and the length of the strip, respectively.
- Each of the next M lines describes a list. It begins with an integer K_{i}, the length of the i-th list, followed by K_{i} integers A_{i1}, A_{i2}, \ldots, A_{iK_{i}} - the color IDs of the i-th list.
- The next line contains L integers S_{1}, S_{2}, \ldots, S_{L} - the color IDs of the strip.

------ Output Format ------ 

For each test case, output in a single line the answer to the problem.

------ Constraints ------ 

$1 ≤ T ≤ 10$
$1 ≤ M ≤ N ≤ 10^{5}$
$1 ≤ L ≤ 10^{5}$
$1 ≤ K_{i}, A_{ij}, S_{i} ≤ N$
$\sum_{i=1}^{M} K_{i} = N$
- Each color belongs to exactly one list.

----- Sample Input 1 ------ 
3
2 2 2
1 2
1 1
2 1
2 2 4
1 1
1 2
1 2 2 1
3 2 3
2 1 3
1 2
1 3 1
----- Sample Output 1 ------ 
2
3
1
----- explanation 1 ------ 
Test Case 1: Since the strip is composed of colors from different lists, the answer is the length of the strip, which is $2$.

Test Case 2: The first and second index have colors from different lists, and the third and fourth index have colors from different lists. So the strip is seen to be composed of $3$ consecutive segments.

Test Case 3: Since the strip is composed of colors from the same list, the answer is $1$ segment.

Difficulty: MEDIUM_HARD
Tags: Implementation

Starter code:

Solutions:
t = int(input())
for _ in range(t):
	(n, m, l) = map(int, input().split())
	lists = {}
	for i in range(m):
		a = list(map(int, input().split()))
		a = a[1:]
		for c in a:
			lists[c] = i
	s = list(map(int, input().split()))
	for i in range(l):
		s[i] = lists[s[i]]
	segs = 0
	ci = s[0]
	for j in range(1, l):
		if s[j] != ci:
			segs += 1
			ci = s[j]
	print(segs + 1)


for k in range(int(input())):
	(n, m, l) = map(int, input().split())
	d = [0] * n
	for c in range(m):
		a = [int(k) for k in input().split()]
		for k in range(1, len(a)):
			d[a[k] - 1] = c + 1
	A = [int(k) for k in input().split()]
	count = 1
	k = 1
	while k < l:
		if d[A[k - 1] - 1] == d[A[k] - 1]:
			pass
		else:
			count += 1
		k += 1
	print(count)


t = int(input())
for _ in range(t):
	(n, m, l) = map(int, input().split())
	d = {}
	M = [0] * (n + 1)
	for i in range(m):
		N = list(map(int, input().split()))
		for j in range(1, len(N)):
			M[N[j]] = i + 1
	L = list(map(int, input().split()))
	ans = 1
	for i in range(1, len(L)):
		if M[L[i]] == M[L[i - 1]]:
			pass
		else:
			ans += 1
	print(ans)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	n_array = [0 for _ in range(n)]
	for i in range(m):
		temp_list = list(map(int, input().split()))
		for j in temp_list[1:]:
			n_array[j - 1] = i + 1
	l_array = map(int, input().split())
	curr_colour = 0
	ans = 0
	for i in l_array:
		if n_array[i - 1] != curr_colour:
			curr_colour = n_array[i - 1]
			ans += 1
	print(ans)


for t in range(int(input())):
	(a, numlist, b) = input().split()
	list_of_lists = {}
	for i in range(0, int(numlist)):
		input_list = input().split()[1:]
		for j in input_list:
			list_of_lists[j] = i
	Colors = input().split()
	c = list_of_lists[Colors[0]]
	countStrips = 1
	for i in Colors:
		if list_of_lists[i] != c:
			countStrips += 1
		c = list_of_lists[i]
	print(countStrips)


for t in range(int(input())):
	(a, numlist, c) = input().split()
	list_of_lists = {}
	for i in range(0, int(numlist)):
		input_list = input().split()[1:]
		for j in input_list:
			list_of_lists[j] = i
	stripColors = input().split()
	current = list_of_lists[stripColors[0]]
	countStrips = 1
	for i in stripColors:
		if list_of_lists[i] != current:
			countStrips += 1
		current = list_of_lists[i]
	print(countStrips)


def func():
	(n, m, l) = map(int, input().split(' '))
	A = [0 for i in range(n + 1)]
	for i in range(1, m + 1):
		arr = list(input().split(' '))
		k = int(arr[0])
		for j in range(1, k + 1):
			color = int(arr[j])
			A[color] = i
	L = input().split(' ')
	cnt = 1
	for i in range(len(L)):
		temp = int(L[i])
		L[i] = A[temp]
	for i in range(len(L) - 1):
		if L[i] != L[i + 1]:
			cnt += 1
	print(cnt)
t = int(input())
while t:
	func()
	t -= 1


for t in range(int(input())):
	(a, number_of_lists, c) = input().split()
	list_of_lists = {}
	for i in range(0, int(number_of_lists)):
		input_list = input().split()[1:]
		for ii in input_list:
			list_of_lists[ii] = i
	stripColors = input().split()
	current = list_of_lists[stripColors[0]]
	countStrips = 1
	for i in stripColors:
		if list_of_lists[i] != current:
			countStrips += 1
		current = list_of_lists[i]
	print(countStrips)


for i in range(int(input())):
	(n, m, l) = map(int, input().split())
	arr = [0] * n
	for i in range(m):
		x = list(map(int, input().split()[1:]))
		for j in x:
			arr[j - 1] = i
	strip = list(map(int, input().split()))
	count = 1
	for i in range(1, len(strip)):
		if arr[strip[i] - 1] != arr[strip[i - 1] - 1]:
			count += 1
	print(count)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	d = {}
	for __ in range(m):
		ls = list(map(int, input().split()))
		for i in range(len(ls)):
			d[ls[i]] = __ + 1
	lst = list(map(int, input().split()))
	prev = d[lst[0]]
	(curr, c) = (0, 1)
	for i in range(1, l):
		curr = d[lst[i]]
		if prev != curr:
			c += 1
		prev = curr
	print(c)


t = int(input())
for i in range(t):
	(n, m, l) = map(int, input().split())
	a = [0] * (n + 1)
	for j in range(m):
		b = list(map(int, input().split()))
		x = b[0]
		del b[0]
		for k in range(x):
			a[b[k]] = j + 1
	s = list(map(int, input().split()))
	c = 0
	d = 0
	for z in range(l):
		if a[s[z]] == d:
			continue
		else:
			c += 1
		d = a[s[z]]
	print(c)


n_test = int(input())
assert n_test >= 1 and n_test <= 10
for i in range(n_test):
	array = input()
	array = array.split()
	array = list(map(int, array))
	n = array[0]
	assert n >= 1 and n <= pow(10, 5)
	m = array[1]
	assert m >= 1 and m <= pow(10, 5)
	assert m >= 1 and m <= n
	l = array[2]
	assert l >= 1 and l <= pow(10, 5)
	d = {}
	for i in range(m):
		array = input()
		array = array.split()
		array = list(map(int, array))
		k = array[0]
		for j in range(k):
			d[array[j + 1]] = i + 1
	s = 1
	sequence = input()
	sequence = sequence.split()
	sequence = list(map(int, sequence))
	for i in range(l - 1):
		if d[sequence[i]] != d[sequence[i + 1]]:
			s = s + 1
	print(s)


for _ in range(int(input())):
	(N, M, L) = map(int, input().split())
	dictColors = {}
	for i in range(M):
		A = list(map(int, input().split()))
		for j in range(A.pop(0)):
			dictColors[A[j]] = i
	L = list(map(int, input().split()))
	cont = 1
	for i in range(len(L) - 1):
		if dictColors[L[i]] != dictColors[L[i + 1]]:
			cont += 1
	print(cont)


for _ in range(int(input())):
	(n, m, l) = map(int, input().strip().split())
	d = dict()
	for lists in range(m):
		li = [int(i) for i in input().split()]
		k = li.pop(0)
		for i in range(len(li)):
			d[li[i]] = lists
	strips = [int(i) for i in input().split()]
	count = 1
	for i in range(1, len(strips)):
		if d[strips[i]] != d[strips[i - 1]]:
			count += 1
	print(count)


ans = ''
for _ in range(int(input())):
	(n, m, l) = list(map(int, input().split()))
	d = {}
	for i in range(m):
		inp = list(map(int, input().split()))
		for j in range(1, len(inp)):
			d[inp[j]] = i
	inp = list(map(int, input().split()))
	prev = d[inp[0]]
	cnt = 1
	for num in inp:
		if d[num] != prev:
			cnt += 1
			prev = d[num]
	ans += str(cnt) + '\n'
print(ans)


t = int(input())
for _ in range(t):
	(n, m, l) = map(int, input().split())
	d = [0] * (n + 1)
	for i in range(m):
		foo = list(map(int, input().split()))
		x = foo[0]
		del foo[0]
		for j in range(x):
			d[foo[j]] = i + 1
	strip = list(map(int, input().split()))
	cnt = 0
	prev = -1
	for i in range(l):
		if d[strip[i]] == prev:
			continue
		else:
			cnt += 1
		prev = d[strip[i]]
	print(cnt)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	mapping = {}
	for i in range(m):
		a = list(map(int, input().split()))
		for j in range(1, a[0] + 1):
			mapping[a[j]] = i
	s = list(map(int, input().split()))
	(i, g) = (0, 0)
	while i < l:
		x = mapping[s[i]]
		while i < l and mapping[s[i]] == x:
			i += 1
		g += 1
	print(g)


tc = int(input())
for _ in range(tc):
	(n, m, l) = map(int, input().split())
	dic = {}
	for j in range(m):
		x = tuple(map(int, input().split()))
		for i in range(1, x[0] + 1):
			dic[x[i]] = j
	L = tuple(map(int, input().split()))
	count = 1
	for i in range(1, l):
		if dic[L[i]] != dic[L[i - 1]]:
			count += 1
	print(count)


t = int(input())
for i in range(t):
	(n, m, l) = map(int, input().split())
	list_m = [[]] * (m + 1)
	list_id = [0] * (n + 1)
	for j in range(1, m + 1):
		list_m[j] = list(map(int, input().split()))
		for k in range(1, len(list_m[j])):
			list_id[list_m[j][k]] = j
	list_l = list(map(int, input().split()))
	count = 0
	id = list_id[list_l[0]]
	for j in range(1, len(list_l)):
		if id != list_id[list_l[j]]:
			count += 1
		id = list_id[list_l[j]]
	print(count + 1)


T = int(input())
for i in range(1, T + 1):
	(N, M, L) = map(int, input().split())
	arr = [0] * N
	for i in range(M):
		k = list(map(int, input().split()[1:]))
		for j in k:
			arr[j - 1] = i
	strip = list(map(int, input().split()))
	c = 1
	for i in range(1, len(strip)):
		if arr[strip[i] - 1] != arr[strip[i - 1] - 1]:
			c = c + 1
	print(c)


dic = {}
t = int(input())
for tc in range(t):
	segments = 1
	strip = []
	(n, m, l) = tuple(map(int, input().split(' ')))
	for strip_no in range(1, m + 1):
		inp = map(int, input().split(' '))
		for color_id in inp:
			dic[color_id] = strip_no
	strip = list(map(int, input().split(' ')))
	for index in range(len(strip) - 1):
		if dic[strip[index]] != dic[strip[index + 1]]:
			segments += 1
		else:
			pass
	print(segments)


t = int(input())
while t > 0:
	(n, m, l) = map(int, input().split(' '))
	col = [0] * (n + 1)
	for j in range(m):
		x = list(map(int, input().split(' ')))
		for i in range(1, x[0] + 1):
			col[x[i]] = j + 1
	a = list(map(int, input().split(' ')))
	seg = 1
	i = 1
	while i < l:
		if col[a[i]] != col[a[i - 1]]:
			seg += 1
		i += 1
	print(seg)
	t -= 1


t = int(input())
for test in range(t):
	(n, m, l) = [int(x) for x in input().split()]
	colorList = []
	group = []
	for i in range(n):
		group.append(0)
	for i in range(m):
		colorInput = [int(x) for x in input().split()]
		actualColors = colorInput[1:]
		for j in range(len(actualColors)):
			group[actualColors[j] - 1] = i
		colorList.append(actualColors)
	strip = [int(x) for x in input().split()]
	count = 1
	for i in range(1, l):
		if group[strip[i] - 1] != group[strip[i - 1] - 1]:
			count += 1
	print(count)


for _ in range(int(input())):
	(n, m, l) = [int(x) for x in input().split()]
	d = dict()
	for i in range(m):
		a = [int(x) for x in input().split()]
		for j in a[1:]:
			d[j] = i
	a = [int(x) for x in input().split()]
	c = 1
	for i in range(1, l):
		if d[a[i]] != d[a[i - 1]]:
			c += 1
	print(c)


for t in range(int(input())):
	(n, m, l) = map(int, input().split())
	a = [0] * (n + 1)
	for i in range(m):
		b = [int(x) for x in input().split()]
		k = b[0]
		b = b[1:]
		for j in range(k):
			a[b[j]] = i + 1
	c = 0
	z = [0]
	s = [int(x) for x in input().split()]
	for i in range(l):
		z.append(a[s[i]])
		pre = z[i]
		cur = z[i + 1]
		if pre != cur:
			c += 1
	print(c)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	a = [0 for x in range(n + 1)]
	count = 1
	for i in range(m):
		k = list(map(int, input().split()))
		for j in k[1:]:
			a[j] = i
	l1 = list(map(int, input().split()))
	for x in range(1, l):
		if a[l1[x]] != a[l1[x - 1]]:
			count += 1
	print(count)


t = int(input())
from collections import Counter
for _ in range(t):
	c = Counter([])
	(n, m, l) = list(map(int, input().split()))
	arr = []
	for q in range(m):
		z = list(map(int, input().split()))
		arr.append(z)
	col = list(map(int, input().split()))
	for i in range(len(arr)):
		for j in range(1, len(arr[i])):
			c[arr[i][j]] = i + 1
	col = [c[item] for item in col]
	k = col[0]
	ans = 0
	for i in range(len(col)):
		if col[i] == k:
			continue
		else:
			ans += 1
			k = col[i]
	ans += 1
	print(ans)


def A():
	for _ in range(int(input())):
		(R1, W1, C1) = [int(x) for x in input().split()]
		(R2, W2, C2) = [int(x) for x in input().split()]
		(A, B) = (0, 0)
		if R1 > R2:
			A += 1
		else:
			B += 1
		if W1 > W2:
			A += 1
		else:
			B += 1
		if C1 > C2:
			A += 1
		else:
			B += 1
		print('A' if A > B else 'B')

def B():
	for _ in range(int(input())):
		(X, Y) = [int(x) for x in input().split()]
		s = X + Y
		if s >= 6:
			print(0)
		else:
			print((6 - s) / 6)

def C():
	for _ in range(int(input())):
		(N, M, L) = [int(x) for x in input().split()]
		A = [[int(x) for x in input().split()] for i in range(M)]
		S = [int(x) for x in input().split()]
		colors = [0] * (N + 1)
		for i in range(M):
			for color in A[i][1:]:
				colors[color] = i
		segments = 1
		for i in range(1, L):
			if colors[S[i - 1]] != colors[S[i]]:
				segments += 1
		print(segments)
C()


t = int(input())
for _ in range(t):
	count = 0
	(n, m, l) = map(int, input().split())
	lis1 = [0] * (n + 1)
	for i in range(1, m + 1):
		a = list(map(int, input().split()))
		q = a[1:]
		for p in q:
			lis1[p] = i
	s = list(map(int, input().split()))
	rr = 1
	while rr < len(s):
		if lis1[s[rr]] != lis1[s[rr - 1]]:
			count += 1
			rr += 1
		else:
			rr += 1
	print(count + 1)


test_case = int(input())
for i in range(test_case):
	(n, m, l) = map(int, input().split())
	arr = [0] * (n + 1)
	for j in range(1, m + 1):
		x = list(map(int, input().split()))
		for k in x[1:]:
			arr[k] = j
	l_arr = list(map(int, input().split()))
	for colors in range(l):
		l_arr[colors] = arr[l_arr[colors]]
	count = 1
	for element in range(1, l):
		if l_arr[element] != l_arr[element - 1]:
			count += 1
	print(count)


for t in range(int(input())):
	(n, m, l) = list(map(int, input().split()))
	m_list = [0 for i in range(n + 1)]
	for i in range(1, m + 1):
		val = list(map(int, input().split()))
		for j in val[1:]:
			m_list[j] = i
	strip = list(map(int, input().split()))
	p = -1
	store = 0
	for k in strip:
		if m_list[k] != p:
			p = m_list[k]
			store += 1
	print(store)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	lm = {}
	I = [0 for j in range(n)]
	for j in range(m):
		lm[j] = list(map(int, input().split()))
		temp = lm[j]
		lm[j] = temp[1:]
		for k in lm[j]:
			I[k - 1] = j + 2
	ll = list(map(int, input().split()))
	prev = None
	seg = 0
	for j in range(len(ll)):
		curr = I[ll[j] - 1]
		if prev is None:
			prev = curr
		elif prev != curr:
			prev = curr
			seg += 1
	seg += 1
	print(seg)


t = int(input())
for i in range(t):
	ip = input()
	(sn, sl, sm) = ip.split(' ')
	(n, l, m) = (int(sn), int(sl), int(sm))
	arr = [0] * (n + 1)
	for j in range(1, l + 1):
		ip = input()
		(sli, li) = ([], [])
		sli = ip.split(' ')
		li = list(map(int, sli))
		lt = li[0]
		for k in range(1, lt + 1):
			pos = li[k]
			arr[pos] = j
	istrip = input()
	strip = []
	strip = istrip.split(' ')
	strip = list(map(int, strip))
	elem = strip[0]
	pos = arr[elem]
	c = 0
	for k in range(1, m):
		elem = strip[k]
		curr = arr[elem]
		if pos != curr:
			c += 1
			pos = arr[elem]
		else:
			pos = arr[elem]
	print(c + 1)


t = int(input())
for i in range(t):
	(n, m, l) = map(int, input().split())
	list_no = [0] * (n + 1)
	for i in range(1, m + 1):
		for j in list(input().split())[1:]:
			if int(j) > 0:
				list_no[int(j)] = i
	colors = list(map(int, list(input().split())))
	count = 0
	v = 0
	for k in colors:
		if list_no[k] != v:
			count = count + 1
		v = list_no[k]
	print(count)


try:
	n = int(input())
	for _ in range(n):
		li = list(map(int, input().split()))
		n = li[0]
		m = li[1]
		l = li[2]
		listimp = []
		for _ in range(m):
			a = list(map(int, input().split()))
			listimp.append(a[1:])
		b = list(map(int, input().split()))
		count = 1
		ind = {}
		for (i, g) in enumerate(listimp):
			for e in g:
				ind.setdefault(e, set()).add(i)

		def same_group(e1, e2):
			return ind.get(e1, set()) & ind.get(e2, set())
		for i in range(len(b) - 1):
			if same_group(b[i], b[i + 1]) == set():
				count += 1
		print(count)
except EOFError:
	pass


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	cnt = 1
	d = {}
	for i in range(m):
		a = [int(x) for x in input().split()]
		for j in range(1, len(a)):
			d[a[j]] = i + 1
	s = [int(x) for x in input().split()]
	for i in range(1, l):
		if d[s[i - 1]] != d[s[i]]:
			cnt += 1
	print(cnt)


def calSegments(n, m, l, arr, strip):
	count = {}
	for i in range(m):
		for j in range(1, arr[i][0] + 1):
			count[arr[i][j]] = i + 1
	segment = 1
	for i in range(l - 1):
		if count[strip[i]] != count[strip[i + 1]]:
			segment += 1
	return segment
for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	arr = []
	for i in range(m):
		tempList = list(map(int, input().split()))
		arr.append(tempList)
	strip = list(map(int, input().split()))
	print(calSegments(n, m, l, arr, strip))


for _ in range(int(input())):
	(n, m, l) = list(map(int, input().split()))
	ans = [0] * (n + 1)
	count = 1
	for i in range(m):
		lst = list(map(int, input().split()))
		k = lst[0]
		for j in lst[1:]:
			ans[j] = count
		count += 1
	arr = list(map(int, input().split()))
	c = 1
	for k in range(1, len(arr)):
		if ans[arr[k]] != ans[arr[k - 1]]:
			c += 1
	print(c)


d = {}
t = int(input())
for i in range(t):
	(n, m, l) = map(int, input().split())
	for k in range(m):
		x = list(map(int, input().split()))
		for j in range(1, x[0] + 1):
			d[x[j]] = k + 1
	cl = list(map(int, input().split()))
	c = 1
	for i in range(l - 1):
		if d[cl[i]] != d[cl[i + 1]]:
			c += 1
		else:
			continue
	print(c)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	M = {}
	for i in range(m):
		t = list(map(int, input().split()))[1:]
		for j in range(len(t)):
			M[t[j]] = i + 1
	L = list(map(int, input().split()))
	prev = M[L[0]]
	(curr, c) = (0, 1)
	for i in range(1, l):
		curr = M[L[i]]
		if prev != curr:
			c += 1
		prev = curr
	print(c)


t = int(input())
for i in range(t):
	(n, m, o) = map(int, input().split())
	arr = list()
	arr.append(0)
	for j in range(n):
		arr.append(0)
	for j in range(m):
		temp = list(map(int, input().split()))
		size = temp.pop(0)
		for x in temp:
			arr[x] = j + 1
	l = list(map(int, input().split()))
	count = 0
	cur = 0
	for x in l:
		if arr[x] == cur:
			pass
		else:
			cur = arr[x]
			count += 1
	print(count)


import sys, collections, math, itertools, random, bisect, string
INF = sys.maxsize

def intp():
	return map(int, sys.stdin.readline().strip().split())

def ls():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
mod = 1000000007
for _ in range(int(input())):
	s = 0
	(n, m, l) = intp()
	a = {}
	for i in range(m):
		b = list(map(int, input().split()))
		for k in b[1:]:
			a[k] = i
	strip = list(map(int, input().split()))
	var_col = a[strip[0]]
	for i in range(1, len(strip)):
		if a[strip[i]] != var_col:
			s = s + 1
			var_col = a[strip[i]]
	print(s + 1)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	dic = {}
	for i in range(m):
		lis = list(map(int, input().split()))
		lis = lis[1:]
		for j in lis:
			dic[j] = i
	lis = list(map(int, input().split()))
	ans = 1
	for i in range(1, len(lis)):
		if dic[lis[i]] != dic[lis[i - 1]]:
			ans += 1
	print(ans)


t = int(input())
for i in range(t):
	(n, m, l) = map(int, input().split())
	cc = [0] * (n + 1)
	for i in range(m):
		for x in input().split()[1:]:
			cc[int(x)] = i + 1
	counti = 1
	li = [int(x) for x in input().split()]
	for i in range(1, l):
		if cc[li[i - 1]] != cc[li[i]]:
			counti += 1
	print(counti)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	dic = {}
	for i in range(m):
		lst = list(map(int, input().split()))[1:]
		for j in range(len(lst)):
			dic[lst[j]] = i + 1
	strip = list(map(int, input().split()))
	prev = dic[strip[0]]
	c = 1
	for i in range(1, l):
		curr = dic[strip[i]]
		if prev != curr:
			c += 1
		prev = curr
	print(c)


t = int(input())
while t:
	(n, m, l) = map(int, input().split())
	colorcodes = [0] * (n + 1)
	for i in range(m):
		for x in input().split()[1:]:
			colorcodes[int(x)] = i + 1
	count = 1
	l1 = [int(x) for x in input().split()]
	for i in range(1, l):
		if colorcodes[l1[i - 1]] != colorcodes[l1[i]]:
			count += 1
	print(count)
	t -= 1


map_inputs = lambda Type=int: map(Type, input().strip().split())
array_input = lambda n, Type=int: list(map_inputs(Type))[:n]
matrix_input = lambda row, col, Type=int: [array_input(col, Type) for _ in range(row)]
str_matrix = lambda row, col: [input().strip()[:col] for _ in range(row)]
str_listed_matrix = lambda row, col: [list(input().strip())[:col] for _ in range(row)]
test_cases = lambda t=1: [i for i in range(t)]

def main():
	(N, M, L) = map_inputs()
	arr = [0 for _ in range(N + 1)]
	for i in range(M):
		temp = list(map_inputs())
		for j in temp[1:]:
			arr[j] = i
	strips = array_input(L)
	ans = 0
	i = 0
	while i < L:
		try:
			let = arr[strips[i]]
			ans += 1
			while arr[strips[i]] == let:
				i += 1
				if i >= L:
					break
		except:
			break
	print(ans)
for test in test_cases(int(input())):
	main()


t = int(input())
for i in range(t):
	se = 1
	(n, m, l) = map(int, input().split())
	a = [0] * (n + 1)
	for j in range(m):
		c = list(map(int, input().split()))
		c.remove(c[0])
		for k in c:
			a[k] = j
	s = list(map(int, input().split()))
	for h in range(1, len(s)):
		if a[s[h]] != a[s[h - 1]]:
			se += 1
	print(se)


t = int(input())
for k in range(t):
	arr = list(map(int, input().split()))
	n = arr[0]
	m = arr[1]
	l = arr[2]
	a = []
	for i in range(m):
		arr_ = list(map(int, input().split()))
		a.append(arr_[1:])
	x = list(map(int, input().split()))
	count = 1
	y = {}
	for (i, g) in enumerate(a):
		for e in g:
			y.setdefault(e, set()).add(i)

	def samegrp(e1, e2):
		return y.get(e1, set()) & y.get(e2, set())
	for i in range(len(x) - 1):
		if samegrp(x[i], x[i + 1]) == set():
			count += 1
	print(count)


def solve():
	dup = [0] * (N + 1)
	for i in range(M):
		for a in A[i][1:]:
			dup[a] = i
	segments = 1
	for i in range(1, L):
		if dup[S[i]] != dup[S[i - 1]]:
			segments += 1
	return segments
for _ in range(int(input())):
	(N, M, L) = [int(x) for x in input().split()]
	A = []
	for i in range(M):
		A.append([int(x) for x in input().split()])
	S = [int(x) for x in input().split()]
	print(solve())


T = int(input())
while T > 0:
	(n, m, l) = input().split()
	n = int(n)
	m = int(m)
	l = int(l)
	ans = 1
	final = [x for x in range(n + 1)]
	for i in range(m):
		arr = input().split()
		x = 0
		for j in arr:
			if x == 0:
				x = 1
				continue
			j = int(j)
			final[j] = i
	strip = input().split()
	for i in range(len(strip) - 1):
		if final[int(strip[i])] != final[int(strip[i + 1])]:
			ans = ans + 1
	print(ans)
	T -= 1


try:
	t = int(input())
	for _ in range(t):
		arr = list(map(int, input().split()))
		n = arr[0]
		m = arr[1]
		l = arr[2]
		a = []
		for i in range(m):
			b = list(map(int, input().split()))
			a.append(b[1:])
		strip = list(map(int, input().split()))
		length = 1
		ind = {}
		for (i, g) in enumerate(a):
			for e in g:
				ind.setdefault(e, set()).add(i)

		def same_group(e1, e2):
			return ind.get(e1, set()) & ind.get(e2, set())
		for i in range(len(strip) - 1):
			if same_group(strip[i], strip[i + 1]) == set():
				length += 1
		print(length)
except:
	pass


from collections import defaultdict
for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	z = [-1] * (n + 1)
	for i in range(m):
		(k, *a) = map(int, input().split())
		for j in a:
			z[j] = i + 1
	strip = [int(i) for i in input().split()]
	segcnt = 0
	prev = -1
	for i in strip:
		k = z[i]
		if prev == -1 or prev != k:
			segcnt += 1
			prev = k
	print(segcnt)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	color_index = [0] * (n + 1)
	for i in range(1, m + 1):
		color = list(map(int, input().split()))[1:]
		for j in color:
			color_index[j] = i
	s = list(map(int, input().split()))
	sections = 1
	for i in range(1, l):
		if color_index[s[i]] != color_index[s[i - 1]]:
			sections += 1
	print(sections)


for i in range(int(input())):
	c = 0
	prev = 0
	(n, m, l) = map(int, input().split())
	ar = [0] * (n + 1)
	for i in range(1, m + 1):
		a = list(map(int, input().split()))
		for j in a:
			ar[j] = i
	lis = list(map(int, input().split()))
	for k in lis:
		if ar[k] != prev:
			c += 1
			prev = ar[k]
	print(c)


t = int(input().strip())
while t:
	(N, M, L) = map(int, input().strip().split())
	id = [-1] * (N + 1)
	for i in range(M):
		line = list(map(int, input().strip().split()))[1:]
		for j in range(len(line)):
			id[line[j]] = i + 1
	l_list = list(map(int, input().strip().split()))
	count = 0
	for i in range(1, len(l_list)):
		if id[l_list[i]] != id[l_list[i - 1]]:
			count += 1
	count += 1
	print(count)
	t -= 1


try:
	t = int(input())
	i = 1
	while i <= t:
		arr = list(map(int, input().split(' ')))
		N = int(arr[0])
		M = int(arr[1])
		L = int(arr[2])
		m = 1
		list_2d = []
		dict = {}
		while m <= M:
			temp_list = list(input().split(' '))
			for a in range(1, len(temp_list)):
				temp = int(temp_list[a])
				dict[temp] = m
			m += 1
		count = 1
		Strip = list(map(int, input().split(' ')))
		prev = dict[Strip[0]]
		for strip in range(1, L):
			temp = Strip[strip]
			if dict[temp] != prev:
				count += 1
				prev = dict[temp]
			else:
				prev = dict[temp]
		print(count)
		i += 1
except IOError:
	print('IO Exception')


from collections import defaultdict
for t in range(int(input())):
	(N, M, L) = map(int, input().split())
	D = defaultdict(int)
	c = 1
	for i in range(M):
		lst = list(map(int, input().split()))
		for col in lst[1:]:
			D[col] = i
	stp = list(map(int, input().split()))
	if len(stp) == 1:
		print(c)
	else:
		for i in range(1, len(stp)):
			if D[stp[i]] != D[stp[i - 1]]:
				c += 1
		print(c)


for i in range(int(input())):
	(n, m, l) = map(int, input().split())
	dic = {}
	j = 0
	for i in range(m):
		j += 1
		lst = list(map(int, input().split()))
		for i in range(1, len(lst)):
			dic[lst[i]] = j
	ls = list(map(int, input().split()))
	chk = 0
	count = 0
	for i in ls:
		chk1 = dic[i]
		if chk != chk1:
			count += 1
			chk = chk1
		else:
			continue
	print(count)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	d = dict()
	for idx in range(m):
		lst = list(map(int, input().split()))[1:]
		for i in lst:
			d[i] = idx
	s = list(map(int, input().split()))
	arr = []
	for i in s:
		if len(arr) == 0:
			arr.append([d[i], 1])
		elif arr[-1][0] == d[i]:
			arr[-1][1] += 1
		else:
			arr.append([d[i], 1])
	print(len(arr))


T = int(input())
for test in range(T):
	(n, m, l) = map(int, input().split())
	checker = {}
	idForList = 0
	for listInput in range(m):
		inp = list(map(int, input().split()))
		inp = inp[1:]
		for i in inp:
			checker[i] = idForList
		idForList += 1
	stripID = list(map(int, input().split()))
	cnt = 1
	for i in range(1, l):
		if checker[stripID[i]] != checker[stripID[i - 1]]:
			cnt += 1
	print(cnt)


t = int(input())
for i in range(t):
	(n, m, l) = map(int, input().split())
	idx_list = {}
	for i in range(m):
		temp = list(map(int, input().split()))
		for j in range(1, len(temp)):
			idx_list[temp[j]] = i + 1
	strip = list(map(int, input().split()))
	segment = l
	for i in range(l - 1):
		if idx_list[strip[i]] == idx_list[strip[i + 1]]:
			segment -= 1
	print(segment)


try:
	for _ in range(int(input())):
		A = [int(x) for x in input().split()]
		M = [0 for i in range(A[0] + 1)]
		for j in range(A[1]):
			m = [int(x) for x in input().split()]
			for i in m[1:]:
				M[i] = j - 1
		count = 1
		L = [int(x) for x in input().split()]
		for j in range(1, len(L)):
			if M[L[j]] != M[L[j - 1]]:
				count += 1
		print(count)
except:
	pass


T = int(input())
for i in range(T):
	lengths = list(map(int, input().split()))
	(N, M, L) = (lengths[0], lengths[1], lengths[2])
	colour_groups = {}
	for j in range(M):
		colours = list(map(int, input().split()))
		for k in colours[1:]:
			colour_groups[k] = j
	strip = list(map(int, input().split()))
	prev = None
	segment = 1
	for h in strip:
		if prev == None:
			prev = colour_groups[h]
			continue
		elif prev != colour_groups[h]:
			segment += 1
			prev = colour_groups[h]
	print(segment)


for i in range(int(input())):
	a = list(map(int, input().split()))
	z = {}
	for i in range(a[1]):
		arr = list(map(int, input().split()))
		for x in arr[1:]:
			z.update({x: i})
	l = list(map(int, input().split()))
	L = a[2]
	X = -1
	for x in range(a[2]):
		y = z.get(l[x])
		if X == y:
			L = L - 1
		X = y
	print(L)


t = int(input())
for _ in range(t):
	(N, M, L) = map(int, input().split())
	arr = [0] * N
	for __ in range(1, M + 1):
		K = list(map(int, input().split()))
		for i in range(1, K[0] + 1):
			inp = K[i]
			arr[inp - 1] = __
	prev = 0
	ans = 0
	s = list(map(int, input().split()))
	for ll in range(L):
		pp = s[ll] - 1
		if arr[pp] != prev:
			ans = ans + 1
		prev = arr[pp]
	print(ans)


test = int(input())
for t in range(test):
	major = []
	(n, m, l) = map(int, input().split())
	for i in range(m):
		major.append(list(map(int, input().split())))
	strip = list(map(int, input().split()))
	check = [-1 for i in range(n + 1)]
	ind = 0
	for i in major:
		for j in range(1, i[0] + 1):
			check[i[j]] = ind
		ind += 1
	count = 0
	i = 0
	prevstrip = -2
	while i < l:
		if check[strip[i]] == prevstrip:
			i += 1
			continue
		prevstrip = check[strip[i]]
		count += 1
		i += 1
	print(count)


for i in range(int(input())):
	(n, m, l) = map(int, input().split())
	g = {}
	for j in range(m):
		l = list(map(int, input().split()))
		for sd in range(1, len(l)):
			g[l[sd]] = j + 1
	z = list(map(int, input().split()))
	p = 1
	k = g[z[0]]
	for i in range(1, len(z)):
		if g[z[i]] != k:
			p += 1
			k = g[z[i]]
	print(p)


test = int(input())
for t in range(test):
	major = []
	(n, m, l) = map(int, input().split())
	for i in range(m):
		major.append(list(map(int, input().split())))
	strip = list(map(int, input().split()))
	check = [-1 for i in range(n + 1)]
	ind = 0
	for i in major:
		for j in range(1, i[0] + 1):
			check[i[j]] = ind
		ind += 1
	count = 0
	i = 0
	while i < l:
		while i < l - 1:
			if check[strip[i]] == check[strip[i + 1]]:
				i += 1
			else:
				break
		i += 1
		count += 1
	print(count)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	dict = {}
	for x in range(m):
		l1 = list(map(int, input().split()))[1:]
		for y in range(len(l1)):
			dict[l1[y]] = x
	l = list(map(int, input().split()))
	c = 0
	for x in range(len(l) - 1):
		if dict[l[x]] != dict[l[x + 1]]:
			c += 1
	print(c + 1)


for _ in range(int(input())):
	(n, m, l) = map(int, input().split())
	m_list = [0] * n
	for i in range(m):
		a = list(map(int, input().split()))[1:]
		for j in a:
			m_list[j - 1] = i + 1
	l_list = list(map(int, input().split()))
	count = 0
	for i in range(1, l):
		if m_list[l_list[i - 1] - 1] != m_list[l_list[i] - 1]:
			count += 1
	print(count + 1)