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;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const char opname[4][5] = {"and", "nand", "or", "nor"};
const int inputmapping[3][4] = {{0, 1, 0, 1}, {0, 1, 0, 2}, {0, 1, 2, 3}};
int parseop(char *s) {
  for (int i = (0); i < (4); ++i)
    if (strcmp(opname[i], s) == 0) return i;
  assert(false);
  return -1;
}
int op(int id, int x, int y) {
  if (id == 0) return x == 1 && y == 1 ? 1 : 0;
  if (id == 1) return x == 1 && y == 1 ? 0 : 1;
  if (id == 2) return x == 1 || y == 1 ? 1 : 0;
  if (id == 3) return x == 1 || y == 1 ? 0 : 1;
  assert(false);
  return -1;
}
void gen() {
  for (int x = (0); x < (4); ++x)
    for (int y = (0); y < (4); ++y)
      for (int z = (y); z < (4); ++z) {
        for (int i = (0); i < (3); ++i) {
          int masklen = i + 2;
          vector<int> res(1 << masklen, 0);
          for (int mask = (0); mask < (1 << masklen); ++mask) {
            for (int love = (0); love < (2); ++love) {
              int w1 = (mask >> inputmapping[i][0]) & 1,
                  w2 = (mask >> inputmapping[i][1]) & 1,
                  w3 = (mask >> inputmapping[i][2]) & 1,
                  w4 = (mask >> inputmapping[i][3]) & 1;
              int resy = op(y ^ love, w1, w2), resz = op(z ^ love, w3, w4),
                  resx = op(x ^ love, resy, resz);
              res[mask] |= resx << love;
            }
          }
          bool ok = true;
          for (int j = (0); j < (((int)(res).size())); ++j)
            if (res[j] == 3) ok = false;
          if (!ok) continue;
          printf("%s(%s(%c,%c),%s(%c,%c)): ", opname[x], opname[y],
                 'a' + inputmapping[i][0], 'a' + inputmapping[i][1], opname[z],
                 'a' + inputmapping[i][2], 'a' + inputmapping[i][3]);
          for (int j = (0); j < (((int)(res).size())); ++j)
            printf("%d", res[j]);
          puts("");
        }
      }
}
const int MAXA = 50;
const int MAXB = 50;
const int MAXC = 50;
struct Z {
  int kind;
  vector<pair<int, int> > condzero;
};
int na, nb, nc;
int bkind[MAXB], b[MAXB][2];
int ckind[MAXC], c[MAXC][2];
char opbuff[5], inbuff[(MAXB >= MAXC ? MAXB : MAXC) + 1];
Z classify(int id) {
  int x = ckind[id], y = bkind[c[id][0]], z = bkind[c[id][1]],
      w1 = b[c[id][0]][0], w2 = b[c[id][0]][1], w3 = b[c[id][1]][0],
      w4 = b[c[id][1]][1];
  map<int, int> mp;
  vector<int> in(4);
  vector<int> a;
  int nin = 0;
  if (mp.count(w1))
    in[0] = mp[w1];
  else
    mp[w1] = in[0] = nin++, a.push_back(w1);
  if (mp.count(w2))
    in[1] = mp[w2];
  else
    mp[w2] = in[1] = nin++, a.push_back(w2);
  if (mp.count(w3))
    in[2] = mp[w3];
  else
    mp[w3] = in[2] = nin++, a.push_back(w3);
  if (mp.count(w4))
    in[3] = mp[w4];
  else
    mp[w4] = in[3] = nin++, a.push_back(w4);
  int orgmask = 0, lovemask = 0, allmask = 0;
  for (int mask = (0); mask < (1 << nin); ++mask) {
    for (int love = (0); love < (2); ++love) {
      int v1 = (mask >> in[0]) & 1, v2 = (mask >> in[1]) & 1,
          v3 = (mask >> in[2]) & 1, v4 = (mask >> in[3]) & 1;
      int vy = op(y ^ love, v1, v2), vz = op(z ^ love, v3, v4),
          vx = op(x ^ love, vy, vz);
      allmask |= vx << mask;
      if (love == 0)
        orgmask |= vx << mask;
      else
        lovemask |= vx << mask;
    }
  }
  Z ret;
  if ((orgmask & lovemask) != 0) {
    ret.kind = -1;
    return ret;
  }
  assert(orgmask == 0 || lovemask == 0 && allmask != 0);
  ret.kind = orgmask == 0 ? 0 : 1;
  int donemask = 0;
  for (int i = (0); i < (nin); ++i)
    for (int ii = (0); ii < (2); ++ii)
      for (int j = (i + 1); j < (nin); ++j)
        for (int jj = (0); jj < (2); ++jj) {
          bool ok = true;
          for (int k = (0); k < (1 << nin); ++k)
            if (!(((k >> i) & 1) == ii || ((k >> j) & 1) == jj) &&
                ((allmask >> k) & 1) == 0)
              ok = false;
          if (!ok) continue;
          ret.condzero.push_back(make_pair((ii == 0 ? -1 : +1) * (a[i] + 1),
                                           (jj == 0 ? -1 : +1) * (a[j] + 1)));
          for (int k = (0); k < (1 << nin); ++k)
            if (!(((k >> i) & 1) == ii || ((k >> j) & 1) == jj) &&
                ((allmask >> k) & 1) == 1)
              donemask |= 1 << k;
        }
  assert(donemask == allmask);
  return ret;
}
struct G {
  int n;
  vector<int> adj[2 * MAXA];
  int comp[2 * MAXA], ncomp;
  int stck[2 * MAXA], nstck;
  int id[2 * MAXA], lid[2 * MAXA], nid;
  void init(int _n) {
    n = _n;
    for (int i = (0); i < (n); ++i) adj[i].clear();
  }
  void tarjan(int at) {
    id[at] = lid[at] = nid++;
    stck[nstck++] = at;
    for (int i = (0); i < (((int)(adj[at]).size())); ++i) {
      int to = adj[at][i];
      if (id[to] == -1) {
        tarjan(to);
        lid[at] = min(lid[at], lid[to]);
      } else if (comp[to] == -1) {
        lid[at] = min(lid[at], id[to]);
      }
    }
    if (lid[at] >= id[at]) {
      while (comp[at] == -1) comp[stck[--nstck]] = ncomp;
      ++ncomp;
    }
  }
  void calccomp() {
    nid = ncomp = nstck = 0;
    for (int i = (0); i < (n); ++i) comp[i] = id[i] = -1;
    for (int i = (0); i < (n); ++i)
      if (id[i] == -1) tarjan(i);
  }
};
G g;
bool satisfiable(vector<pair<int, int> > cond) {
  g.init(2 * na);
  for (int i = (0); i < (((int)(cond).size())); ++i) {
    int x = cond[i].first, y = cond[i].second;
    int xid = 2 * (abs(x) - 1) + (x > 0 ? 1 : 0),
        yid = 2 * (abs(y) - 1) + (y > 0 ? 1 : 0);
    g.adj[xid ^ 1].push_back(yid);
    g.adj[yid ^ 1].push_back(xid);
  }
  g.calccomp();
  for (int i = (0); i < (na); ++i)
    if (g.comp[2 * i + 0] == g.comp[2 * i + 1]) return false;
  return true;
}
void run() {
  scanf("%d%d%d", &na, &nb, &nc);
  for (int i = (0); i < (nb); ++i) {
    scanf("%s", opbuff);
    bkind[i] = parseop(opbuff);
    scanf("%s", inbuff);
    assert(strlen(inbuff) == na);
    int cnt = 0;
    for (int j = (0); j < (na); ++j)
      if (inbuff[j] == 'x') b[i][cnt++] = j;
    assert(cnt == 2);
  }
  for (int i = (0); i < (nc); ++i) {
    scanf("%s", opbuff);
    ckind[i] = parseop(opbuff);
    scanf("%s", inbuff);
    assert(strlen(inbuff) == nb);
    int cnt = 0;
    for (int j = (0); j < (nb); ++j)
      if (inbuff[j] == 'x') c[i][cnt++] = j;
    assert(cnt == 2);
  }
  vector<pair<int, int> > cond0, cond1;
  int cnt0 = 0, cnt1 = 0;
  for (int i = (0); i < (nc); ++i) {
    Z res = classify(i);
    if (res.kind == 0) {
      ++cnt0;
      for (int j = (0); j < (((int)(res.condzero).size())); ++j)
        cond0.push_back(res.condzero[j]);
    }
    if (res.kind == 1) {
      ++cnt1;
      for (int j = (0); j < (((int)(res.condzero).size())); ++j)
        cond1.push_back(res.condzero[j]);
    }
  }
  bool ok0 = !satisfiable(cond0), ok1 = !satisfiable(cond1);
  if (!ok0 && !ok1) {
    printf("-1\n");
    return;
  }
  int keep = max(ok0 ? cnt0 : 0, ok1 ? cnt1 : 0), ret = nc - keep;
  printf("%d\n", ret);
}
int main() {
  run();
  return 0;
}