#include <bits/stdc++.h>
using namespace std;
struct Node {
char data;
int l, r;
}lt[105], rt[105];
int _list[105] = {
0};
int build(int n, Node tree[]) {
memset(_list, 0, sizeof _list);
for (int i = 0; i < n; ++i) {
char a, b, c;
cin >> a >> b >> c;
tree[i].data = a;
if (b != '-') {
tree[i].l = b - '0';
_list[tree[i].l] = 1;
}
else {
tree[i].l = -1;
}
if (c != '-') {
tree[i].r = c - '0';
_list[tree[i].r] = 1;
}
else {
tree[i].r = -1;
}
}
int root = -1;
for (int i = 0; i < n; ++i) {
if (tree[i].data != 0 and _list[i] != 1) {
root = i;
break;
}
}
return root;
}
bool judge(int lroot, int rroot) {
if (lroot == -1 and rroot == -1) {
return true;
}
if ((lroot == -1 and rroot != -1) or (lroot != -1 and rroot == -1)) {
return false;
}
if (lt[lroot].data != rt[rroot].data) {
return false;
}
if (lt[lroot].l == -1 and rt[rroot].l == -1) {
return judge(lt[lroot].r, rt[rroot].r);
}
if (lt[lroot].l != -1 and rt[rroot].l != -1 and lt[lt[lroot].l].data == rt[rt[rroot].l].data) {
return judge(lt[lroot].l, rt[rroot].l) and judge(lt[lroot].r, rt[rroot].r);
}
else {
return judge(lt[lroot].l, rt[rroot].r) and judge(lt[lroot].r, rt[rroot].l);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("D:/VS CODE/C++/in.txt", "r", stdin);
freopen("D:/VS CODE/C++/out.txt", "w", stdout);
#endif
int n1, n2;
cin >> n1;
int root1 = build(n1, lt);
cin >> n2;
int root2 = build(n2, rt);
bool ans;
ans = judge(root1, root2);
if (ans)
cout << "Yes";
else
cout << "No";
fclose(stdin);
fclose(stdout);
return 0;
}