#include <bits/stdc++.h>
using namespace std;
int i, j, k, l, m, n, mk;
int A[1100], B[1100], done[1100];
set<int>::iterator p, q;
struct ST {
  char js[20];
  int f[1100], mark[1100];
  set<int> st[1100], ts[1100];
  set<int>::iterator p;
  int find(int a) { return (f[a] == a ? a : (f[a] = find(f[a]))); }
  bool judge() {
    bool ans = false;
    set<int>::iterator p, q;
    for (int i = 1; i <= n; i++) {
      if (find(i) != i) continue;
      for (p = st[i].begin(); p != st[i].end(); p++) {
        if (find(A[*p]) != find(i) && find(B[*p]) != find(i)) continue;
        if (find(A[*p]) == find(B[*p])) continue;
        ans = true;
        if (find(A[*p]) != i) swap(A[*p], B[*p]);
        f[f[A[*p]]] = f[B[*p]];
        for (q = st[i].begin(); q != st[i].end(); q++) st[f[B[*p]]].insert(*q);
        for (q = ts[i].begin(); q != ts[i].end(); q++) ts[f[B[*p]]].insert(*q);
        return true;
      }
    }
    return ans;
  }
  int hash(char s[]) {
    int ans = 0;
    for (i = 0; s[i]; i++) ans = (ans * 27 + s[i] - 'a') % 23333333;
    return ans;
  }
  void solve() {
    int a, b, T;
    for (int i = 1; i <= n; i++) f[i] = i;
    for (int i = 1; i <= mk; i++) {
      scanf("%s", js);
      scanf("%d", &a);
      ts[a].insert(hash(js));
      for (scanf("%d", &T); T; T--) {
        scanf("%d", &b);
        st[a].insert(b);
      }
    }
    while (judge())
      ;
    int top = 0;
    for (int i = 1; i <= n; i++) find(i);
    for (int i = 1; i <= n; i++) {
      if (!mark[f[i]]) mark[f[i]] = ++top;
    }
  }
} A_, B_;
int main() {
  scanf("%d %d %d", &n, &m, &mk);
  for (i = 1; i <= m; i++) scanf("%d %d", &A[i], &B[i]);
  A_.solve();
  B_.solve();
  for (i = 1; i <= n; i++)
    if (A_.mark[A_.f[i]] != B_.mark[B_.f[i]]) {
      printf("NO");
      return 0;
    }
  for (i = 1; i <= n; i++) {
    if (done[A_.mark[A_.f[i]]]) continue;
    done[A_.mark[A_.f[i]]] = 1;
    if (A_.st[A_.f[i]].size() != B_.st[B_.f[i]].size()) {
      printf("NO");
      return 0;
    }
    for (p = A_.st[A_.f[i]].begin(), q = B_.st[B_.f[i]].begin();
         p != A_.st[A_.f[i]].end(); p++, q++)
      if (*p != *q) {
        printf("NO");
        return 0;
      }
    if (A_.ts[A_.f[i]].size() != B_.ts[B_.f[i]].size()) {
      printf("NO");
      return 0;
    }
    for (p = A_.ts[A_.f[i]].begin(), q = B_.ts[B_.f[i]].begin();
         p != A_.ts[A_.f[i]].end(); p++, q++)
      if (*p != *q) {
        printf("NO");
        return 0;
      }
  }
  printf("YES");
  return 0;
}
