def inputL():
L=[]
n=int(input())
for i in range(n):
a=list(input().split())
L.append(a)
return n,L
n1,L1=inputL()
n2,L2=inputL()
def findline(L):
LC=[]
for i in range(len(L)):
l=[]
if (L[i][1]=='-')&(L[i][2]=='-'):
l=makeline(L,i)
LC.append(l)
return LC
def makeline(L,a):
l=[]
l.append(L[a][0])
L.append([None,None,None])
while True:
for i in range(len(L)):
if((L[i][1]==str(a))|(L[i][2]==str(a))):
l.append(L[i][0])
a=i
break
if L[i]==[None,None,None]:
break
return l
def compare(L1,L2):
a=[]
if len(L1)!=len(L2):
print('No')
else:
for i in L1:
for j in L2:
if i==j:
a.append(i)
if(len(a)==len(L1)):
print("Yes")
else:
print("No")
if n1!=n2:
print('No')
else:
L1N=findline(L1)
L2N=findline(L2)
compare(L1N,L2N)