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.ArrayList;
  import java.util.StringTokenizer;
  
  public class TheNumberOfImposters {
  
  
      static int mod = 1000000007;
  
      public static void main(String[] args) throws IOException {
  
          FastReader reader = new FastReader();
          FastWriter writer = new FastWriter();
          int numTests = reader.readSingleInt();
  
          // 1 = imposter, 0 = crew, -1 = indetermined
          outer:
          for(int t = 0; t<numTests; t++){
              int[] nm = reader.readIntArray(2);
              int n = nm[0], m = nm[1];
  
              Person[] people = new Person[n];
  
              for(int i = 0; i<n; i++){
                  people[i] = new Person();
              }
  
              for(int i = 0; i<m; i++){
                  StringTokenizer s = new StringTokenizer(reader.readString());
                  int a = Integer.parseInt(s.nextToken());
                  int b = Integer.parseInt(s.nextToken());
                  String role = s.nextToken();
                  int c = role.equals("imposter") ? 1 : 0;
                  people[a-1].adj.add(people[b-1]);
                  people[a-1].accusations.add(c);
                  people[b-1].adjR.add(people[a-1]);
                  people[b-1].accusationsR.add(c);
              }
  
              //Compute connected components
  
              ArrayList<ArrayList<Person>> components = new ArrayList<>();
  
              for(int i = 0; i<people.length; i++){
                  if(people[i].seenComponent) continue;
                  ArrayList<Person> newComponent = new ArrayList<>();
                  people[i].dfs(newComponent);
                  components.add(newComponent);
              }
  
              int[] maximums = new int[components.size()];
              for(int i = 0; i<components.size(); i++){
                  ArrayList<Person> component = components.get(i);
                  Person start = component.get(0);
                  start.role = 1;
                  boolean possible1 = start.dfs();
                  int count = start.countAndReset();
                  int max = (possible1) ? count : 0;
  
                  start.role = 0;
                  boolean possible2 = start.dfs();
                  count = start.countAndReset();
                  max = (possible2) ? Math.max(max, count) : max;
                  maximums[i] = max;
  
                  if(!possible1 && !possible2){
                      writer.writeSingleInteger(-1);
                      continue outer;
                  }
                  maximums[i] = max;
  
              }
  
              int maxTotal = 0;
              for(int i = 0; i<components.size(); i++){
                  maxTotal += maximums[i];
              }
              writer.writeSingleInteger(maxTotal);
          }
  
      }
  
  
      public static class Person {
  
          int role = 0;
          boolean seenComponent, seen;
          ArrayList<Person> adj = new ArrayList<>();
          ArrayList<Person> adjR = new ArrayList<>();
          ArrayList<Integer> accusations = new ArrayList<>();
          ArrayList<Integer> accusationsR = new ArrayList<>();
  
  
          public void dfs(ArrayList<Person> component){
              if(seenComponent) return;
  
              component.add(this);
              seenComponent = true;
  
              for(int i = 0; i<adj.size(); i++){
                  adj.get(i).dfs(component);
              }
  
              for(int i = 0; i<adjR.size(); i++){
                  adjR.get(i).dfs(component);
              }
          }
  
          public boolean dfs(){
  
              this.seen = true;
  
              for(int i = 0; i<adj.size(); i++){
                  int adjRole = adj.get(i).role;
                  if(!adj.get(i).seen) {
                      if(role == 1){
                          adj.get(i).role = (accusations.get(i) == 1) ? 0 : 1;
                      } else {
                          adj.get(i).role = (accusations.get(i) == 1) ? 1 : 0;
                      }
                  } else {
                      if(role == 1){
                          if(accusations.get(i) == adjRole) return false;
                      } else {
                          if(accusations.get(i) != adjRole) return false;
                      }
                  }
              }
  
              for(int i = 0; i<adjR.size(); i++){
                  int adjRRole = adjR.get(i).role;
                  if(!adjR.get(i).seen) {
                      if(role == 1){
                          adjR.get(i).role = (accusationsR.get(i) == 1) ? 0 : 1;
                      } else {
                          adjR.get(i).role = (accusationsR.get(i) == 1) ? 1 : 0;
                      }
                  } else {
                      if(role == 1){
                          if(accusationsR.get(i) == adjRRole) return false;
                      } else {
                          if(accusationsR.get(i) != adjRRole) return false;
                      }
                  }
              }
  
              for(int i = 0; i<adj.size(); i++){
                  if(adj.get(i).seen) continue;
                  if(!adj.get(i).dfs()) return false;
              }
  
              for(int i = 0; i<adjR.size(); i++){
                  if(adjR.get(i).seen) continue;
                  if(!adjR.get(i).dfs()) return false;
              }
              return true;
          }
  
          public int countAndReset(){
              if(!seen) return 0;
  
              seen = false;
              int count = 0;
              if(role == 1) count = 1;
  
              for(int i = 0; i<adj.size(); i++){
                  count += adj.get(i).countAndReset();
              }
  
              for(int i = 0; i<adjR.size(); i++){
                  count += adjR.get(i).countAndReset();
              }
              return count;
          }
  
      }
  
  
      public static void mergeSort(int[] a, int n) {
          if (n < 2) {
              return;
          }
          int mid = n / 2;
          int[] l = new int[mid];
          int[] r = new int[n - mid];
  
          for (int i = 0; i < mid; i++) {
              l[i] = a[i];
          }
          for (int i = mid; i < n; i++) {
              r[i - mid] = a[i];
          }
          mergeSort(l, mid);
          mergeSort(r, n - mid);
  
          merge(a, l, r, mid, n - mid);
      }
  
  
      public static void merge(int[] a, int[] l, int[] r, int left, int right) {
  
          int i = 0, j = 0, k = 0;
          while (i < left && j < right) {
              if (l[i] <= r[j]) {
                  a[k++] = l[i++];
              }
              else {
                  a[k++] = r[j++];
              }
          }
          while (i < left) {
              a[k++] = l[i++];
          }
          while (j < right) {
              a[k++] = r[j++];
          }
      }
  
  
      public static class FastReader {
  
          BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
          StringTokenizer tokenizer;
  
  
          public int readSingleInt() throws IOException {
              return Integer.parseInt(reader.readLine());
          }
  
          public int[] readIntArray(int numInts) throws IOException {
              int[] nums = new int[numInts];
              tokenizer = new StringTokenizer(reader.readLine());
              for(int i = 0; i<numInts; i++){
                  nums[i] = Integer.parseInt(tokenizer.nextToken());
              }
              return nums;
          }
  
          public String readString() throws IOException {
              return reader.readLine();
          }
  
      }
  
  
  
      public static class FastWriter {
  
          BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
  
  
          public void writeSingleInteger(int i) throws IOException {
              writer.write(Integer.toString(i));
              writer.newLine();
              writer.flush();
          }
  
          public void writeSingleLong(long i) throws IOException {
              writer.write(Long.toString(i));
              writer.newLine();
              writer.flush();
          }
  
          public void writeIntArrayWithSpaces(int[] nums) throws IOException {
              for(int i = 0; i<nums.length; i++){
                  writer.write(nums[i] + " ");
              }
              writer.newLine();
              writer.flush();
          }
  
          public void writeIntArrayListWithSpaces(ArrayList<Integer> nums) throws IOException {
              for(int i = 0; i<nums.size(); i++){
                  writer.write(nums.get(i) + " ");
              }
              writer.newLine();
              writer.flush();
          }
  
          public void writeIntArrayWithoutSpaces(int[] nums) throws IOException {
              for(int i = 0; i<nums.length; i++){
                  writer.write(Integer.toString(nums[i]));
              }
              writer.newLine();
              writer.flush();
          }
  
          public void writeString(String s) throws IOException {
              writer.write(s);
              writer.newLine();
              writer.flush();
          }
  
      }
  
  
  
  }
  