Problem: The Committee for Research on Binary Viruses discovered a method of replication for a large family of viruses whose genetic codes are sequences of zeros and ones. Each virus originates from a single gene; for simplicity genes are denoted by integers from 0 to G - 1. At each moment in time a virus is a sequence of genes. When mutation occurs, one of the genes from the sequence is replaced by a certain sequence of genes, according to the mutation table. The virus stops mutating when it consists only of genes 0 and 1.

For instance, for the following mutation table: $$$ 2 → ⟨ 0\ 1 ⟩ \\\ 3 → ⟨ 2\ 0\ 0⟩\\\ 3 → ⟨ 1\ 3⟩\\\ 4 → ⟨ 0\ 3\ 1\ 2⟩\\\ 5 → ⟨ 2\ 1⟩\\\ 5 → ⟨ 5⟩  a virus that initially consisted of a single gene 4, could have mutated as follows:  ⟨ 4 ⟩ → ⟨ \underline{0\ 3\ 1\ 2} ⟩ → ⟨ 0\ \underline{2\ 0\ 0}\ 1\ 2 ⟩ → ⟨ 0\ \underline{0\ 1}\ 0\ 0\ 1\ 2 ⟩ → ⟨ 0\ 0\ 1\ 0\ 0\ 1\ \underline{0\ 1} ⟩  or in another way:  ⟨ 4 ⟩ → ⟨ \underline{0\ 3\ 1\ 2} ⟩ → ⟨ 0\ \underline{1\ 3}\ 1\ 2 ⟩ → ⟨ 0\ 1\ 3\ 1\ \underline{0\ 1} ⟩ → ⟨ 0\ 1\ \underline{2\ 0\ 0}\ 1\ 0\ 1 ⟩ → ⟨ 0\ 1\ \underline{0\ 1}\ 0\ 0\ 1\ 0\ 1 ⟩ $$$

Viruses are detected by antibodies that identify the presence of specific continuous fragments of zeros and ones in the viruses' codes. For example, an antibody reacting to a fragment ⟨ 0\ 0\ 1\ 0\ 0 ⟩ will detect a virus ⟨ 0\ 0\ 1\ 0\ 0\ 1\ 0\ 1 ⟩, but it will not detect a virus ⟨ 0\ 1\ 0\ 1\ 0\ 0\ 1\ 0\ 1 ⟩.

For each gene from 2 to G-1, the scientists are wondering whether a given set of antibodies is enough to detect all viruses that can emerge through mutations from this gene. If not, they want to know the length of the shortest virus that cannot be detected.

It may happen that sometimes scientists don't have any antibodies. Then of course no virus can be detected, so the scientists are only interested in the length of the shortest possible virus that can emerge from the gene mutations.

Input

The first line of the input will contain three integers G, N and M (G > 2, N ≥ G - 2, M ≥ 0) specifying the number of genes, the number of rows in the mutation table, and the number of antibodies.

The following N lines contain descriptions of rows of the mutation table; each line begins with two integers a and k (2 ≤ a < G, k ≥ 1), followed by a sequence of k integers b_1, b_2, …, b_k (0 ≤ b_i < G), that encode the row $$$ a → ⟨ b_1\ b_2\ …\ b_k ⟩ $$$

The sum of all values k does not exceed 100. Every integer from 2 to G - 1 appears in the table as a at least once.

The next M lines contain descriptions of the antibodies; each such line begins with an integer ℓ (ℓ ≥ 1), followed by a sequence of ℓ integers c_1, c_2, …, c_ℓ (0 ≤ c_i ≤ 1), describing the antibody. The sum of all values ℓ does not exceed 50.

Output

Your program needs to output exactly G - 2 lines, containing the answers for the subsequent genes from 2 to G - 1.

If all viruses that can mutate from this single gene can be detected by the given set of antibodies, you need to print the word "YES". You also need to print this if there are no viruses that could originate from this gene (it can happen when the sequences never stop mutating).

Otherwise you need to print the word "NO", followed by an integer denoting the minimal length of the undetectable virus. You can assume that for all the prepared input data this value will be smaller than 2^{63}.

Scoring

Subtasks: 

  1. (11 points) No antibodies (M = 0) 
  2. (14 points) N = G - 2 
  3. (25 points) One antibody (M = 1) 
  4. (32 points) The sum of all values ℓ does not exceed 10 
  5. (18 points) No further constraints 

Example

Input


6 6 2
2 2 0 1
3 3 2 0 0
3 2 1 3
4 4 0 3 1 2
5 2 2 1
5 1 5
2 1 1
5 0 0 1 0 0


Output


