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;
const int MK = 1000, ML = 51;
namespace AhoCorasick {
struct node {
  int next[2];
  bool leaf;
  int p;
  char pch;
  int link;
  int go[2];
};
node t[ML + 1];
int sz;
void readString() {
  int len;
  ignore = scanf("%d", &len);
  int v = 0;
  for (int i = 0, c; i < len; i++) {
    ignore = scanf("%d", &c);
    if (t[v].next[c] == -1) {
      memset(t[sz].next, -1, sizeof t[sz].next);
      memset(t[sz].go, -1, sizeof t[sz].go);
      t[sz].link = -1;
      t[sz].p = v;
      t[sz].pch = c;
      t[v].next[c] = sz++;
    }
    v = t[v].next[c];
  }
  t[v].leaf = true;
}
int go(int v, int c);
int get_link(int v) {
  if (t[v].link == -1) {
    if (v == 0 || t[v].p == 0)
      t[v].link = 0;
    else
      t[v].link = go(get_link(t[v].p), t[v].pch);
    get_link(t[v].link);
    if (t[t[v].link].leaf) t[v].leaf = true;
  }
  return t[v].link;
}
int go(int v, int c) {
  if (t[v].go[c] == -1) {
    if (t[v].next[c] != -1)
      t[v].go[c] = t[v].next[c];
    else
      t[v].go[c] = v == 0 ? 0 : go(get_link(v), c);
  }
  return t[v].go[c];
}
int to[ML][2];
int create(int m) {
  t[0].p = t[0].link = -1;
  memset(t[0].next, -1, sizeof t[0].next);
  memset(t[0].go, -1, sizeof t[0].go);
  sz = 1;
  for (int i = 0; i < m; i++) readString();
  int cnt = 0;
  map<int, int> mp;
  for (int i = 0; i < sz; i++) {
    get_link(i);
    if (t[i].leaf) continue;
    mp[i] = cnt++;
  }
  for (auto p : mp) {
    int v = p.first;
    for (int c = 0; c < 2; c++) {
      int u = go(v, c);
      if (t[u].leaf)
        to[p.second][c] = -1;
      else
        to[p.second][c] = mp[u];
    }
  }
  return cnt;
}
}  // namespace AhoCorasick
struct Int {
  static const unsigned long long INF;
  unsigned long long x;
  Int(unsigned long long x = INF) : x(x) {}
  Int operator+(const Int& rhs) const {
    if (x == INF || rhs.x == INF) return INF;
    return min(x + rhs.x, INF);
  }
  bool operator<(const Int& rhs) const {
    if (x == INF) return false;
    if (rhs.x == INF) return true;
    return x < rhs.x;
  }
};
const unsigned long long Int::INF = 1ULL << 63;
int a[MK], b[3 * MK], firstB[MK], lastB[MK];
vector<int> mutations[MK];
void simplifyMutations(int& n, int g) {
  int it = lastB[n - 1];
  for (int i = 0; i < n; i++) {
    int k = lastB[i] - firstB[i];
    if (k <= 2) continue;
    a[n] = g;
    firstB[n] = firstB[i];
    lastB[n] = lastB[i] - 1;
    firstB[i] = it;
    b[it++] = g;
    b[it++] = b[lastB[i] - 1];
    lastB[i] = it;
    g++;
    n++;
  }
  for (int i = 0; i < n; i++) {
    for (int j = firstB[i]; j < lastB[i]; j++) {
      mutations[b[j]].push_back(i);
    }
  }
}
int L;
Int dp[MK][ML][ML];
set<tuple<unsigned long long, int, int, int>> S;
void update(int f, int s, int e, const Int& val) {
  if (val < dp[f][s][e]) {
    if (dp[f][s][e].x != Int::INF) {
      S.erase(make_tuple(dp[f][s][e].x, f, s, e));
    }
    dp[f][s][e] = val;
    S.emplace(val.x, f, s, e);
  }
}
void recalcMutationFast(int mut, int f, int s, int e) {
  int x = a[mut];
  int y = b[firstB[mut]];
  int k = lastB[mut] - firstB[mut];
  if (k == 1) {
    update(x, s, e, dp[y][s][e]);
    return;
  }
  assert(k == 2);
  int z = b[firstB[mut] + 1];
  if (y == f) {
    for (int t = 0; t < L; t++) {
      update(x, s, t, dp[y][s][e] + dp[z][e][t]);
    }
  }
  if (z == f) {
    for (int t = 0; t < L; t++) {
      update(x, t, e, dp[y][t][s] + dp[z][s][e]);
    }
  }
}
int main() {
  int g, n, m;
  ignore = scanf("%d %d %d", &g, &n, &m);
  for (int i = 0, it = 0, k; i < n; i++) {
    ignore = scanf("%d %d", a + i, &k);
    firstB[i] = it;
    for (int j = 0; j < k; j++) {
      ignore = scanf("%d", b + (it++));
    }
    lastB[i] = it;
  }
  simplifyMutations(n, g);
  L = AhoCorasick::create(m);
  for (int s = 0; s < L; s++) {
    for (int c = 0; c < 2; c++) {
      if (AhoCorasick::to[s][c] == -1) continue;
      update(c, s, AhoCorasick::to[s][c], 1);
    }
  }
  while (S.empty() == false) {
    int f, s, e;
    tie(ignore, f, s, e) = *S.begin();
    S.erase(S.begin());
    for (int i : mutations[f]) recalcMutationFast(i, f, s, e);
  }
  for (int i = 2; i < g; i++) {
    Int ans;
    for (int s = 0; s < L; s++)
      for (int e = 0; e < L; e++) ans = min(dp[i][s][e], ans);
    if (ans.x == Int::INF)
      printf("%s\n", "YES");
    else
      printf("%s %llu\n", "NO", ans.x);
  }
  return 0;
}