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;
std::mt19937 rnd(
    (int)std::chrono::steady_clock::now().time_since_epoch().count());
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXMUTRHSSUM = 100;
const int MAXMUT = MAXMUTRHSSUM;
const int MAXGENE = MAXMUT + 2;
const int MAXPATRHSSUM = 50;
const int MAXPAT = MAXPATRHSSUM;
const int MAXTOK = 2 + MAXMUTRHSSUM;
const int MAXTRIE = 1 + MAXPATRHSSUM;
const int MAXSINGLE = MAXMUTRHSSUM;
const int MAXPAIR = MAXMUTRHSSUM - 1;
const int ALPH = 2;
int ngene, nmut, npat;
vector<int> mut[MAXMUT];
int mutlhs[MAXMUT];
vector<int> muts[MAXGENE];
vector<int> pat[MAXPAT];
long long ans[MAXGENE];
int ntok, nsingle, npair, ntrie;
int singlelhs[MAXSINGLE], singlerhs[MAXSINGLE];
int pairlhs[MAXPAIR];
pair<int, int> pairrhs[MAXPAIR];
vector<int> singleusage[MAXTOK];
vector<pair<int, int>> pairusage[MAXTOK];
long long dp[MAXTOK][MAXTRIE][MAXTRIE];
priority_queue<pair<long long, pair<int, pair<int, int>>>> pq;
struct TrieNode {
  int nxt[ALPH];
  int safenxt[ALPH];
  int suff;
  bool detected;
};
TrieNode trie[MAXTRIE];
int trieroot;
int maketrie() {
  assert(ntrie < MAXTRIE);
  int ret = ntrie++;
  for (int i = (0); i < (ALPH); ++i)
    trie[ret].nxt[i] = trie[ret].safenxt[i] = -1;
  trie[ret].detected = false;
  trie[ret].suff = -1;
  return ret;
}
void buildtrie() {
  ntrie = 0;
  trieroot = maketrie();
  for (int i = (0); i < (npat); ++i) {
    int at = trieroot;
    for (int j = (0); j < (((int)(pat[i]).size())); ++j) {
      int x = pat[i][j];
      assert(0 <= x && x < ALPH);
      int to = trie[at].nxt[x];
      if (to == -1) {
        to = maketrie();
        trie[at].nxt[x] = to;
      }
      at = to;
    }
    trie[at].detected = true;
  }
  queue<int> q;
  q.push(trieroot);
  while (!q.empty()) {
    int at = q.front();
    q.pop();
    for (int k = (0); k < (ALPH); ++k) {
      int suff;
      if (at == trieroot) {
        suff = at;
      } else {
        suff = trie[at].suff;
        while (suff != trieroot && trie[suff].nxt[k] == -1)
          suff = trie[suff].suff;
        if (trie[suff].nxt[k] != -1) suff = trie[suff].nxt[k];
      }
      int to = trie[at].nxt[k];
      trie[at].safenxt[k] = to == -1 ? suff : to;
      if (to != -1) {
        trie[to].suff = suff;
        if (trie[suff].detected) trie[to].detected = true;
        q.push(to);
      }
    }
  }
}
void upd(int x, int s, int t, long long cost) {
  if (cost >= dp[x][s][t]) return;
  dp[x][s][t] = cost;
  pq.push(make_pair(-cost, make_pair(x, make_pair(s, t))));
}
void solve() {
  ntok = ngene, nsingle = 0, npair = 0;
  for (int i = (0); i < (nmut); ++i) {
    if (((int)(mut[i]).size()) == 1) {
      int idx = nsingle++;
      singlelhs[idx] = mutlhs[i];
      singlerhs[idx] = mut[i][0];
      continue;
    }
    int at = 0, last = mutlhs[i];
    while (at + 2 < ((int)(mut[i]).size())) {
      int next = ntok++, idx = npair++;
      pairlhs[idx] = last, pairrhs[idx] = make_pair(mut[i][at], next);
      ++at, last = next;
    }
    {
      int idx = npair++;
      pairlhs[idx] = last;
      pairrhs[idx] = make_pair(mut[i][at], mut[i][at + 1]);
    }
  }
  for (int i = (0); i < (nsingle); ++i) singleusage[singlerhs[i]].push_back(i);
  for (int i = (0); i < (npair); ++i)
    pairusage[pairrhs[i].first].push_back(make_pair(i, 0)),
        pairusage[pairrhs[i].second].push_back(make_pair(i, 1));
  buildtrie();
  for (int x = (0); x < (ntok); ++x)
    for (int s = (0); s < (ntrie); ++s)
      for (int t = (0); t < (ntrie); ++t) dp[x][s][t] = LLONG_MAX;
  for (int x = (0); x < (2); ++x)
    for (int s = (0); s < (ntrie); ++s)
      if (!trie[s].detected) {
        int t = trie[s].safenxt[x];
        if (!trie[t].detected) upd(x, s, t, 1);
      }
  while (!pq.empty()) {
    long long cost = -pq.top().first;
    int x = pq.top().second.first, s = pq.top().second.second.first,
        t = pq.top().second.second.second;
    pq.pop();
    if (cost > dp[x][s][t]) continue;
    for (int i = (0); i < (((int)(singleusage[x]).size())); ++i) {
      int idx = singleusage[x][i];
      int a = singlelhs[idx], b = singlerhs[idx];
      assert(b == x);
      upd(a, s, t, cost);
    }
    for (int i = (0); i < (((int)(pairusage[x]).size())); ++i) {
      int idx = pairusage[x][i].first, side = pairusage[x][i].second;
      int a = pairlhs[idx], b = pairrhs[idx].first, c = pairrhs[idx].second;
      if (side == 0) {
        assert(b == x);
        for (int tt = (0); tt < (ntrie); ++tt)
          if (dp[c][t][tt] != LLONG_MAX) upd(a, s, tt, cost + dp[c][t][tt]);
      }
      if (side == 1) {
        assert(c == x);
        for (int ss = (0); ss < (ntrie); ++ss)
          if (dp[b][ss][s] != LLONG_MAX) upd(a, ss, t, cost + dp[b][ss][s]);
      }
    }
  }
  for (int x = (2); x < (ngene); ++x) ans[x] = LLONG_MAX;
  for (int x = (2); x < (ngene); ++x)
    for (int t = (0); t < (ntrie); ++t)
      if (!trie[t].detected) ans[x] = min(ans[x], dp[x][trieroot][t]);
}
void run() {
  scanf("%d%d%d", &ngene, &nmut, &npat);
  for (int i = (0); i < (nmut); ++i) {
    int a, cnt;
    scanf("%d%d", &a, &cnt);
    vector<int> cur(cnt);
    for (int j = (0); j < (cnt); ++j) scanf("%d", &cur[j]);
    mutlhs[i] = a, mut[i] = cur;
    muts[a].push_back(i);
  }
  for (int i = (0); i < (npat); ++i) {
    int cnt;
    scanf("%d", &cnt);
    vector<int> cur(cnt);
    for (int j = (0); j < (cnt); ++j) scanf("%d", &cur[j]);
    pat[i] = cur;
  }
  solve();
  for (int i = (2); i < (ngene); ++i)
    if (ans[i] == LLONG_MAX)
      printf("YES\n");
    else
      printf("NO %lld\n", ans[i]);
}
int main() {
  run();
  return 0;
}