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;
using cat = long long;
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  int N[3];
  cin >> N[0] >> N[1] >> N[2];
  string tp[2][100];
  int inp[2][100][2];
  for (int k = 0; k < 2; k++)
    for (int i = 0; i < N[k + 1]; i++) {
      string inp_s;
      cin >> tp[k][i] >> inp_s;
      int a = 0;
      for (int j = 0; j < N[k]; j++)
        if (inp_s[j] == 'x') inp[k][i][a++] = j;
    }
  bool del[100];
  memset(del, 0, sizeof(del));
  vector<int> res[2];
  bool has_ok[2] = {false, false};
  vector<vector<int> > G[2];
  G[0] = G[1] = vector<vector<int> >(2 * N[0], vector<int>(2 * N[0], 0));
  for (int i = 0; i < N[2]; i++) {
    int tp_inp[2];
    for (int j = 0; j < 2; j++)
      tp_inp[j] = (tp[0][inp[1][i][j]] == "or" || tp[0][inp[1][i][j]] == "and");
    vector<int> inputs(N[0], 0);
    for (int j = 0; j < 2; j++)
      for (int k = 0; k < 2; k++) inputs[inp[0][inp[1][i][j]][k]]++;
    int num_inputs = 0;
    for (int j = 0; j < N[0]; j++)
      if (inputs[j]) num_inputs++;
    if (tp_inp[0] == tp_inp[1] || num_inputs == 4) {
      del[i] = true;
      continue;
    }
    int group = (tp[1][i] == "nor" || tp[1][i] == "and");
    string tp_cur[2] = {tp[0][inp[1][i][0]], tp[0][inp[1][i][1]]};
    if (tp_cur[0] == "n" + tp_cur[1] || tp_cur[1] == "n" + tp_cur[0]) {
      if (num_inputs == 3) {
        del[i] = true;
        continue;
      }
      has_ok[group] = true;
      res[group].push_back(i);
      continue;
    }
    if (tp[1][i][0] == 'n') {
      if (tp_cur[0] == "and" || tp_cur[1] == "and") {
        del[i] = true;
        continue;
      }
    } else {
      if (tp_cur[0] == "or" || tp_cur[1] == "or") {
        del[i] = true;
        continue;
      }
    }
    res[group].push_back(i);
    if (num_inputs == 2) {
      for (int j = 0; j < N[0]; j++)
        if (inputs[j]) {
          for (int k = j + 1; k < N[0]; k++)
            if (inputs[k])
              G[group][2 * j][2 * k + 1] = G[group][2 * k][2 * j + 1] =
                  G[group][2 * j + 1][2 * k] = G[group][2 * k + 1][2 * j] = 1;
          break;
        }
      continue;
    }
    for (int j = 0; j < N[0]; j++)
      if (inputs[j] == 2)
        for (int k = 0; k < N[0]; k++)
          if (inputs[k] == 1) {
            for (int l = k + 1; l < N[0]; l++)
              if (inputs[l] == 1) {
                int id_gor = (tp_cur[0] == "or" || tp_cur[0] == "nor")
                                 ? inp[1][i][0]
                                 : inp[1][i][1];
                int inp1 = inp[0][id_gor][0] + inp[0][id_gor][1] - j;
                int inp0 = k + l - inp1;
                G[group][2 * inp1][2 * inp0] =
                    G[group][2 * inp0 + 1][2 * inp1 + 1] = 1;
                G[group][2 * inp1][2 * j + 1] = G[group][2 * j][2 * inp1 + 1] =
                    1;
                G[group][2 * inp0 + 1][2 * inp1 + 1] =
                    G[group][2 * inp1][2 * inp0] = 1;
                G[group][2 * inp0 + 1][2 * j] = G[group][2 * j + 1][2 * inp0] =
                    1;
              }
            break;
          }
  }
  int ans = 0;
  for (int k = 0; k < 2; k++) {
    if (has_ok[k]) {
      ans = max(ans, (int)res[k].size());
      continue;
    }
    bool fails = true;
    for (int i = 0; i < N[0]; i++) {
      bool reach[2] = {false, false};
      for (int b = 0; b < 2; b++) {
        queue<int> q;
        vector<int> vis(2 * N[0], 0);
        vis[2 * i + b] = 1;
        q.push(2 * i + b);
        while (!q.empty()) {
          for (int j = 0; j < 2 * N[0]; j++)
            if (G[k][q.front()][j] && vis[j] == 0) {
              vis[j] = 1;
              q.push(j);
            }
          q.pop();
        }
        if (vis[2 * i]) reach[b] = true;
      }
      if (reach[0] && reach[1]) fails = false;
    }
    if (!fails) ans = max(ans, (int)res[k].size());
  }
  cout << ((ans == 0) ? -1 : (N[2] - ans)) << "\n";
  return 0;
}