description: Theofanis started playing the new online game called "Among them". However, he always plays with Cypriot players, and they all have the same name: "Andreas" (the most common name in Cyprus).
  
  In each game, Theofanis plays with n other players. Since they all have the same name, they are numbered from 1 to n.
  
  The players write m comments in the chat. A comment has the structure of "i j c" where i and j are two distinct integers and c is a string (1 ≤ i, j ≤ n; i ≠ j; c is either imposter or crewmate). The comment means that player i said that player j has the role c.
  
  An imposter always lies, and a crewmate always tells the truth. 
  
  Help Theofanis find the maximum possible number of imposters among all the other Cypriot players, or determine that the comments contradict each other (see the notes for further explanation).
  
  Note that each player has exactly one role: either imposter or crewmate.
  
  Input
  
  The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of each test case follows.
  
  The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5; 0 ≤ m ≤ 5 ⋅ 10^5) — the number of players except Theofanis and the number of comments.
  
  Each of the next m lines contains a comment made by the players of the structure "i j c" where i and j are two distinct integers and c is a string (1 ≤ i, j ≤ n; i ≠ j; c is either imposter or crewmate).
  
  There can be multiple comments for the same pair of (i, j).
  
  It is guaranteed that the sum of all n does not exceed 2 ⋅ 10^5 and the sum of all m does not exceed 5 ⋅ 10^5.
  
  Output
  
  For each test case, print one integer — the maximum possible number of imposters. If the comments contradict each other, print -1.
  
  Example
  
  Input
  
  
  5
  3 2
  1 2 imposter
  2 3 crewmate
  5 4
  1 3 crewmate
  2 5 crewmate
  2 4 imposter
  3 4 imposter
  2 2
  1 2 imposter
  2 1 crewmate
  3 5
  1 2 imposter
  1 2 imposter
  3 2 crewmate
  3 2 crewmate
  1 3 imposter
  5 0
  
  
  Output
  
  
  2
  4
  -1
  2
  5
  
  Note
  
  In the first test case, imposters can be Andreas 2 and 3.
  
  In the second test case, imposters can be Andreas 1, 2, 3 and 5.
  
  In the third test case, comments contradict each other. This is because player 1 says that player 2 is an imposter, and player 2 says that player 1 is a crewmate. If player 1 is a crewmate, then he must be telling the truth, so player 2 must be an imposter. But if player 2 is an imposter then he must be lying, so player 1 can't be a crewmate. Contradiction.
