#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(long long a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10) write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(long long a) {
  write(a);
  puts("");
}
inline void wri(long long a) {
  write(a);
  putchar(' ');
}
long long rnd() {
  unsigned long long ans = 0;
  for (int i = (int)(0); i <= (int)(4); i++) ans = ans << 15 ^ rand();
  return ans % ((unsigned long long)1 << 63);
}
const int N = 105, M = 52;
const unsigned long long inf = (unsigned long long)1 << 63;
int a[N], go[M][2], vis[M], tot = 1;
vector<int> v[N];
unsigned long long dis[N][M][M], dd[M], ee[M];
int q[M], fail[M];
void buildfail() {
  int l = 0, r = 1;
  q[0] = 1;
  fail[1] = 0;
  go[0][1] = go[0][0] = 1;
  while (l < r) {
    int now = q[l++];
    vis[now] |= vis[fail[now]];
    for (int i = (int)(0); i <= (int)(1); i++)
      if (!go[now][i])
        go[now][i] = go[fail[now]][i];
      else
        fail[q[r++] = go[now][i]] = go[fail[now]][i];
  }
}
int main() {
  int g = read() - 1, n = read(), m = read();
  for (int i = (int)(1); i <= (int)(n); i++) {
    a[i] = read();
    int k = read();
    while (k--) v[i].push_back(read());
  }
  for (int i = (int)(1); i <= (int)(m); i++) {
    int len = read(), p = 1;
    for (int j = (int)(1); j <= (int)(len); j++) {
      int t = read();
      if (!go[p][t]) go[p][t] = ++tot;
      p = go[p][t];
    }
    vis[p] = 1;
  }
  buildfail();
  for (int i = (int)(0); i <= (int)(g); i++)
    for (int j = (int)(1); j <= (int)(tot); j++)
      for (int k = (int)(1); k <= (int)(tot); k++) dis[i][j][k] = inf;
  for (int i = (int)(0); i <= (int)(1); i++)
    for (int j = (int)(1); j <= (int)(tot); j++)
      if (!vis[go[j][i]]) dis[i][j][go[j][i]] = 1;
  int f = 1;
  while (f) {
    f = 0;
    for (int i = (int)(1); i <= (int)(n); i++) {
      for (int o = (int)(1); o <= (int)(tot); o++)
        if (!vis[o]) {
          for (int j = (int)(1); j <= (int)(tot); j++) dd[j] = inf;
          dd[o] = 0;
          for (auto k : v[i]) {
            for (int j = (int)(1); j <= (int)(tot); j++) ee[j] = inf;
            for (int j = (int)(1); j <= (int)(tot); j++)
              if (dd[j] < inf)
                for (int o = (int)(1); o <= (int)(tot); o++)
                  ee[o] = min(ee[o], dd[j] + dis[k][j][o]);
            memcpy(dd, ee, sizeof(ee));
          }
          for (int j = (int)(1); j <= (int)(tot); j++)
            if (ee[j] < dis[a[i]][o][j]) {
              assert(!vis[j]);
              dis[a[i]][o][j] = ee[j];
              f = 1;
            }
        }
    }
  }
  for (int o = (int)(2); o <= (int)(g); o++) {
    unsigned long long ans = inf;
    for (int i = (int)(1); i <= (int)(tot); i++) ans = min(ans, dis[o][1][i]);
    if (ans == inf)
      puts("YES");
    else
      cout << "NO " << ans << endl;
  }
}
