#include <bits/stdc++.h>
using namespace std;
namespace ACM {
int ch[55][2], nd = 1;
int fa[55], gg[55], q[55];
void init() {
  int l, x = 1, c;
  scanf("%d", &l);
  for (int i = (int)(1); i <= (int)(l); i++) {
    scanf("%d", &c);
    if (!ch[x][c]) ch[x][c] = ++nd;
    x = ch[x][c];
  }
  gg[x] = 1;
}
void build() {
  int h = 0, t = 0;
  for (int i = (int)(0); i <= (int)(1); i++)
    if (ch[1][i]) {
      fa[ch[1][i]] = 1;
      q[++t] = ch[1][i];
    } else
      ch[1][i] = 1;
  while (h != t) {
    int x = q[++h];
    gg[x] |= gg[fa[x]];
    for (int i = (int)(0); i <= (int)(1); i++)
      if (ch[x][i]) {
        fa[ch[x][i]] = ch[fa[x]][i];
        q[++t] = ch[x][i];
      } else
        ch[x][i] = ch[fa[x]][i];
  }
}
}  // namespace ACM
int G, n, m;
int to[105];
int f1[105], f2[105];
vector<int> op[105];
unsigned long long d1[55][55];
unsigned long long d2[55][55];
unsigned long long dis[105][55][55];
int main() {
  scanf("%d%d%d", &G, &n, &m);
  for (int i = (int)(1); i <= (int)(n); i++) {
    int l, x;
    scanf("%d%d", &to[i], &l);
    for (int j = (int)(1); j <= (int)(l); j++)
      scanf("%d", &x), op[i].push_back(x);
  }
  for (int i = (int)(1); i <= (int)(m); i++) ACM::init();
  ACM::build();
  m = ACM::nd;
  for (int i = (int)(0); i <= (int)(G - 1); i++)
    for (int j = (int)(1); j <= (int)(m); j++)
      for (int k = (int)(1); k <= (int)(m); k++) dis[i][j][k] = 1ull << 63;
  for (int i = (int)(0); i <= (int)(1); i++)
    for (int j = (int)(1); j <= (int)(m); j++) {
      int to = ACM::ch[j][i];
      if (!ACM::gg[to]) dis[i][j][to] = 1;
    }
  f1[0] = f1[1] = 1;
  for (;;) {
    bool fllag = 0;
    memset(f2, 0, sizeof(f2));
    for (int i = (int)(1); i <= (int)(n); i++) {
      bool flag = 0;
      for (auto p : op[i]) flag |= f1[p] && (!f2[p]);
      if (!flag) continue;
      for (int j = (int)(1); j <= (int)(m); j++)
        for (int k = (int)(1); k <= (int)(m); k++)
          d1[j][k] = (j == k ? 0 : (1ull << 63));
      for (auto p : op[i]) {
        for (int j = (int)(1); j <= (int)(m); j++)
          for (int k = (int)(1); k <= (int)(m); k++) d2[j][k] = 1ull << 63;
        for (int j = (int)(1); j <= (int)(m); j++)
          for (int k = (int)(1); k <= (int)(m); k++)
            for (int l = (int)(1); l <= (int)(m); l++)
              if (d1[j][k] < -dis[p][k][l])
                d2[j][l] = min(d2[j][l], d1[j][k] + dis[p][k][l]);
        for (int j = (int)(1); j <= (int)(m); j++)
          for (int k = (int)(1); k <= (int)(m); k++) d1[j][k] = d2[j][k];
      }
      for (int j = (int)(1); j <= (int)(m); j++)
        for (int k = (int)(1); k <= (int)(m); k++)
          if (d1[j][k] < dis[to[i]][j][k]) {
            dis[to[i]][j][k] = d1[j][k], fllag = 1, f2[to[i]] = 1;
          }
    }
    if (!fllag) break;
    for (int i = (int)(0); i <= (int)(G - 1); i++) swap(f1[i], f2[i]);
  }
  for (int i = (int)(2); i <= (int)(G - 1); i++) {
    unsigned long long ans = 1ull << 63;
    for (int j = (int)(1); j <= (int)(m); j++) ans = min(ans, dis[i][1][j]);
    if (ans == (1ull << 63))
      puts("YES");
    else
      printf("NO %llu\n", ans);
  }
}
