#include <bits/stdc++.h>
using namespace std;
struct tree{
char data;
int left,right;
};
struct tree bt1[1000];
struct tree bt2[1000];
int flag[1000];
int FindRoot(struct tree bt[]){
int n;
cin >> n;
if(n == 0) return -1;\
memset(flag,0,sizeof(flag));
for(int j = 0;j < n;j++){
cin >> bt[j].data;
char a,b;
cin >> a >> b;
if(a == '-'){
bt[j].left = -1;
}
else{
bt[j].left = a - '0';
flag[a-'0'] = 1;
}
if(b == '-'){
bt[j].right = -1;
}
else{
bt[j].right = b - '0';
flag[b-'0'] = 1;
}
}
int root;
for(int i = 0;i < n;i++){
if(flag[i] != 1)
root = i;
}
return root;
}
int JudgeTG(int root1,int root2){
if(root1 == -1 && root2 == -1)
return 1;
if((root1 == -1 && root2 != -1) || (root1 != -1 && root2 == -1) || bt1[root1].data != bt2[root2].data)
return 0;
return (JudgeTG(bt1[root1].left,bt2[root2].left) && JudgeTG(bt1[root1].right,bt2[root2].right)) || (JudgeTG(bt1[root1].left,bt2[root2].right) &&JudgeTG(bt1[root1].right,bt2[root2].left));
}
int main(){
int root1 = FindRoot(bt1);
int root2 = FindRoot(bt2);
int result = JudgeTG(root1,root2);
if(result)
cout << "Yes" << endl;
else
cout << "No" << endl;
}