solution: import java.io.*;
  import java.util.*;
  
  import static java.lang.Math.max;
  import static java.lang.Math.min;
  
  public class Main {
  
  
      static int[] readArray(int size, InputReader in, boolean subOne) {
          int[] a = new int[size];
          for (int i = 0; i < size; i++) {
              a[i] = in.nextInt() + (subOne ? -1 : 0);
          }
          return a;
      }
  
      static long[] readLongArray(int size, InputReader in) {
          long[] a = new long[size];
          for (int i = 0; i < size; i++) {
              a[i] = in.nextLong();
          }
          return a;
      }
  
      static void sortArray(long[] a) {
          Random random = new Random();
  
          for (int i = 0; i < a.length; i++) {
              int randomPos = random.nextInt(a.length);
              long t = a[i];
              a[i] = a[randomPos];
              a[randomPos] = t;
          }
          Arrays.sort(a);
      }
  
      static int[] sieveOfEuler(int size, List<Integer> primes) {
          int[] lp = new int[size];
  
          lp[1] = 1;
  
          for (int j = 2; j < lp.length; ++j) {
              if (lp[j] == 0) {
                  lp[j] = j;
                  primes.add(j);
              }
              for (Integer p : primes) {
                  if (p > lp[j] || p * j >= size) {
                      break;
                  }
                  lp[p * j] = p;
              }
          }
          return lp;
      }
  
  
      public static void main(String[] args) throws FileNotFoundException {
  //        long startTime = System.currentTimeMillis();
  
  //        PrintWriter out = new PrintWriter(new BufferedOutputStream(new FileOutputStream("cowrect.out")));
          InputReader in = new InputReader(System.in);
  //        InputReader in = new InputReader(new FileInputStream("input.txt"));
          PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
          int t = in.nextInt();
  
  //        ArrayList<Integer> primes = new ArrayList<>();
  //        int[] sieve = sieveOfEuler(3 * 100005, primes);
  
          outer:
          for (int ii = 0; ii < t; ii++) {
              int n = in.nextInt();
              int m = in.nextInt();
  
              Map<Integer, Integer>[] graph = new HashMap[n];
              for (int i = 0; i < n; i++) {
                  graph[i] = new HashMap<>();
              }
              boolean failed = false;
              for (int i = 0; i < m; i++) {
                  int u = in.nextInt() - 1;
                  int v = in.nextInt() - 1;
                  String s = in.nextWord();
                  if (s.equals("imposter")) {
                      if (graph[u].containsKey(v) && graph[u].get(v) == 0) {
                          failed = true;
                      }
                      graph[u].put(v, 1);
                      graph[v].put(u, 1);
                  } else {
                      if (graph[u].containsKey(v) && graph[u].get(v) == 1) {
                          failed = true;
                      }
                      graph[u].put(v, 0);
                      graph[v].put(u, 0);
                  }
  
              }
  
              if (failed) {
                  out.println(-1);
                  continue;
              }
  
              boolean[] visited = new boolean[n];
              int[] colors = new int[n];
              int ans = 0;
              for (int i = 0; i < n; i++) {
                  if (!visited[i]) {
                      int[] result = new int[2];
                      if (dfs(i, graph, visited, result, 0, colors)) {
                          ans += max(result[0], result[1]);
                      } else {
                          failed = true;
                          break;
                      }
                  }
              }
              if (failed) {
                  out.println(-1);
              }else {
                  out.println(ans);
              }
  
          }
  
  
          out.close();
      }
  
      private static boolean dfs(int v, Map<Integer, Integer>[] graph, boolean[] visited, int[] result, int color, int[] colors) {
          visited[v] = true;
          colors[v] = color;
          result[color] += 1;
          for (int u : graph[v].keySet()) {
              if (visited[u]) {
                  if (graph[v].get(u) == 0 && colors[u] != color) {
                      return false;
                  } else if (graph[v].get(u) == 1 && colors[u] != 1 - color) {
                      return false;
                  }
              } else {
                  if (graph[v].get(u) == 0) {
                      if (!dfs(u, graph, visited, result, color, colors)) {
                          return false;
                      }
                  } else if (graph[v].get(u) == 1) {
                      if (!dfs(u, graph, visited, result, 1 - color, colors)) {
                          return false;
                      }
                  }
              }
          }
          return true;
      }
  
      private static void outputArray(Iterable<Integer> ans, PrintWriter out, boolean addOne) {
          StringBuilder str = new StringBuilder();
          for (long an : ans) {
              str.append(addOne ? (an + 1) : an).append(" ");
          }
          out.println(str);
  //        out.flush();
      }
  
      private static class InputReader {
          public BufferedReader reader;
          public StringTokenizer tokenizer;
  
          public InputReader(InputStream stream) {
              reader = new BufferedReader(new InputStreamReader(stream), 32768);
              tokenizer = null;
          }
  
          public String next() {
              while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                  try {
                      tokenizer = new StringTokenizer(reader.readLine());
                  } catch (IOException e) {
                      throw new RuntimeException(e);
                  }
              }
              return tokenizer.nextToken();
          }
  
          public String nextString() {
              try {
                  return reader.readLine();
              } catch (IOException e) {
                  throw new RuntimeException(e);
              }
          }
  
          public int nextInt() {
              return Integer.parseInt(next());
          }
  
          public long nextLong() {
              return Long.parseLong(next());
          }
  
          public double nextDouble() {
              return Double.parseDouble(next());
          }
  
          public char nextChar() {
              return next().charAt(0);
          }
  
          public String nextWord() {
              return next();
          }
  
          private List<Integer>[] readTree(int n) {
              return readGraph(n, n - 1);
          }
  
          private List<Integer>[] readGraph(int n, int m) {
              List<Integer>[] result = new List[n];
              for (int i = 0; i < n; i++) {
                  result[i] = new ArrayList<>();
              }
              for (int i = 0; i < m; i++) {
                  int u = nextInt() - 1;
                  int v = nextInt() - 1;
                  result[u].add(v);
                  result[v].add(u);
              }
              return result;
          }
  
          private Map<Integer, Long>[] readWeightedGraph(int n, int m) {
              Map<Integer, Long>[] result = new HashMap[n];
              for (int i = 0; i < n; i++) {
                  result[i] = new HashMap<>();
              }
              for (int i = 0; i < m; i++) {
                  int u = nextInt() - 1;
                  int v = nextInt() - 1;
                  long w = nextLong();
                  result[u].put(v, min(w, result[u].getOrDefault(v, Long.MAX_VALUE)));
                  result[v].put(u, min(w, result[v].getOrDefault(u, Long.MAX_VALUE)));
              }
              return result;
          }
  
      }
  }