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>
#pragma GCC optimize("O3")
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
  if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
  if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}
const int ALPHA_BASE = 0;
const int ALPHA_SIZE = 2;
struct Node {
  long long sum;
  int mask, head, fail;
  Node() {
    sum = 0;
    mask = 0;
    head = 0;
    fail = 0;
  }
  int child(char c) const {
    int t = c - ALPHA_BASE;
    if (~mask >> t & 1) return -1;
    return head + __builtin_popcount(mask & ((1 << t) - 1));
  }
};
vector<Node> nodes;
int get_next(int x, char c) {
  while (x >= 0 && ~nodes[x].mask >> (c - ALPHA_BASE) & 1) x = nodes[x].fail;
  if (x < 0)
    return 0;
  else
    return nodes[x].child(c);
}
template <class ITER>
void build(ITER begin, ITER end) {
  int n = end - begin;
  vector<int> idx(n);
  for (int i = 0, i_len = (n); i < i_len; ++i) idx[i] = i;
  sort(idx.begin(), idx.end(),
       [&](int i, int j) { return begin[i] < begin[j]; });
  nodes.clear();
  nodes.push_back(Node());
  vector<int> depth, left, right;
  depth.push_back(0);
  left.push_back(0);
  right.push_back(n);
  for (int i = 0; i < (int)nodes.size(); i++) {
    nodes[i].head = nodes.size();
    int j = left[i];
    while (j < right[i] && (int)begin[idx[j]].size() <= depth[i]) {
      nodes[i].sum += 1;
      j++;
    }
    while (j < right[i]) {
      const char c = begin[idx[j]][depth[i]];
      int k = j;
      while (k < right[i] && c == begin[idx[k]][depth[i]]) k++;
      nodes.push_back(Node());
      depth.push_back(depth[i] + 1);
      left.push_back(j);
      right.push_back(k);
      nodes[i].mask |= 1 << (c - ALPHA_BASE);
      j = k;
    }
  }
  nodes[0].fail = -1;
  for (int i = 1; i < (int)nodes.size(); i++) {
    int k = nodes[i].head;
    for (int j = 0; j < ALPHA_SIZE; j++)
      if (nodes[i].mask >> j & 1) {
        int f = nodes[i].fail;
        nodes[k].fail = get_next(f, j + ALPHA_BASE);
        nodes[k].sum += nodes[nodes[k].fail].sum;
        k++;
      }
  }
}
int G, N, M;
struct Mutation {
  int from;
  vector<int> to;
  Mutation() {}
  bool operator<(const Mutation &o) const { return from < o.from; }
};
vector<Mutation> table;
vector<vector<int> > anti;
__int128 INF;
__int128 dp[111];
namespace SUBTASK1 {
void solve() {
  memset(dp, 0x3f, sizeof dp);
  INF = dp[0];
  dp[0] = dp[1] = 1;
  while (1) {
    bool update = false;
    for (__typeof((table).begin()) e = (table).begin(), e_end = (table).end();
         e != e_end; ++e) {
      vector<int> &to = e->to;
      __int128 sum = 0;
      for (__typeof((to).begin()) f = (to).begin(), f_end = (to).end();
           f != f_end; ++f) {
        if (dp[*f] == INF) {
          sum = INF;
          break;
        }
        sum += dp[*f];
      }
      if (sum < dp[e->from]) {
        update = true;
        dp[e->from] = sum;
      }
    }
    if (!update) break;
  }
}
};  // namespace SUBTASK1
namespace SUBTASK2 {
int nxt[55][111];
bool dead_edge[55][111];
void solve() {
  build(anti.begin(), anti.end());
  vector<pair<__int128, int> > B;
  for (int i = 2; i < G; i++)
    if (dp[i] != INF) B.emplace_back(dp[i], i);
  sort(B.begin(), B.end());
  memset(nxt, -1, sizeof nxt);
  for (int x = 0, x_len = (nodes.size()); x < x_len; ++x) {
    nxt[x][0] = get_next(x, 0);
    nxt[x][1] = get_next(x, 1);
  }
  for (int t = 0, t_len = (2); t < t_len; ++t) {
    vector<int> one(1, t);
    if (count(anti.begin(), anti.end(), one)) {
      dp[t] = INF;
      for (int x = 0, x_len = (nodes.size()); x < x_len; ++x) {
        dead_edge[x][t] = true;
      }
    }
  }
  for (int i = 0, i_len = (B.size()); i < i_len; ++i) {
    int g = B[i].second;
    vector<int> &to = table[g - 2].to;
    for (int x = 0, x_len = (nodes.size()); x < x_len; ++x) {
      int cur = x;
      bool dead = false;
      for (int j = 0, j_len = (to.size()); j < j_len; ++j) {
        if (dead_edge[cur][to[j]]) dead = true;
        cur = nxt[cur][to[j]];
        if (nodes[cur].sum) dead = true;
      }
      if (dead) dead_edge[x][g] = true;
      nxt[x][g] = cur;
    }
    if (dead_edge[0][g]) {
      dp[g] = INF;
    }
  }
}
};  // namespace SUBTASK2
namespace SUBTASK5 {
__int128 cost[55][111][55];
__int128 buf[55][55];
int update_table[111];
void solve() {
  build(anti.begin(), anti.end());
  memset(cost, 0x3f, sizeof cost);
  for (int t = 0, t_len = (2); t < t_len; ++t) {
    vector<int> one(1, t);
    if (count(anti.begin(), anti.end(), one)) {
      dp[t] = INF;
    } else {
      for (int x = 0, x_len = (nodes.size()); x < x_len; ++x) {
        int y = get_next(x, t);
        if (nodes[y].sum == 0) cost[x][t][y] = 1;
      }
    }
  }
  for (int cnt = 1;; cnt++) {
    bool update = false;
    for (int i = 0, i_len = (table.size()); i < i_len; ++i) {
      int from = table[i].from;
      vector<int> &to = table[i].to;
      bool need_update = false;
      for (int j = 0, j_len = (to.size()); j < j_len; ++j)
        if (update_table[to[j]] == cnt - 1) need_update = true;
      if (!need_update) continue;
      for (int x = 0, x_len = (nodes.size()); x < x_len; ++x) {
        memset(buf, 0x3f, sizeof buf);
        buf[0][x] = 0;
        for (int j = 0, j_len = (to.size()); j < j_len; ++j)
          for (int y = 0, y_len = (nodes.size()); y < y_len; ++y)
            if (buf[j][y] < INF && nodes[y].sum == 0) {
              for (int z = 0, z_len = (nodes.size()); z < z_len; ++z) {
                __int128 tmp = buf[j][y] + cost[y][to[j]][z];
                amin(buf[j + 1][z], tmp);
              }
            }
        for (int y = 0, y_len = (nodes.size()); y < y_len; ++y)
          if (cost[x][from][y] > buf[to.size()][y]) {
            update_table[from] = cnt;
            update = true;
            cost[x][from][y] = buf[to.size()][y];
          }
      }
    }
    if (!update) break;
  }
  static __int128 ans[111];
  for (int g = 0, g_len = (G); g < g_len; ++g) ans[g] = INF;
  for (int g = 2; g < G; g++)
    if (dp[g] != INF) {
      for (int y = 0, y_len = (nodes.size()); y < y_len; ++y)
        amin(ans[g], cost[0][g][y]);
    }
  for (int g = 0, g_len = (G); g < g_len; ++g) dp[g] = ans[g];
}
};  // namespace SUBTASK5
void MAIN() {
  scanf("%d%d%d", &G, &N, &M);
  table.reserve(N);
  for (int i = 0, i_len = (N); i < i_len; ++i) {
    int a, len;
    scanf("%d%d", &a, &len);
    table.emplace_back();
    table.back().from = a;
    table.back().to.reserve(len);
    for (int j = 0, j_len = (len); j < j_len; ++j) {
      int x;
      scanf("%d", &x);
      table.back().to.push_back(x);
    }
  }
  sort(table.begin(), table.end());
  anti.reserve(M);
  for (int i = 0, i_len = (M); i < i_len; ++i) {
    int len;
    scanf("%d", &len);
    anti.emplace_back();
    anti.back().reserve(len);
    for (int j = 0, j_len = (len); j < j_len; ++j) {
      int x;
      scanf("%d", &x);
      anti.back().push_back(x);
    }
  }
  SUBTASK1::solve();
  SUBTASK5::solve();
  for (int i = 2; i < G; i++) {
    if (dp[i] == INF)
      puts("YES");
    else
      printf("NO %llu\n", (unsigned long long)dp[i]);
  }
}
int main() {
  int TC = 1;
  for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();
  return 0;
}