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.*;
  
  
  public class A {
      static FastWriter out;
  
      private static class GraphAdjacencyList {
          public int SIZE;
          public final List<List<Integer>> graph;
  
  
          public GraphAdjacencyList(final int VERTICES) {
              SIZE = VERTICES;
              graph = new ArrayList<>(SIZE);
              for (int i = 0; i < SIZE; i++) {
                  graph.add(i, new ArrayList<>());
              }
          }
  
          public int newVertex() {
              SIZE++;
              graph.add(new ArrayList<>());
              return SIZE - 1;
          }
  
          public void addTwoWayEdge(int xx, int yy) {
              this.graph.get(xx).add(yy);
              this.graph.get(yy).add(xx);
          }
      }
  
      private static FastScanner scanner;
      static int t;
      static int ii = 0, nn;
      static int[] color, count;
      static GraphAdjacencyList gg;
      static boolean ok;
  
      public static class FastWriter {
          private static final int BUF_SIZE = 1 << 13;
          private final byte[] buf = new byte[BUF_SIZE];
          private final OutputStream out;
          private int ptr = 0;
  
          private FastWriter() {
              out = null;
          }
  
          public FastWriter(OutputStream os) {
              this.out = os;
          }
  
          public FastWriter(String path) {
              try {
                  this.out = new FileOutputStream(path);
              } catch (FileNotFoundException e) {
                  throw new RuntimeException("FastWriter");
              }
          }
  
          public FastWriter write(byte b) {
              buf[ptr++] = b;
              if (ptr == BUF_SIZE) innerflush();
              return this;
          }
  
          public FastWriter write(char c) {
              return write((byte) c);
          }
  
          public FastWriter write(char[] s) {
              for (char c : s) {
                  buf[ptr++] = (byte) c;
                  if (ptr == BUF_SIZE) innerflush();
              }
              return this;
          }
  
          public FastWriter write(String s) {
              s.chars().forEach(c -> {
                  buf[ptr++] = (byte) c;
                  if (ptr == BUF_SIZE) innerflush();
              });
              return this;
          }
  
          private static int countDigits(int l) {
              if (l >= 1000000000) return 10;
              if (l >= 100000000) return 9;
              if (l >= 10000000) return 8;
              if (l >= 1000000) return 7;
              if (l >= 100000) return 6;
              if (l >= 10000) return 5;
              if (l >= 1000) return 4;
              if (l >= 100) return 3;
              if (l >= 10) return 2;
              return 1;
          }
  
          public FastWriter write(int x) {
              if (x == Integer.MIN_VALUE) {
                  return write((long) x);
              }
              if (ptr + 12 >= BUF_SIZE) innerflush();
              if (x < 0) {
                  write((byte) '-');
                  x = -x;
              }
              int d = countDigits(x);
              for (int i = ptr + d - 1; i >= ptr; i--) {
                  buf[i] = (byte) ('0' + x % 10);
                  x /= 10;
              }
              ptr += d;
              return this;
          }
  
          private static int countDigits(long l) {
              if (l >= 1000000000000000000L) return 19;
              if (l >= 100000000000000000L) return 18;
              if (l >= 10000000000000000L) return 17;
              if (l >= 1000000000000000L) return 16;
              if (l >= 100000000000000L) return 15;
              if (l >= 10000000000000L) return 14;
              if (l >= 1000000000000L) return 13;
              if (l >= 100000000000L) return 12;
              if (l >= 10000000000L) return 11;
              if (l >= 1000000000L) return 10;
              if (l >= 100000000L) return 9;
              if (l >= 10000000L) return 8;
              if (l >= 1000000L) return 7;
              if (l >= 100000L) return 6;
              if (l >= 10000L) return 5;
              if (l >= 1000L) return 4;
              if (l >= 100L) return 3;
              if (l >= 10L) return 2;
              return 1;
          }
  
          public FastWriter write(long x) {
              if (x == Long.MIN_VALUE) {
                  return write("" + x);
              }
              if (ptr + 21 >= BUF_SIZE) innerflush();
              if (x < 0) {
                  write((byte) '-');
                  x = -x;
              }
              int d = countDigits(x);
              for (int i = ptr + d - 1; i >= ptr; i--) {
                  buf[i] = (byte) ('0' + x % 10);
                  x /= 10;
              }
              ptr += d;
              return this;
          }
  
          public FastWriter write(double x, int precision) {
              if (x < 0) {
                  write('-');
                  x = -x;
              }
              x += Math.pow(10, -precision) / 2;
              //		if(x < 0){ x = 0; }
              write((long) x).write(".");
              x -= (long) x;
              for (int i = 0; i < precision; i++) {
                  x *= 10;
                  write((char) ('0' + (int) x));
                  x -= (int) x;
              }
              return this;
          }
  
          public FastWriter writeln(char c) {
              return write(c).writeln();
          }
  
          public FastWriter writeln(int x) {
              return write(x).writeln();
          }
  
          public FastWriter writeln(long x) {
              return write(x).writeln();
          }
  
          public FastWriter writeln(double x, int precision) {
              return write(x, precision).writeln();
          }
  
          public FastWriter write(int... xs) {
              boolean first = true;
              for (int x : xs) {
                  if (!first) write(' ');
                  first = false;
                  write(x);
              }
              return this;
          }
  
          public FastWriter write(long... xs) {
              boolean first = true;
              for (long x : xs) {
                  if (!first) write(' ');
                  first = false;
                  write(x);
              }
              return this;
          }
  
          public FastWriter writeln() {
              return write((byte) '\n');
          }
  
          public FastWriter writeln(int... xs) {
              return write(xs).writeln();
          }
  
          public FastWriter writeln(long... xs) {
              return write(xs).writeln();
          }
  
          public FastWriter writeln(char[] line) {
              return write(line).writeln();
          }
  
          public FastWriter writeln(char[]... map) {
              for (char[] line : map) write(line).writeln();
              return this;
          }
  
          public FastWriter writeln(String s) {
              return write(s).writeln();
          }
  
          private void innerflush() {
              try {
                  out.write(buf, 0, ptr);
                  ptr = 0;
              } catch (IOException e) {
                  throw new RuntimeException("innerflush");
              }
          }
  
          public void flush() {
              innerflush();
              try {
                  out.flush();
              } catch (IOException e) {
                  throw new RuntimeException("flush");
              }
          }
  
          public FastWriter print(byte b) {
              return write(b);
          }
  
          public FastWriter print(char c) {
              return write(c);
          }
  
          public FastWriter print(char[] s) {
              return write(s);
          }
  
          public FastWriter print(String s) {
              return write(s);
          }
  
          public FastWriter print(int x) {
              return write(x);
          }
  
          public FastWriter print(long x) {
              return write(x);
          }
  
          public FastWriter print(double x, int precision) {
              return write(x, precision);
          }
  
          public FastWriter println(char c) {
              return writeln(c);
          }
  
          public FastWriter println(int x) {
              return writeln(x);
          }
  
          public FastWriter println(long x) {
              return writeln(x);
          }
  
          public FastWriter println(double x, int precision) {
              return writeln(x, precision);
          }
  
          public FastWriter print(int... xs) {
              return write(xs);
          }
  
          public FastWriter print(long... xs) {
              return write(xs);
          }
  
          public FastWriter println(int... xs) {
              return writeln(xs);
          }
  
          public FastWriter println(long... xs) {
              return writeln(xs);
          }
  
          public FastWriter println(char[] line) {
              return writeln(line);
          }
  
          public FastWriter println(char[]... map) {
              return writeln(map);
          }
  
          public FastWriter println(String s) {
              return writeln(s);
          }
  
          public FastWriter println() {
              return writeln();
          }
      }
  
      static class FastScanner {
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          StringTokenizer st = new StringTokenizer("");
  
          String next() {
              while (!st.hasMoreTokens())
                  try {
                      st = new StringTokenizer(br.readLine());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              return st.nextToken();
          }
  
          int nextInt() {
              return Integer.parseInt(next());
          }
  
          int[] readArray(int n) {
              int[] a = new int[n];
              for (int i = 0; i < n; i++) a[i] = nextInt();
              return a;
          }
  
          long nextLong() {
              return Long.parseLong(next());
          }
  
          double nextDouble() {
              return Double.parseDouble(next());
          }
  
          long[] readLongArray(int n) {
              long[] a = new long[n];
              for (int i = 0; i < n; i++) a[i] = nextLong();
              return a;
          }
      }
  
      public static void main(String[] a) {
          scanner = new FastScanner();
          out = new FastWriter(System.out);
          t = scanner.nextInt();
  
          while (t-- > 0) {
              ii++;
              solve();
          }
          out.flush();
      }
  
      static final long MOD = 1000_000_007;
  
      private static void solve() {
          int n = scanner.nextInt();
          int m = scanner.nextInt();
          nn = n;
          gg = new GraphAdjacencyList(n + 1);
          for (int i = 0; i < m; i++) {
              int ii = scanner.nextInt();
              int jj = scanner.nextInt();
              String tt = scanner.next();
              if ("crewmate".equals(tt)) {
                  int fakeNode = gg.newVertex();
                  gg.addTwoWayEdge(ii, fakeNode);
                  gg.addTwoWayEdge(fakeNode, jj);
              } else if ("imposter".equals(tt)) {
                  gg.addTwoWayEdge(ii, jj);
              } else {
                  new RuntimeException(tt);
              }
          }
          color = new int[gg.SIZE];
          Arrays.fill(color, -1);
          int ans = 0;
          ok = true;
          count = new int[2];
          for (int i = 1; i < n + 1; i++) {
              if (color[i] != -1) {
                  continue;
              }
              color[i] = 0;
              Arrays.fill(count, 0);
              dfs(i);
              ans += Math.max(count[0], count[1]);
          }
          if (!ok) {
              ans = -1;
          }
  //        System.out.println(String.format("Case #%d: %d", ii, ans));
          out.println(ans);
  
      }
  
      private static void dfs(int vertex) {
          count[color[vertex]] += (vertex <= nn) ? 1 : 0;
          int destiny = 1 ^ color[vertex];
          for (int neighbor : gg.graph.get(vertex)) {
              int cc = color[neighbor];
              if (cc == -1) {
                  color[neighbor] = destiny;
                  dfs(neighbor);
              } else if (cc != destiny) {
                  ok = false;
              }
          }
      }
  }
  