Today you are to solve the problem even the famous Hercule Poirot can't cope with! That's why this crime has not yet been solved and this story was never included in Agatha Christie's detective story books. 

You are not informed on what crime was committed, when and where the corpse was found and other details. We only know that the crime was committed in a house that has n rooms and m doors between the pairs of rooms. The house residents are very suspicious, that's why all the doors can be locked with keys and all the keys are different. According to the provided evidence on Thursday night all the doors in the house were locked, and it is known in what rooms were the residents, and what kind of keys had any one of them. The same is known for the Friday night, when all the doors were also locked. On Friday it was raining heavily, that's why nobody left the house and nobody entered it. During the day the house residents could

  * open and close doors to the neighboring rooms using the keys at their disposal (every door can be opened and closed from each side); 
  * move freely from a room to a room if a corresponding door is open; 
  * give keys to one another, being in one room. 



"Little grey matter" of Hercule Poirot are not capable of coping with such amount of information. Find out if the positions of people and keys on the Thursday night could result in the positions on Friday night, otherwise somebody among the witnesses is surely lying.

Input

The first line contains three preset integers n, m и k (1 ≤ n, m, k ≤ 1000) — the number of rooms, the number of doors and the number of house residents respectively. The next m lines contain pairs of room numbers which join the doors. The rooms are numbered with integers from 1 to n. There cannot be more that one door between the pair of rooms. No door connects a room with itself. The next k lines describe the residents' position on the first night. Every line contains a resident's name (a non-empty line consisting of no more than 10 Latin letters), then after a space follows the room number, then, after a space — the number of keys the resident has. Then follow written space-separated numbers of the doors that can be unlocked by these keys. The doors are numbered with integers from 1 to m in the order in which they are described in the input data. All the residents have different names, uppercase and lowercase letters considered to be different. Every m keys occurs exactly once in the description. Multiple people may be present in one room, some rooms may be empty. The next k lines describe the position of the residents on the second night in the very same format. It is guaranteed that in the second night's description the residents' names remain the same and every m keys occurs exactly once.

Output

Print "YES" (without quotes) if the second arrangement can result from the first one, otherwise, print "NO".

Examples

Input

2 1 2
1 2
Dmitry 1 1 1
Natalia 2 0
Natalia 1 1 1
Dmitry 2 0


Output

YES


Input

4 4 3
1 3
1 2
2 3
3 4
Artem 1 1 4
Dmitry 1 1 2
Edvard 4 2 1 3
Artem 2 0
Dmitry 1 0
Edvard 4 4 1 2 3 4


Output

NO

Source: codeforces | Difficulty: HARD
Tags: Spanning trees

Graph algorithms


Solutions:
def solve(n, m, k, edge_list, house_members):
	room_sets = [i for i in range(n)]
	valid_move = [True for i in range(n)]
	room_status = [[1 << i, 0, []] for i in range(n)]
	for i in range(k):
		room_status[house_members[i][1]][1] |= 1 << i
		for j in house_members[i][2]:
			room_status[house_members[i][1]][2].append(j)
	move = True
	while move:
		move = False
		for i in range(n):
			if valid_move[i]:
				move = move or expand(i, room_sets, valid_move, room_status, edge_list)
	house_part = []
	for i in range(n):
		if room_sets[i] == i:
			house_part.append(i)
	return adjust_answer(house_part, room_status, n, m, k)

def setoff(index, room_sets):
	if room_sets[index] == index:
		return index
	else:
		new_val = setoff(room_sets[index], room_sets)
		room_sets[index] = new_val
		return new_val

def expand(current_room, room_sets, valid_move, room_status, edge_list):
	moving = room_status[current_room][2]
	move = False
	for i in moving:
		edge = edge_list[i]
		if setoff(edge[0], room_sets) == current_room:
			move = move or merge(current_room, edge[1], room_sets, valid_move, room_status)
		if setoff(edge[1], room_sets) == current_room:
			move = move or merge(current_room, edge[0], room_sets, valid_move, room_status)
	return move

def merge(x, y, room_sets, valid_move, room_status):
	setx = setoff(x, room_sets)
	sety = setoff(y, room_sets)
	if setx == sety:
		return False
	else:
		m = len(room_status[setx][2])
		m1 = len(room_status[sety][2])
		if m >= m1:
			head = setx
			tail = sety
		else:
			head = sety
			tail = setx
		valid_move[tail] = 0
		room_sets[tail] = head
		keys = room_status[tail][2]
		while len(keys) > 0:
			room_status[head][2].append(keys.pop())
		room_status[head][1] |= room_status[tail][1]
		room_status[head][0] |= room_status[tail][0]
		return True

def adjust_answer(house_part, room_status, n, m, k):
	size = len(house_part)
	rooms = [0 for i in range(size)]
	members = [0 for i in range(size)]
	keys = [0 for i in range(size)]
	for i in range(size):
		keys_list = room_status[house_part[i]][2]
		for j in keys_list:
			keys[i] |= 1 << j
		rooms[i] = room_status[house_part[i]][0]
		members[i] = room_status[house_part[i]][1]
	return [rooms, members, keys]

def compare_answer(answer_1, answer_2, n, m, k):
	rooms1 = answer_1[0]
	members1 = answer_1[1]
	keys1 = answer_1[2]
	if len(rooms1) != len(members1) != len(keys1):
		return False
	rooms2 = answer_2[0]
	members2 = answer_2[1]
	keys2 = answer_2[2]
	if len(rooms2) != len(members2) != len(keys2):
		return False
	if len(rooms1) != len(rooms2):
		return False
	for i in range(len(rooms1)):
		if rooms1[i] != rooms2[i] or keys1[i] != keys2[i] or members1[i] != members2[i]:
			return False
	return True
data = [int(i) for i in input().split()]
(n, m, k) = (data[0], data[1], data[2])
house_members = [['', 0, []] for i in range(k)]
house_members_2 = [['', 0, []] for i in range(k)]
pos_adjustment = {}
edge_list = []
for i in range(m):
	doors_data = [int(i) for i in input().split()]
	(a, b) = (doors_data[0] - 1, doors_data[1] - 1)
	edge_list.append((a, b))
for i in range(k):
	temp = input().split()
	pos_adjustment[temp[0]] = i
	house_members[i][0] = temp[0]
	house_members[i][1] = int(temp[1]) - 1
	for j in range(3, len(temp)):
		val = int(temp[j]) - 1
		house_members[i][2].append(val)
answer_1 = solve(n, m, k, edge_list, house_members)
for i in range(k):
	temp = input().split()
	house_members_2[pos_adjustment[temp[0]]][0] = temp[0]
	house_members_2[pos_adjustment[temp[0]]][1] = int(temp[1]) - 1
	for j in range(3, len(temp)):
		val = int(temp[j]) - 1
		house_members_2[pos_adjustment[temp[0]]][2].append(val)
answer_2 = solve(n, m, k, edge_list, house_members_2)
if compare_answer(answer_1, answer_2, n, m, k):
	print('YES')
else:
	print('NO')