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;
using ll = long long;
const ll INF = (ll)1e18 + 7;
struct Prique {
  const ll INF = 4 * (ll)1e18;
  vector<pair<ll, int>> data;
  const int n;
  Prique(int siz) : n(siz), data(2 * siz, {INF, -1}) { data[0] = {-INF, -1}; }
  void push(int i, ll v) {
    data[i + n] = {v, (v >= INF ? -1 : i)};
    for (i += n; i > 1; i >>= 1) data[i >> 1] = min(data[i], data[i ^ 1]);
  }
  void decKey(int i, ll v) {
    for (int j = i + n; data[j].first > v; j >>= 1) data[j] = {v, i};
  }
  pair<ll, int> pop() {
    auto res = data[1];
    push(res.second, INF);
    return res;
  }
  bool empty() { return data[1].first >= INF; }
};
class AhoCora {
 private:
  vector<int> pat_ind;
  vector<int> suf_link;
  vector<int> pat_link;
  vector<map<char, int>> conns;
  int makeNode() {
    pat_ind.push_back(-1);
    suf_link.push_back(-1);
    pat_link.push_back(-1);
    conns.emplace_back();
    return (int)pat_ind.size() - 1;
  }
  int get(int i, char c) {
    auto it = conns[i].find(c);
    if (it == conns[i].end())
      return -1;
    else
      return (*it).second;
  }
  void set(int i, char c, int t) { conns[i][c] = t; }
  int extend(int i, char c) {
    int res = -1;
    while ((i != -1) && (res == -1)) {
      res = get(i, c);
      i = suf_link[i];
    }
    return max(res, 0);
  }

 public:
  AhoCora() { makeNode(); }
  void addPattern(const string& pat) {
    int i = 0;
    for (char c : pat) {
      if (get(i, c) == -1) set(i, c, makeNode());
      i = get(i, c);
    }
    pat_ind[i] = 1;
  }
  void buildLinks() {
    vector<int> que;
    que.push_back(0);
    for (int j = 0; j < que.size(); ++j) {
      int i = que[j];
      for (auto pr : conns[i]) {
        char c = pr.first;
        int t = pr.second;
        suf_link[t] = extend(suf_link[i], c);
        if (pat_ind[suf_link[t]] != -1)
          pat_link[t] = suf_link[t];
        else
          pat_link[t] = pat_link[suf_link[t]];
        que.push_back(t);
      }
    }
  }
  int addChar(int i, char c) {
    int t = extend(i, c);
    if (pat_ind[t] != -1 || pat_link[t] != -1)
      return -1;
    else
      return t;
  }
  int size() { return pat_ind.size(); }
};
const int G = 102;
const int N = 100;
const int M = 50;
vector<int> apps[G];
vector<int> mts[N];
ll moves[G][M + 1][M + 1];
ll cur[2][M + 1][M + 1];
vector<int> ss, tt;
void clearCur(int d, int k) {
  for (int s = 0; s < k; ++s) {
    for (int t = 0; t < k; ++t) cur[d][s][t] = INF;
  }
}
void updateVal(int i, int s, int t, ll v, Prique& que) {
  if (moves[i][s][t] > v) {
    moves[i][s][t] = min(moves[i][s][t], v);
    que.decKey(i, v);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int g, n, m;
  cin >> g >> n >> m;
  for (int j = 0; j < n; ++j) {
    int i, len;
    cin >> i >> len;
    mts[j].resize(len + 1);
    mts[j][0] = i;
    for (int x = 1; x <= len; ++x) {
      cin >> mts[j][x];
      bool first_app = 1;
      for (int y = 1; y < x; ++y) {
        if (mts[j][y] == mts[j][x]) first_app = 0;
      }
      if (first_app) apps[mts[j][x]].push_back(j);
    }
  }
  AhoCora ac;
  for (int i = 0; i < m; ++i) {
    string str;
    int len;
    cin >> len;
    for (int j = 0; j < len; ++j) {
      int b;
      cin >> b;
      str.push_back('0' + b);
    }
    ac.addPattern(str);
  }
  ac.buildLinks();
  int k = ac.size();
  for (int i = 0; i < g; ++i) {
    for (int s = 0; s < k; ++s) {
      for (int t = 0; t < k; ++t) moves[i][s][t] = INF;
    }
  }
  Prique que(g);
  for (int s = 0; s < k; ++s) {
    int t = ac.addChar(s, '0');
    if (t != -1) updateVal(0, s, t, 1, que);
    t = ac.addChar(s, '1');
    if (t != -1) updateVal(1, s, t, 1, que);
  }
  while (!que.empty()) {
    int i = que.pop().second;
    for (int pat_ind : apps[i]) {
      vector<int>& pat = mts[pat_ind];
      int ti = pat[0];
      clearCur(0, k);
      clearCur(1, k);
      for (int s = 0; s < k; ++s) cur[0][s][s] = 0;
      for (int j = 1; j < pat.size(); ++j) {
        int ci = pat[j];
        int d1 = (j & 1);
        int d0 = 1 - d1;
        clearCur(d1, k);
        for (int p = 0; p < k; ++p) {
          tt.clear();
          for (int t = 0; t < k; ++t) {
            if (moves[ci][p][t] < INF) tt.push_back(t);
          }
          if (tt.empty()) continue;
          ss.clear();
          for (int s = 0; s < k; ++s) {
            if (cur[d0][s][p] < INF) ss.push_back(s);
          }
          for (auto s : ss) {
            for (auto t : tt) {
              cur[d1][s][t] =
                  min(cur[d1][s][t], cur[d0][s][p] + moves[ci][p][t]);
            }
          }
        }
      }
      int dl = ((int)pat.size() - 1) & 1;
      for (int s = 0; s < k; ++s) {
        for (int t = 0; t < k; ++t) {
          updateVal(ti, s, t, cur[dl][s][t], que);
        }
      }
    }
  }
  for (int i = 2; i < g; ++i) {
    ll res = INF;
    for (int t = 0; t < k; ++t) {
      res = min(res, moves[i][0][t]);
    }
    if (res >= INF)
      cout << "YES\n";
    else
      cout << "NO " << res << '\n';
  }
}