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;
struct tarjan_t {
  int n;
  vector<vector<int>> adj;
  vector<int> preorder, inv_preorder;
  stack<int> active;
  vector<bool> is_active;
  vector<int> cnt_of;
  vector<vector<int>> cnts;
  tarjan_t(int _n)
      : n(_n),
        adj(n),
        inv_preorder(n, -1),
        is_active(n, false),
        cnt_of(n, -1) {}
  int dfs(int v) {
    inv_preorder[v] = preorder.size();
    preorder.push_back(v);
    active.push(v);
    is_active[v] = true;
    int highest = inv_preorder[v];
    for (int u : adj[v]) {
      if (inv_preorder[u] == -1)
        highest = min(highest, dfs(u));
      else if (is_active[u])
        highest = min(highest, inv_preorder[u]);
    }
    if (highest == inv_preorder[v]) {
      int u;
      vector<int> cnt;
      do {
        u = active.top();
        active.pop();
        is_active[u] = false;
        cnt.push_back(u);
        cnt_of[u] = cnts.size();
      } while (u != v);
      cnts.push_back(move(cnt));
    }
    return highest;
  }
  void solve() {
    for (int i = 0; i < n; ++i)
      if (inv_preorder[i] == -1) dfs(i);
  }
};
struct twosat_t {
  int n;
  vector<vector<int>> adj;
  twosat_t() {}
  twosat_t(int _n) : n(_n), adj(2 * n) {}
  void add_or(int a, int b) {
    adj[a ^ 1].push_back(b);
    adj[b ^ 1].push_back(a);
  }
  void set(int a) { adj[a ^ 1].push_back(a); }
  bool solve() {
    tarjan_t tarjan(2 * n);
    tarjan.adj = adj;
    tarjan.solve();
    for (int i = 0; i < n; ++i)
      if (tarjan.cnt_of[2 * i] == tarjan.cnt_of[2 * i + 1]) return false;
    return true;
  }
};
enum op_t {
  AND = 0,
  OR,
  NAND,
  NOR,
  INVALID,
};
string read_string() {
  char *str;
  scanf("%ms", &str);
  string result(str);
  free(str);
  return result;
}
vector<int> read_ids() {
  string s = read_string();
  vector<int> result;
  for (int i = 0; i < (int)s.size(); ++i)
    if (s[i] == 'x') result.push_back(i);
  assert(result.size() == 2);
  return result;
}
op_t read_op() {
  string s = read_string();
  if (s == "and") return AND;
  if (s == "or") return OR;
  if (s == "nand") return NAND;
  if (s == "nor") return NOR;
  return INVALID;
}
int apply(op_t op, int a, int b) {
  switch (op) {
    case AND:
      return a & b;
    case OR:
      return a | b;
    case NAND:
      return !(a & b);
    case NOR:
      return !(a | b);
    default:
      return INVALID;
  }
}
op_t mirror(op_t op) {
  switch (op) {
    case AND:
      return OR;
    case OR:
      return AND;
    case NAND:
      return NOR;
    case NOR:
      return NAND;
    default:
      return INVALID;
  }
}
struct gate_t {
  int id = -1;
  op_t op = INVALID;
  gate_t *left = nullptr, *right = nullptr;
  int enforce_value = -1;
  gate_t() {}
  gate_t(int _id) : id(_id) {}
  gate_t(op_t _op, gate_t *_left, gate_t *_right)
      : op(_op), left(_left), right(_right) {}
  int get() {
    if (enforce_value != -1) return enforce_value;
    assert(id == -1);
    return apply(op, left->get(), right->get());
  }
  int get_other() {
    if (enforce_value != -1) return enforce_value;
    assert(id == -1);
    return apply(mirror(op), left->get(), right->get());
  }
  void add_constraints(int target, twosat_t &twosat) {
    int a = left->id, b = right->id;
    op_t cur_op = op;
    if (cur_op == NAND) {
      cur_op = AND;
      target ^= 1;
    } else if (cur_op == NOR) {
      cur_op = OR;
      target ^= 1;
    }
    if (cur_op == AND && target == 0) {
      twosat.add_or(2 * a + 0, 2 * b + 0);
    }
    if (cur_op == AND && target == 1) {
      twosat.set(2 * a + 1);
      twosat.set(2 * b + 1);
    }
    if (cur_op == OR && target == 0) {
      twosat.set(2 * a + 0);
      twosat.set(2 * b + 0);
    }
    if (cur_op == OR && target == 1) {
      twosat.add_or(2 * a + 1, 2 * b + 1);
    }
  }
};
void dfs(int v, int s, const vector<vector<int>> &adj, vector<bool> &mark,
         vector<int> &side0, vector<int> &side1) {
  mark[v] = true;
  if (s == 0)
    side0.push_back(v);
  else
    side1.push_back(v);
  for (int u : adj[v])
    if (!mark[u]) dfs(u, s ^ 1, adj, mark, side0, side1);
}
void propag_to_supersets(vector<long long> &values) {
  for (int i = 1; i < (int)values.size(); ++i) {
    int low_bit = i & -i;
    values[i] |= values[low_bit];
    values[i] |= values[i ^ low_bit];
  }
}
vector<int> get_ids(const vector<gate_t *> &gates) {
  vector<int> result;
  for (gate_t *gate : gates) {
    if (gate->id >= 0)
      result.push_back(gate->id);
    else {
      vector<int> res = get_ids({gate->left, gate->right});
      result.insert(result.end(), res.begin(), res.end());
    }
  }
  sort(result.begin(), result.end());
  result.resize(unique(result.begin(), result.end()) - result.begin());
  return result;
}
int main() {
  int ni, nl1, nl2;
  scanf("%d %d %d", &ni, &nl1, &nl2);
  vector<gate_t> inputs(ni);
  for (int i = 0; i < ni; ++i) inputs[i] = gate_t(i);
  vector<gate_t> layer1(nl1);
  for (int i = 0; i < nl1; ++i) {
    op_t op = read_op();
    vector<int> ids = read_ids();
    layer1[i] = gate_t(op, &inputs[ids[0]], &inputs[ids[1]]);
  }
  vector<gate_t> layer2(nl2);
  for (int i = 0; i < nl2; ++i) {
    op_t op = read_op();
    vector<int> ids = read_ids();
    layer2[i] = gate_t(op, &layer1[ids[0]], &layer1[ids[1]]);
  }
  vector<vector<int>> adj(nl2);
  vector<bool> removed(nl2);
  for (int i = 0; i < nl2; ++i)
    for (int j = i; j < nl2; ++j) {
      vector<int> ids = get_ids({&layer2[i], &layer2[j]});
      bool conflict = false;
      for (int mask = 0; mask < (1 << (int)ids.size()); ++mask) {
        for (int k = 0; k < (int)ids.size(); ++k)
          inputs[ids[k]].enforce_value = ((mask & (1 << k)) != 0);
        int normal_value = layer2[i].get() || layer2[j].get();
        int other_value = layer2[i].get_other() || layer2[j].get_other();
        if (normal_value && other_value) conflict = true;
      }
      for (int k = 0; k < (int)ids.size(); ++k)
        inputs[ids[k]].enforce_value = -1;
      if (conflict) {
        if (i == j)
          removed[i] = true;
        else {
          adj[i].push_back(j);
          adj[j].push_back(i);
        }
      }
    }
  vector<int> left_side, right_side;
  vector<bool> mark = removed;
  for (int i = 0; i < nl2; ++i)
    if (!mark[i]) {
      vector<int> side0, side1;
      dfs(i, 0, adj, mark, side0, side1);
      if (side0.size() > side1.size()) swap(side0, side1);
      left_side.insert(left_side.end(), side0.begin(), side0.end());
      right_side.insert(right_side.end(), side1.begin(), side1.end());
    }
  const int nleft = left_side.size(), nright = right_side.size();
  vector<long long> left_node_mask(nleft);
  for (int i = 0; i < nleft; ++i) {
    int v = left_side[i];
    for (int u : adj[v]) {
      if (!removed[u]) {
        int j =
            find(right_side.begin(), right_side.end(), u) - right_side.begin();
        assert(j != (int)right_side.size());
        left_node_mask[i] |= 1LL << j;
      }
    }
  }
  int half1 = nleft / 2, half2 = (nleft + 1) / 2;
  vector<long long> half1_mask(1 << half1), half2_mask(1 << half2);
  for (int i = 0; i < half1; ++i) half1_mask[1 << i] = left_node_mask[i];
  for (int i = half1; i < nleft; ++i)
    half2_mask[1 << (i - half1)] = left_node_mask[i];
  propag_to_supersets(half1_mask);
  propag_to_supersets(half2_mask);
  const bool ensure_maximal = (nright <= 29);
  vector<bool> visited_right;
  if (ensure_maximal) visited_right.resize(1 << nright, false);
  int max_keep = -1;
  for (int left_mask = (1 << nleft) - 1; left_mask >= 0; --left_mask) {
    long long right_adj_mask = half1_mask[left_mask & ((1 << half1) - 1)] |
                               half2_mask[left_mask >> half1];
    long long right_mask = ((1LL << nright) - 1) ^ right_adj_mask;
    if (ensure_maximal) {
      if (visited_right[right_mask]) continue;
      visited_right[right_mask] = true;
    }
    vector<int> cands;
    for (int i = 0; i < nleft; ++i)
      if (left_mask & (1 << i)) cands.push_back(left_side[i]);
    for (int i = 0; i < nright; ++i)
      if (right_mask & (1LL << i)) cands.push_back(right_side[i]);
    twosat_t twosat(ni);
    for (int c : cands) {
      gate_t &gate = layer2[c];
      gate_t *subs[2] = {gate.left, gate.right};
      vector<int> wont_trigger_masks;
      for (int mask = 0; mask < (1 << 2); ++mask) {
        for (int i = 0; i < 2; ++i)
          subs[i]->enforce_value = ((mask & (1 << i)) != 0);
        bool trigger = gate.get() || gate.get_other();
        if (!trigger) wont_trigger_masks.push_back(mask);
      }
      for (int i = 0; i < 2; ++i) subs[i]->enforce_value = -1;
      if (wont_trigger_masks.size() == 1) {
        for (int i = 0; i < 2; ++i) {
          subs[i]->add_constraints(((wont_trigger_masks[0] & (1 << i)) != 0),
                                   twosat);
        }
      }
    }
    if (!twosat.solve()) max_keep = max(max_keep, (int)cands.size());
  }
  int result;
  if (max_keep == -1)
    result = -1;
  else
    result = nl2 - max_keep;
  printf("%d\n", result);
  return 0;
}