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 amongUs{
      //--------------------------INPUT READER--------------------------------//
      static class fs {
          public BufferedReader br;
          StringTokenizer st = new StringTokenizer("");
  
          public fs() { this(System.in); }
          public fs(InputStream is) {
              br = new BufferedReader(new InputStreamReader(is));
          }
          String next() {
              while (!st.hasMoreTokens()) {
                  try { st = new StringTokenizer(br.readLine()); }
                  catch (IOException e) { e.printStackTrace(); }
              }
              return st.nextToken();
          }
  
          int ni() { return Integer.parseInt(next()); }
          long nl() { return Long.parseLong(next()); }
          double nd() { return Double.parseDouble(next()); }
          String ns() { return next(); }
  
          int[] na(int n) {
              int[] a = new int[n];
              for (int i = 0; i < n; i++) a[i] = ni();
              return a;
          }
  
          long[] nal(int n) {
              long[] l = new long[n];
              for(int i = 0; i < n; i++) l[i] = nl();
              return l;
          }
      }
      //-----------------------------------------------------------------------//
  
      //---------------------------PRINTER-------------------------------------//
      static class Printer {
          static PrintWriter w;
          public Printer() {this(System.out);}
          public Printer(OutputStream os) {
              w = new PrintWriter(os);
          }
          public void p(int i) {w.println(i);};
          public void p(long l) {w.println(l);};
          public void p(double d) {w.println(d);};
          public void p(String s) { w.println(s);};
          public void pr(int i) {w.println(i);};
          public void pr(long l) {w.print(l);};
          public void pr(double d) {w.print(d);};
          public void pr(String s) { w.print(s);};
          public void pl() {w.println();};
          public void close() {w.close();};
      }
      //------------------------------------------------------------------------//
  
      //--------------------------VARIABLES------------------------------------//
      static fs sc = new fs();
      static OutputStream outputStream = System.out;
      static Printer w = new Printer(outputStream);
      //-----------------------------------------------------------------------//
  
      //--------------------------ADMIN_MODE-----------------------------------//
      private static void ADMIN_MODE() throws IOException {
          if (System.getProperty("ONLINE_JUDGE") == null) {
              w = new Printer(new FileOutputStream("output.txt"));
              sc = new fs(new FileInputStream("input.txt"));
          }
      }
      //-----------------------------------------------------------------------//
  
      //----------------------------START--------------------------------------//
      public static void main(String[] args)
              throws IOException {
  
          ADMIN_MODE();
  
          int t = sc.ni();while(t-->0)
              solve();
  
  
          w.close();
      }
  
      static List<List<cl>> graph;
      static int imp = 0;
      static boolean[] vis;
      static boolean[] crews;
      static boolean f = false;
  
      static void solve() throws IOException {
          f = false;
          int n = sc.ni(), m = sc.ni();
          graph = new ArrayList<>();
          for(int i = 0; i <= n; i++) {
              graph.add(new ArrayList<>());
          }
          for(int i = 0; i < m; i++) {
              int from = sc.ni(), to = sc.ni();
              boolean same = sc.ns().equals("crewmate");
              graph.get(from).add(new cl(to, same));
              graph.get(to).add(new cl(from, same));
          }
  
          List<Integer> maxs = new ArrayList<>();
          List<Integer> maxs2 = new ArrayList<>();
  
          vis = new boolean[n+1];
          crews = new boolean[n+1];
          imp = 0;
          for(int i = 1; i <= n; i++) {
              if(!vis[i]) {
                  dfs(i, true);
                  maxs.add(imp);
                  imp = 0;
              }
              if(f) {
                  w.p(-1);
                  return;
              }
          }
  
          vis = new boolean[n+1];
          crews = new boolean[n+1];
          imp = 0;
          for(int i = 1; i <= n; i++) {
              if(!vis[i]) {
                  dfs(i, false);
                  imp++;
                  maxs2.add(imp);
                  imp = 0;
              }
  
              if(f) {
                  w.p(-1);
                  return;
              }
          }
  
          long ans = 0;
          for(int i = 0; i < maxs.size(); i++) {
              ans += Math.max(maxs.get(i), maxs2.get(i));
          }
  
          w.p(ans);
      }
  
      static void dfs(int at, boolean crew) {
          List<cl> to = graph.get(at);
          crews[at] = crew;
          vis[at] = true;
          for(cl cl: to) {
              if(!vis[cl.val]) {
                  if (cl.same) {
                      if(!crew) imp++;
                      dfs(cl.val, crew);
                  } else {
                      if(crew) imp++;
                      dfs(cl.val, !crew);
                  }
              } else {
                  if (cl.same) {
                      if(crew != crews[cl.val]) f = true;
                  } else {
                      if(crew == crews[cl.val]) f = true;
                  }
              }
          }
      }
  
      static class cl {
          int val;
          boolean same;
  
          public cl(int val, boolean same) {
              this.val = val;
              this.same = same;
          }
      }
  }