#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T>
void IN(T& x) {
  cin >> x;
}
template <class H, class... T>
void IN(H& h, T&... t) {
  IN(h);
  IN(t...);
}
template <class H>
void INA(H bg, H ed) {
  while (bg != ed) IN(*bg++);
}
template <class T>
void OUT(const T& x) {
  cout << x;
}
template <class H, class... T>
void OUT(const H& h, const T&... t) {
  OUT(h);
  OUT(t...);
}
void program();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  program();
}
const int MX = 300;
unsigned long long INF = (1ll << 63ll) + (1ll << 62ll);
int og, g, n, m;
int a[MX], k[MX], b[MX][MX];
int l[MX], c[MX][MX], suml;
unsigned long long dp[MX][MX][MX];
priority_queue<tuple<unsigned long long, unsigned long long, unsigned long long,
                     unsigned long long>,
               vector<tuple<unsigned long long, unsigned long long,
                            unsigned long long, unsigned long long>>,
               greater<tuple<unsigned long long, unsigned long long,
                             unsigned long long, unsigned long long>>>
    pq;
vector<int> rev[MX];
int getNextState(string t) {
  int x = 0, ans = 0, longest = 0;
  for (int i = int(0); i < int(m); i++) {
    string s;
    for (int j = int(0); j < int(l[i]); j++) {
      if (s.size() > longest && s.size() <= t.size()) {
        bool pos = 1;
        for (int k = int(0); k < int(s.size()); k++)
          if (t[t.size() - k - 1] != s[s.size() - k - 1]) {
            pos = 0;
            break;
          }
        if (pos) longest = s.size(), ans = x;
      }
      s += '0' + c[i][j];
      x++;
    }
    if (s.size() <= t.size()) {
      bool pos = 1;
      for (int k = int(0); k < int(s.size()); k++)
        if (t[t.size() - k - 1] != s[s.size() - k - 1]) {
          pos = 0;
          break;
        }
      if (pos) return suml;
    }
  }
  return ans;
}
void fillDpBase() {
  int x = 0;
  for (int i = int(0); i < int(m); i++) {
    string s;
    for (int j = int(0); j < int(l[i]); j++) {
      dp[0][x][getNextState(s + "0")] = 1;
      dp[1][x][getNextState(s + "1")] = 1;
      s += '0' + c[i][j];
      x++;
    }
  }
  if (m == 0) {
    dp[0][0][0] = 1;
    dp[1][0][0] = 1;
  }
}
void program() {
  IN(g, n, m);
  og = g;
  for (int i = int(0); i < int(n); i++)
    IN(a[i]), IN(k[i]), INA(b[i], b[i] + k[i]);
  for (int i = int(0); i < int(m); i++) IN(l[i]), INA(c[i], c[i] + l[i]);
  for (int i = int(0); i < int(n); i++) {
    if (k[i] > 2) {
      a[n] = g;
      k[n] = k[i] - 1;
      for (int j = int(0); j < int(k[n]); j++) b[n][j] = b[i][j];
      b[i][0] = g;
      b[i][1] = b[i][k[i] - 1];
      k[i] = 2;
      g++;
      n++;
    }
  }
  for (int i = int(0); i < int(n); i++)
    for (int j = int(0); j < int(k[i]); j++) rev[b[i][j]].push_back(i);
  for (int i = int(0); i < int(m); i++) suml += l[i];
  if (m == 0) suml = 1;
  for (int i = int(0); i < int(g); i++)
    for (int j = int(0); j < int(suml); j++)
      for (int k = int(0); k < int(suml); k++) dp[i][j][k] = INF;
  fillDpBase();
  for (int i = int(0); i < int(2); i++)
    for (int j = int(0); j < int(suml); j++)
      for (int k = int(0); k < int(suml); k++) pq.push({dp[i][j][k], i, j, k});
  while (!pq.empty()) {
    tuple<unsigned long long, unsigned long long, unsigned long long,
          unsigned long long>
        p = pq.top();
    pq.pop();
    unsigned long long X, I, J, K;
    tie(X, I, J, K) = p;
    if (X == INF) break;
    if (dp[I][J][K] != X) continue;
    for (auto& i : rev[I]) {
      if (k[i] == 1) {
        if (dp[a[i]][J][K] > X) {
          dp[a[i]][J][K] = X;
          pq.push({X, a[i], J, K});
        }
      } else {
        if (b[i][0] == I) {
          for (int j = int(0); j < int(suml); j++) {
            if (dp[b[i][1]][K][j] == INF) continue;
            unsigned long long Y = X + dp[b[i][1]][K][j];
            if (dp[a[i]][J][j] > Y) {
              dp[a[i]][J][j] = Y;
              pq.push({Y, a[i], J, j});
            }
          }
        }
        if (b[i][1] == I) {
          for (int j = int(0); j < int(suml); j++) {
            if (dp[b[i][0]][j][J] == INF) continue;
            unsigned long long Y = dp[b[i][0]][j][J] + X;
            if (dp[a[i]][j][K] > Y) {
              dp[a[i]][j][K] = Y;
              pq.push({Y, a[i], j, K});
            }
          }
        }
      }
    }
  }
  for (int i = int(2); i < int(og); i++) {
    unsigned long long mn = INF;
    for (int j = int(0); j < int(suml); j++) mn = min(mn, dp[i][0][j]);
    if (mn == INF)
      OUT("YES\n");
    else
      OUT("NO ", mn, "\n");
  }
}
