Problem: Hibiki and Dita are in love with each other, but belong to communities that are in a long lasting conflict. Hibiki is deeply concerned with the state of affairs, and wants to figure out if his relationship with Dita is an act of love or an act of treason.

<image>

Hibiki prepared several binary features his decision will depend on, and built a three layer logical circuit on top of them, each layer consisting of one or more [logic gates](https://en.wikipedia.org/wiki/Logic_gate). Each gate in the circuit is either "or", "and", "nor" (not or) or "nand" (not and). Each gate in the first layer is connected to exactly two features. Each gate in the second layer is connected to exactly two gates in the first layer. The third layer has only one "or" gate, which is connected to all the gates in the second layer (in other words, the entire circuit produces 1 if and only if at least one gate in the second layer produces 1).

The problem is, Hibiki knows very well that when the person is in love, his ability to think logically degrades drastically. In particular, it is well known that when a person in love evaluates a logical circuit in his mind, every gate evaluates to a value that is the opposite of what it was supposed to evaluate to. For example, "or" gates return 1 if and only if both inputs are zero, "t{nand}" gates produce 1 if and only if both inputs are one etc.

In particular, the "or" gate in the last layer also produces opposite results, and as such if Hibiki is in love, the entire circuit produces 1 if and only if all the gates on the second layer produced 0.

Hibiki can’t allow love to affect his decision. He wants to know what is the smallest number of gates that needs to be removed from the second layer so that the output of the circuit for all possible inputs doesn't depend on whether Hibiki is in love or not.

Input

The first line contains three integers n, m, k (2 ≤ n, m ≤ 50; 1 ≤ k ≤ 50) — the number of input features, the number of gates in the first layer, and the number of gates in the second layer correspondingly.

The second line contains m pairs of strings separated by spaces describing the first layer. The first string in each pair describes the gate ("and", "or", "nand" or "nor"), and the second string describes the two input features the gate is connected two as a string consisting of exactly n characters, with exactly two characters (that correspond to the input features the gate is connected to) equal to 'x' and the remaining characters equal to ".'.

The third line contains k pairs of strings separated by spaces describing the second layer in the same format, where the strings that describe the input parameters have length m and correspond to the gates of the first layer.

Output

Print the number of gates that need to be removed from the second layer so that the output of the remaining circuit doesn't depend on whether Hibiki is in love or not.

If no matter how many gates are removed the output of the circuit continues to depend on Hibiki's feelings, print -1.

Examples

Input

2 2 2
and xx nand xx
and xx or xx


Output

1


Input

3 2 2
and xx. nor .xx
and xx nor xx


Output

-1


Input

4 4 5
nor x..x and ..xx and xx.. nand xx..
nand ..xx nor ..xx and xx.. nor ..xx or ..xx


Output

2

Note

In the first example the two gates in the first layer are connected to the same inputs, but first computes "and" while second computes "nand", and as such their output is always different no matter what the input is and whether Hibiki is in love or not. The second layer has "or" and "and" gates both connected to the two gates in the first layer. If Hibiki is not in love, the "and" gate will produce 0 and the "or" gate will produce 1 no matter what input features are equal to, with the final "or" gate in the third layer always producing the final answer of 1. If Hibiki is in love, "and" gate in the second layer will produce 1 and "or" gate will produce 0 no matter what the input is, with the final "or" gate in the third layer producing the final answer of 0. Thus, if both gates in the second layer are kept, the output of the circuit does depend on whether Hibiki is in love. If any of the two gates in the second layer is dropped, the output of the circuit will no longer depend on whether Hibiki is in love or not, and hence the answer is 1.

In the second example no matter what gates are left in the second layer, the output of the circuit will depend on whether Hibiki is in love or not.

In the third example if Hibiki keeps second, third and fourth gates in the second layer, the circuit will not depend on whether Hibiki is in love or not. Alternatively, he can keep the first and the last gates. The former requires removing two gates, the latter requires removing three gates, so the former is better, and the answer is 2.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T &t1, T t2) {
  t1 = max(t1, t2);
}
template <typename T>
void mintt(T &t1, T t2) {
  t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
  assert(y >= 0);
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et(int x = -1) {
  printf("%d\n", x);
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
struct yq {
  string ope;
  bool smV = false;
  int a, b;
  void rd(int n) {
    string tmp;
    cin >> ope >> tmp;
    a = 0;
    b = 0;
    for (int(i) = 0; (i) < (int)(n); (i)++)
      if (tmp[i] == 'x') {
        if (a == 0)
          a = i + 1;
        else
          b = i + 1;
      }
  }
} l3[55], l2[55];
bool isSpa(yq &x, yq &y) { return x.a == y.a && x.b == y.b; }
bool isCpa(yq &x, yq &y) {
  return x.a == y.a || x.a == y.b || x.b == y.a || x.b == y.b;
}
int getType(yq &o) {
  yq c1 = l3[o.a], c2 = l3[o.b];
  if (!o.smV) {
    if (!isCpa(c1, c2)) return -1;
    if (o.ope == "and") {
      if (c1.ope == "and" && c2.ope == "nor") return 0;
      if (c2.ope == "and" && c1.ope == "nor") return 0;
    } else if (o.ope == "nor") {
      if (c1.ope == "nand" && c2.ope == "or") return 0;
      if (c2.ope == "nand" && c1.ope == "or") return 0;
    }
    return -1;
  }
  if (o.ope == "and") {
    if (c1.ope == "and" && c2.ope == "nand") return 1;
    if (c1.ope == "or" && c2.ope == "nor") return 1;
    if (c1.ope == "and" && c2.ope == "nor") return 0;
    swap(c1, c2);
    if (c1.ope == "and" && c2.ope == "nand") return 1;
    if (c1.ope == "or" && c2.ope == "nor") return 1;
    if (c1.ope == "and" && c2.ope == "nor") return 0;
  } else if (o.ope == "nor") {
    if (c1.ope == "and" && c2.ope == "nand") return 1;
    if (c1.ope == "or" && c2.ope == "nor") return 1;
    if (c1.ope == "nand" && c2.ope == "or") return 0;
    swap(c1, c2);
    if (c1.ope == "and" && c2.ope == "nand") return 1;
    if (c1.ope == "or" && c2.ope == "nor") return 1;
    if (c1.ope == "nand" && c2.ope == "or") return 0;
  }
  return -1;
}
int getType2(yq &o) {
  yq c1 = l3[o.a], c2 = l3[o.b];
  if (!o.smV) {
    if (!isCpa(c1, c2)) return -1;
    if (o.ope == "or") {
      if (c1.ope == "and" && c2.ope == "nor") return 0;
      if (c2.ope == "and" && c1.ope == "nor") return 0;
    } else if (o.ope == "nand") {
      if (c1.ope == "nand" && c2.ope == "or") return 0;
      if (c2.ope == "nand" && c1.ope == "or") return 0;
    }
    return -1;
  }
  if (o.ope == "or") {
    if (c1.ope == "and" && c2.ope == "nand") return 1;
    if (c1.ope == "or" && c2.ope == "nor") return 1;
    if (c1.ope == "and" && c2.ope == "nor") return 0;
    swap(c1, c2);
    if (c1.ope == "and" && c2.ope == "nand") return 1;
    if (c1.ope == "or" && c2.ope == "nor") return 1;
    if (c1.ope == "and" && c2.ope == "nor") return 0;
  } else if (o.ope == "nand") {
    if (c1.ope == "and" && c2.ope == "nand") return 1;
    if (c1.ope == "or" && c2.ope == "nor") return 1;
    if (c1.ope == "nand" && c2.ope == "or") return 0;
    swap(c1, c2);
    if (c1.ope == "and" && c2.ope == "nand") return 1;
    if (c1.ope == "or" && c2.ope == "nor") return 1;
    if (c1.ope == "nand" && c2.ope == "or") return 0;
  }
  return -1;
}
int ans = -1, cc[2];
void fmain(int ID) {
  scanf("%d%d%d", &k, &n, &m);
  for (int(i) = 1; (i) <= (int)(n); (i)++) l3[i].rd(k);
  for (int(i) = 1; (i) <= (int)(m); (i)++) l2[i].rd(n);
  for (int(i) = 0; (i) < (int)(2); (i)++) cc[i] = 0;
  for (int(i) = 1; (i) <= (int)(m); (i)++) {
    l2[i].smV = isSpa(l3[l2[i].a], l3[l2[i].b]);
    int z = getType(l2[i]);
    if (z != -1) cc[z]++;
  }
  if (cc[1]) ans = cc[0] + cc[1];
  for (int(i) = 0; (i) < (int)(2); (i)++) cc[i] = 0;
  for (int(i) = 1; (i) <= (int)(m); (i)++) {
    int z = getType2(l2[i]);
    if (z != -1) cc[z]++;
  }
  if (cc[1]) maxtt(ans, cc[0] + cc[1]);
  if (ans != -1) ans = m - ans;
  printf("%d\n", ans);
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}