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 unsigned int maxl = 50, maxk = 100, maxn = maxk, maxg = maxn + 2,
                   maxm = 50;
constexpr unsigned int maxv = maxg * 2, maxe = maxk * 2, avert = maxm * 2;
constexpr unsigned long long inf =
    numeric_limits<unsigned long long>::max() / 3;
template <class T>
using StateMatrix = T[maxv + 10][avert + 10][avert + 10];
class MutationGraph {
 public:
  struct Mutation {
    unsigned int to, another;
    const Mutation* pre;
  } mut[maxe + 10];
  const Mutation* head[maxv + 10];
  inline void add(const unsigned int from, const unsigned int to,
                  const unsigned int another) {
    new (cur) Mutation{to, another, head[from]};
    head[from] = cur++;
  }

 private:
  Mutation* cur = mut;
} single, asFirst, asSecond;
StateMatrix<unsigned long long> dis;
unsigned int gcur;
namespace AhoCorasick {
unsigned int ch[avert + 10][2], fail[avert + 10], id[avert + 10];
bool isEnd[avert + 10];
unsigned int vcur;
void readString() {
  unsigned int len;
  cin >> len;
  unsigned int ptr = 0;
  for (unsigned int i = 0; i < len; ++i) {
    unsigned int cb;
    cin >> cb;
    if (!ch[ptr][cb]) ch[ptr][cb] = ++vcur;
    ptr = ch[ptr][cb];
  }
  isEnd[ptr] = true;
}
unsigned int reLabel() {
  unsigned int icur = 0;
  for (unsigned int i = 0; i <= vcur; ++i)
    if (!isEnd[i]) id[i] = icur++;
  return icur;
}
unsigned int build() {
  queue<unsigned int> q;
  for (unsigned int i : ch[0])
    if (i) q.push(i);
  while (!q.empty()) {
    const unsigned int cur = q.front();
    q.pop();
    for (unsigned int i = 0; i < 2; ++i)
      if (!ch[cur][i])
        ch[cur][i] = ch[fail[cur]][i];
      else {
        const unsigned int cv = ch[cur][i];
        fail[cv] = ch[fail[cur]][i];
        isEnd[cv] = isEnd[cv] || isEnd[fail[cv]];
        q.push(cv);
      }
  }
  return reLabel();
}
}  // namespace AhoCorasick
void addMutation(const unsigned int a) {
  static unsigned int buf[maxk + 10];
  unsigned int k;
  cin >> k;
  copy_n(istream_iterator<unsigned int>(cin), k, buf);
  if (k == 1) {
    single.add(buf[0], a, 0);
    return;
  }
  unsigned int lst = buf[k - 1];
  for (const unsigned int* i = buf + k - 2; i > buf; --i) {
    const unsigned int virt = gcur++;
    asFirst.add(*i, virt, lst);
    asSecond.add(lst, virt, *i);
    lst = virt;
  }
  asFirst.add(buf[0], a, lst);
  asSecond.add(lst, a, buf[0]);
}
struct StateNode {
  unsigned int id, start, end;
  unsigned long long dis;
  template <class T>
  inline T& operator[](StateMatrix<T> mat) const {
    return mat[id][start][end];
  }
};
struct StateLess {
  inline bool operator()(const StateNode& a, const StateNode& b) const {
    return a.dis > b.dis;
  }
};
static void dpInit(priority_queue<StateNode, vector<StateNode>, StateLess>& q) {
  using namespace AhoCorasick;
  for (unsigned int i = 0; i <= vcur; ++i) {
    if (isEnd[i]) continue;
    for (unsigned int j = 0; j < 2; ++j) {
      const unsigned int nvert = ch[i][j];
      if (!isEnd[nvert]) {
        dis[j][id[i]][id[nvert]] = 1;
        q.push(StateNode{j, id[i], id[nvert], 1});
      }
    }
  }
}
void dijkstra(const unsigned int acId) {
  static StateMatrix<bool> vis;
  priority_queue<StateNode, vector<StateNode>, StateLess> q;
  fill(reinterpret_cast<unsigned long long*>(dis),
       reinterpret_cast<unsigned long long*>(dis) +
           sizeof(dis) / sizeof(unsigned long long),
       inf);
  dpInit(q);
  auto update = [&q](const StateNode&& sn) {
    if (sn[dis] > sn.dis) {
      sn[dis] = sn.dis;
      q.push(sn);
    }
  };
  while (!q.empty()) {
    const StateNode cur = q.top();
    q.pop();
    if (cur[vis]) continue;
    cur[vis] = true;
    for (auto i = single.head[cur.id]; i; i = i->pre)
      update(StateNode{i->to, cur.start, cur.end, cur.dis});
    for (auto i = asFirst.head[cur.id]; i; i = i->pre)
      for (unsigned int j = 0; j < acId; ++j)
        update(StateNode{i->to, cur.start, j,
                         cur.dis + dis[i->another][cur.end][j]});
    for (auto i = asSecond.head[cur.id]; i; i = i->pre)
      for (unsigned int j = 0; j < acId; ++j)
        update(StateNode{i->to, j, cur.end,
                         dis[i->another][j][cur.start] + cur.dis});
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  unsigned int g, n, m;
  cin >> g >> n >> m;
  gcur = g;
  for (unsigned int i = 0; i < n; ++i) {
    unsigned int a;
    cin >> a;
    addMutation(a);
  }
  for (unsigned int i = 0; i < m; ++i) AhoCorasick::readString();
  const unsigned int acId = AhoCorasick::build();
  dijkstra(acId);
  for (unsigned int i = 2; i < g; ++i) {
    unsigned long long ans = inf;
    for (unsigned int s = 0; s < acId; ++s)
      ans = min(ans, *min_element(dis[i][s], dis[i][s] + acId));
    if (ans < inf)
      cout << "NO " << ans << "\n";
    else
      cout << "YES\n";
  }
  return 0;
}