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;
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");
  }
}