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.OutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.PrintWriter;
  import java.io.OutputStream;
  import java.util.Arrays;
  import java.util.Iterator;
  import java.util.Collection;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.FileNotFoundException;
  import java.util.Objects;
  import java.util.StringTokenizer;
  import java.io.Writer;
  import java.io.BufferedReader;
  import java.io.InputStream;
  
  /**
   * Built using CHelper plug-in
   * Actual solution is at the top
   *
   * @author Asgar Javadov
   */
  public class Main {
      public static void main(String[] args) {
          InputStream inputStream = System.in;
          OutputStream outputStream = System.out;
          InputReader in = new InputReader(inputStream);
          OutputWriter out = new OutputWriter(outputStream);
          DTheNumberOfImposters solver = new DTheNumberOfImposters();
          int testCount = Integer.parseInt(in.next());
          for (int i = 1; i <= testCount; i++)
              solver.solve(i, in, out);
          out.close();
      }
  
      static class DTheNumberOfImposters {
          DTheNumberOfImposters.Direction dir;
  
          public void solve(int testNumber, InputReader in, OutputWriter out) {
              int n = in.nextInt();
              int m = in.nextInt();
              var edges = new DTheNumberOfImposters.Edge[m];
              dir = new DTheNumberOfImposters.Direction(n);
              var size = new int[n];
              for (int i = 0; i < m; ++i) {
                  edges[i] = new DTheNumberOfImposters.Edge(in.nextInt() - 1, in.nextInt() - 1, in.next());
                  size[edges[i].from]++;
                  size[edges[i].to]++;
              }
  
              for (int i = 0; i < n; i++) {
                  dir.adj[i] = new DTheNumberOfImposters.Edge[size[i]];
              }
              for (DTheNumberOfImposters.Edge edge : edges) {
                  dir.adj[edge.from][--size[edge.from]] = edge;
                  dir.adj[edge.to][--size[edge.to]] = new DTheNumberOfImposters.Edge(edge.to, edge.from, edge.role);
              }
  
              var result = 0;
              var color = new int[n];
              Arrays.fill(color, -1);
              for (int i = 0; i < n; ++i) {
                  if (color[i] != -1) continue;
  
                  var que = new IntList(n);
                  que.add(i);
                  color[i] = 0;
                  var answer = 0;
                  for (int b = 0; b < que.size(); ++b) {
                      var current = que.get(b);
                      for (var neighbor : dir.adj[current]) {
                          var col = color[current] ^ (neighbor.role.charAt(0) == 'i' ? 1 : 0);
                          if (color[neighbor.to] == -1) {
                              color[neighbor.to] = col;
                              que.add(neighbor.to);
                              answer += color[neighbor.to];
                          } else if (color[neighbor.to] != col) {
                              out.minusOne();
                              return;
                          }
                      }
                  }
                  result += Math.max(answer, que.size() - answer);
              }
              out.println(result);
          }
  
          static class Direction {
              DTheNumberOfImposters.Edge[][] adj;
  
              public Direction(int n) {
                  adj = new DTheNumberOfImposters.Edge[n][];
              }
  
          }
  
          static class Edge {
              int from;
              int to;
              String role;
  
              public Edge(int from, int to, String role) {
                  this.from = from;
                  this.to = to;
                  this.role = role;
              }
  
              public String toString() {
                  return "Edge{" +
                          "from=" + from +
                          ", to=" + to +
                          ", role='" + role + '\'' +
                          '}';
              }
  
          }
  
      }
  
      static class InputReader extends BufferedReader {
          StringTokenizer tokenizer;
  
          public InputReader(InputStream inputStream) {
              super(new InputStreamReader(inputStream), 32768);
          }
  
          public InputReader(String filename) {
              super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
          }
  
          public String next() {
              while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                  try {
                      String line = readLine();
                      if (line == null) continue;
                      tokenizer = new StringTokenizer(line);
                  } catch (IOException e) {
                      throw new RuntimeException();
                  }
              }
              return tokenizer.nextToken();
          }
  
          public Integer nextInt() {
              return Integer.valueOf(next());
          }
  
      }
  
      static class OutputWriter extends PrintWriter {
          public OutputWriter(OutputStream outputStream) {
              super(outputStream);
          }
  
          public OutputWriter(Writer writer) {
              super(writer);
          }
  
          public OutputWriter(String filename) throws FileNotFoundException {
              super(filename);
          }
  
          public void minusOne() {
              println(-1);
          }
  
          public void close() {
              super.close();
          }
  
      }
  
      static class IntList implements Iterable<Integer> {
          public static final int INITIAL_CAPACITY = 4;
          private int size;
          private int[] array;
  
          public IntList() {
              this(INITIAL_CAPACITY);
          }
  
          public IntList(int initialCapacity) {
              this.array = new int[initialCapacity];
              this.size = 0;
          }
  
          public IntList(int[] array) {
              this.array = Arrays.copyOf(array, array.length);
              this.size = this.array.length;
          }
  
          public IntList(Collection<Integer> collection) {
              this.size = collection.size();
              this.array = new int[size];
              int index = 0;
              for (int e : collection) {
                  array[index++] = e;
              }
          }
  
          public void add(int value) {
              if (size == array.length) {
                  ensureCapacity();
              }
  
              this.array[this.size++] = value;
          }
  
          public int get(int index) {
              if (index < 0 || index >= size)
                  throw new IllegalArgumentException("Expected argument in [0, " + size + "), but found " + index);
              return array[index];
          }
  
          public int size() {
              return this.size;
          }
  
          private void ensureCapacity() {
              if (size < array.length)
                  return;
              this.array = Arrays.copyOf(array, array.length << 1);
          }
  
          public int[] toArray() {
              return Arrays.copyOf(this.array, this.size);
          }
  
          public String toString() {
              return Arrays.toString(Arrays.copyOf(array, this.size));
          }
  
          public boolean equals(Object o) {
              if (this == o) return true;
              if (o == null || getClass() != o.getClass()) return false;
              IntList integers = (IntList) o;
              return size == integers.size &&
                      Arrays.equals(integers.array, 0, integers.size, this.array, 0, this.size);
          }
  
          public int hashCode() {
              int result = Objects.hash(size);
              result = 31 * result + Arrays.hashCode(toArray());
              return result;
          }
  
          public IntList clone() {
              IntList cloned = new IntList(Math.max(1, this.size));
              for (int i = 0; i < size; ++i)
                  cloned.add(array[i]);
              return cloned;
          }
  
          public Iterator<Integer> iterator() {
              return new IntListIterator();
          }
  
          private class IntListIterator implements Iterator<Integer> {
              private int current;
  
              public IntListIterator() {
                  this.current = 0;
              }
  
              public boolean hasNext() {
                  return this.current < size;
              }
  
              public Integer next() {
                  return array[current++];
              }
  
          }
  
      }
  }
  
  