#include <bits/stdc++.h>
using namespace std;
map<string, int> name;
string s;
bool a[1005], b[1005];
int x[1005], y[1005], key[1005], f[1005], n, m, K, p, z, L;
int get(int x) { return f[x] == x ? x : f[x] = get(f[x]); }
void work(bool *a) {
  for (int i = 1; i <= n; i++) f[i] = i;
  int flag = 1;
  while (flag) {
    flag = 0;
    for (int i = 1; i <= m; i++)
      if (!a[i])
        if (get(x[i]) == get(key[i]) || get(y[i]) == get(key[i]))
          f[get(x[i])] = get(y[i]), a[i] = flag = 1;
  }
}
bool solve() {
  cin >> n >> m >> K;
  for (int i = 1; i <= m; i++) cin >> x[i] >> y[i];
  for (int i = 1; i <= K; i++) {
    cin >> s >> p >> L;
    name[s] = p;
    for (int j = 1; j <= L; j++) cin >> z, key[z] = p;
  }
  work(a);
  for (int i = 1; i <= K; i++) {
    cin >> s >> p >> L;
    int p1 = name[s];
    if (get(p) - get(p1)) return 0;
    for (int j = 1; j <= L; j++) {
      cin >> z;
      if (get(p) != get(key[z])) return 0;
      key[z] = p;
    }
  }
  work(b);
  for (int i = 1; i <= m; i++)
    if (a[i] - b[i]) return 0;
  return 1;
}
int main() {
  puts(solve() ? "YES" : "NO");
  return 0;
}