NO 2
NO 4
NO 9
YES

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  int v = 0, f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = (c & 15);
  while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
  x = v * f;
}
inline void read(long long &x) {
  long long v = 0ll, f = 1ll;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = (c & 15);
  while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
  x = v * f;
}
inline void readc(char &x) {
  char c;
  while (((c = getchar()) == ' ') || c == '\n')
    ;
  x = c;
}
int n, m, g, cg, cn, i, j, k, l, ch[105][2], fa[105], ban[105], cnt = 1,
                                                                fr[105];
vector<int> e[105], ef[105];
int lc[105], rc[105];
long long dp[105][105][105];
priority_queue<pair<long long, pair<int, pair<int, int> > >,
               vector<pair<long long, pair<int, pair<int, int> > > >,
               greater<pair<long long, pair<int, pair<int, int> > > > >
    pq;
void upd(int x, int y, int z, long long c) {
  if (c < dp[x][y][z]) {
    dp[x][y][z] = c;
    pq.push(make_pair(c, make_pair(x, make_pair(y, z))));
  }
}
int main() {
  read(g);
  read(n);
  read(m);
  cg = g;
  cn = n;
  for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
    lc[i] = rc[i] = -1;
    int x, y, t;
    read(x);
    read(y);
    e[x].push_back(t = i);
    read(lc[t]);
    y--;
    while (y--) {
      if (y == 0) {
        read(rc[t]);
      } else {
        rc[t] = cg++;
        e[cg - 1].push_back(++cn);
        t = cn;
        read(lc[t]);
        rc[t] = -1;
      }
    }
  }
  for (((i)) = (0); ((i)) <= (((int)(cg)) - 1); ((i))++) {
    for (__typeof((e[i]).begin()) it = (e[i]).begin(); it != (e[i]).end(); it++)
      fr[*it] = i;
  }
  for (((i)) = (1); ((i)) <= ((cn)); ((i))++) {
    ef[lc[i]].push_back(i);
    if (rc[i] != -1 && rc[i] != lc[i]) ef[rc[i]].push_back(i);
  }
  for (((i)) = (1); ((i)) <= ((m)); ((i))++) {
    int x = 1, l;
    read(l);
    while (l--) {
      int t;
      read(t);
      if (!ch[x][t]) ch[x][t] = ++cnt;
      x = ch[x][t];
    }
    ban[x] = 1;
  }
  queue<int> q;
  for (((i)) = (0); ((i)) <= (((int)(2)) - 1); ((i))++)
    if (ch[1][i]) {
      q.push(ch[1][i]);
      fa[ch[1][i]] = 1;
    } else
      ch[1][i] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    ban[x] |= ban[fa[x]];
    for (((i)) = (0); ((i)) <= (((int)(2)) - 1); ((i))++)
      if (ch[x][i]) {
        q.push(ch[x][i]);
        fa[ch[x][i]] = ch[fa[x]][i];
      } else
        ch[x][i] = ch[fa[x]][i];
  }
  memset((dp), (0x16), (sizeof((dp))));
  for (((i)) = (1); ((i)) <= ((cnt)); ((i))++) {
    if (!ban[i]) {
      if (!ban[ch[i][0]]) {
        upd(0, i, ch[i][0], 1);
      }
      if (!ban[ch[i][1]]) {
        upd(1, i, ch[i][1], 1);
      }
    }
  }
  while (!pq.empty()) {
    long long v = pq.top().first;
    int x = pq.top().second.first, a = pq.top().second.second.first,
        b = pq.top().second.second.second;
    pq.pop();
    if (dp[x][a][b] != v) continue;
    for (__typeof((ef[x]).begin()) it = (ef[x]).begin(); it != (ef[x]).end();
         it++) {
      if (lc[*it] == x) {
        if (rc[*it] == -1) {
          upd(fr[*it], a, b, v);
          continue;
        }
        for (((i)) = (1); ((i)) <= ((cnt)); ((i))++) {
          upd(fr[*it], a, i, v + dp[rc[*it]][b][i]);
        }
      }
      if (rc[*it] == x) {
        for (((i)) = (1); ((i)) <= ((cnt)); ((i))++) {
          upd(fr[*it], i, b, v + dp[lc[*it]][i][a]);
        }
      }
    }
  }
  for ((i) = (2); (i) <= (g - 1); (i)++) {
    long long ans = dp[104][104][104];
    for (((j)) = (1); ((j)) <= ((cnt)); ((j))++)
      if (!ban[j]) ans = min(ans, dp[i][1][j]);
    if (ans >= dp[104][104][104]) {
      puts("YES");
    } else {
      printf("NO %lld\n", ans);
    }
  }
  return 0;
}