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.*;
  //solve harder problems
  //upsolve
  //use gitgud
  //learn from everything
  //USACO
  public class Contest1594D
  {
  	static class InputReader {
  		BufferedReader reader;
  		StringTokenizer tokenizer;
  		public InputReader(InputStream stream) {
  			reader = new BufferedReader(new InputStreamReader(stream), 32768);
  			tokenizer = null;
  		}
  		String next() { // reads in the next string
  			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  				try {
  					tokenizer = new StringTokenizer(reader.readLine());
  				} catch (IOException e) {
  					throw new RuntimeException(e);
  				}
  			}
  			return tokenizer.nextToken();
  		}
  		public int nextInt() { // reads in the next int
  			return Integer.parseInt(next());
  		}
  		public long nextLong() { // reads in the next long
  			return Long.parseLong(next());
  		}
  		public double nextDouble() { // reads in the next double
  			return Double.parseDouble(next());
  			}
  		}
  	static InputReader r = new InputReader(System.in);
  	static PrintWriter pw = new PrintWriter(System.out);
  	static ArrayList<Integer>[] adj,adj1,back; static boolean[] visit; static int[] hi; static boolean flag; static int count,size;
  	public static void main(String[] args)
  	{
  		//LOOK FOR INT OVERFLOW
  		//LOOK FOR SCOPE ERROR
  		//Add negatives
  		//SORT BOTH ARRAYS
  		//CHECK THE LAST ONE THAT IS OUT LOOP
  		//SORT BY BOTH VALUES
  		//DON'T ADD STRINGS,USE STRING BUILDER
  		int t = r.nextInt();
  		while (t > 0)
  		{
  			t--;
  			int n = r.nextInt();
  			int m = r.nextInt();
  			adj = new ArrayList[n];
  			adj1 = new ArrayList[n];
  			visit = new boolean[n];
  			hi = new int[n];
  			flag = false;
  			count = 0;
  			size = 0;
  			for (int i = 0; i < n; i ++)
  			{
  				adj[i] = new ArrayList<Integer>();
  				adj1[i] = new ArrayList<Integer>();
  			}
  			for (int i = 0; i < m; i ++)
  			{
  				int x = r.nextInt()-1;
  				int y = r.nextInt() -1;
  				String s = r.next();
  				if (s.charAt(0) == 'i')
  				{
  					adj1[x].add(1);
  					adj1[y].add(1);
  				}
  				else
  				{
  					adj1[x].add(0);
  					adj1[y].add(0);
  				}
  				adj[x].add(y);
  				adj[y].add(x);
  			}
  			int ans = 0;
  			int max1 = -5;
  			int max2 = -5;
  			for (int i = 0; i < n; i ++)
  			{
  				size = 0;
  				count = 0;
  				if (!visit[i])
  				{
  					dfs(i);
  					if (flag)
  					{
  						ans = -1;
  						break;
  					}
  					else
  					{
  						ans += Math.max(count,size-count);
  					}
  				}
  			}
  			pw.println(ans);
  		}
  		
  		pw.close();
  	}
  	static void dfs(int no)
  	{
  		visit[no] = true;
  		size++;
  		int state = hi[no];
  		for (int i = 0; i < adj[no].size(); i ++)
  		{
  			int next = adj[no].get(i);
  			int to = adj1[no].get(i);
  			if (visit[next])
  			{
  				if (hi[next] != (state^to))
  				{
  					flag = true;
  					break;
  				}
  			}
  			else
  			{
  				hi[next] = state^to;
  				count += hi[next];
  				dfs(next);
  			}
  		}
  	}
  }