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.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStreamWriter;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.LinkedList;
  import java.util.PriorityQueue;
  import java.util.Map.Entry;
  
  
  import java.util.Random;
  import java.util.TreeSet;
  
  public final class CF_747_D2_D2
  {
  
  
  	static boolean verb=true;
  	static void log(Object X){if (verb) System.err.println(X);}
  	static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}}
  	static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}}
  	static void logWln(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");}}
  	static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}}
  	static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}}
  
  	static void logWln(Object X){if (verb) System.err.print(X);}
  	static void info(Object o){	System.out.println(o);}
  	static void output(Object o){outputWln(""+o+"\n");	}
  	static void outputFlush(Object o){try {out.write(""+ o+"\n");out.flush();} catch (Exception e) {}}
  	static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}}
  
  
  	static void logBin(int[] tm) {for (int x:tm) logWln(bin(8,x)+" ");log("");}
  
  	static String bin(int L,int x) {
  		String s=Integer.toBinaryString(x);
  		while (s.length()<L) s="0"+s;
  		return s;
  	}
  
  	// Global vars
  	static BufferedWriter out;
  	static InputReader reader;
  
  
  	static void test() {
  		log("testing");
  
  
  		Random r=new Random();
  		int NTESTS=1000000;
  		//int NMAX=22;
  		int VMAX=10;
  		for (int t=0;t<NTESTS;t++) {
  			//int n=r.nextInt(NMAX)+2;
  			int n=5;
  			int m=r.nextInt(n*(n-1)/2);
  			ArrayList<Integer>[] friends=new ArrayList[n];
  			ArrayList<Integer>[] edge=new ArrayList[n];
  			for (int i=0;i<n;i++) {
  				friends[i]=new ArrayList<Integer>();
  				edge[i]=new ArrayList<Integer>();
  
  			}
  
  			int[] a=new int[m];
  			int[] b=new int[m];
  			int[] c=new int[m];
  			
  			for (int i=0;i<m;i++) {
  				int u=r.nextInt(n);
  				int v=r.nextInt(n);
  				while (v==u)
  					v=r.nextInt(n);
  				int xor=r.nextInt(2);
  				a[i]=u;
  				b[i]=v;
  				c[i]=xor;
  				
  				friends[u].add(v);
  				edge[u].add(xor);
  				friends[v].add(u);
  				edge[v].add(xor);
  
  			}
  			int res=solve(friends,edge);
  			if (res>=0) {
  				for (int i=0;i<m;i++) {
  					int u=a[i];
  					int v=b[i];
  					int xor=c[i];
  					if (xor!=(value[u]^value[v])) {
  						log("Error");
  						
  						log("ans:"+res);
  						for (int e=0;e<n;e++) {
  							logWln(value[e]+" ");
  						}
  						log("");
  						log(u+" "+v+" "+xor);
  						log(u+" "+v+" "+(value[u]^value[v]));
  						log("========");
  						log(1);
  						log(n+" "+m);
  						for (int e=0;e<m;e++) {
  							log((a[e]+1)+" "+(b[e]+1)+" "+names[c[e]]);
  						}
  						return;
  					}
  				}
  			}
  
  		}
  		log("testing done");
  
  	}
  
  	static int IMPOSTER=1;
  	static int CREWMATE=0;
  
  
  	static long mod=1000000007;
  
  	static int NX=200001;
  	
  	static int[] lst=new int[NX];
  	static int[] stack=new int[NX];
  	static int[] value=new int[NX];
  	
  
  	
  	
  	
  	static int solve(ArrayList<Integer>[] friends,ArrayList<Integer>[] edge) {
  		int n=friends.length;
  		Arrays.fill(value,-1);
  		boolean error=false;
  		int ans=0;
  		for (int i=0;i<n && !error;i++) {
  			if (value[i]==-1) {
  				int st=0;
  				stack[st++]=i;
  				value[i]=0;
  				int tem=0;
  				lst[tem++]=i;
  				
  				while (st>0 && !error) {
  					
  					int u=stack[--st];
  					//log("==================processing u:"+u);
  					
  					int L=friends[u].size();
  					for (int j=0;j<L;j++) {
  						int v=friends[u].get(j);
  						int xor=edge[u].get(j);
  						//log("looking at v:"+v);
  						if (value[v]==-1) {
  						
  							value[v]=value[u]^xor;
  							//log("setting v:"+v+" to :"+(value[u]^xor));
  							stack[st++]=v;
  							lst[tem++]=v;
  						} else {
  							if (value[v]==(value[u]^xor)) {
  								// nothing
  							} else {
  								error=true;
  								break;
  							}
  						}
  					}
  				}
  				//done
  				if (!error) {
  					int cnt=0;
  					for (int j=0;j<tem;j++) {
  						int u=lst[j];
  						if (value[u]==1)
  							cnt++;
  					}
  					ans+=Math.max(cnt, tem-cnt);
  				}
  			}
  		}
  		if (error) 
  			return -1;
  		 else {
  			//log("value");
  			//log(value,n);
  			 return ans;
  		 }
  	}
  	
  	static String[] names= {"crew","imposter"};
  	static void process() throws Exception {
  
  
  		out = new BufferedWriter(new OutputStreamWriter(System.out));
  		reader=new InputReader(System.in);
  		
  		//test();
  		
  		int T=reader.readInt();
  		for (int t=0;t<T;t++) {
  			int n=reader.readInt();
  			int m=reader.readInt();
  
  			ArrayList<Integer>[] friends=new ArrayList[n];
  			ArrayList<Integer>[] edge=new ArrayList[n];
  			for (int i=0;i<n;i++) {
  				friends[i]=new ArrayList<Integer>();
  				edge[i]=new ArrayList<Integer>();
  
  			}
  
  			for (int i=0;i<m;i++) {
  				int u=reader.readInt()-1;
  				int v=reader.readInt()-1;
  				String s=reader.readString();
  				int xor=0;
  				if (s.contentEquals("imposter"))
  					xor=1;
  				else
  					xor=0;
  				friends[u].add(v);
  				edge[u].add(xor);
  				friends[v].add(u);
  				edge[v].add(xor);
  		
  
  			}
  			
  			output(solve(friends,edge));
  
  
  
  			// fix one, look at the outcome
  
  		}
  
  		try {
  			out.close();
  		} catch (Exception Ex) {
  		}
  
  	}
  
  
  
  
  
  	public static void main(String[] args) throws Exception {
  		process();
  
  	}
  
  	static final class InputReader {
  		private final InputStream stream;
  		private final byte[] buf = new byte[1024];
  		private int curChar;
  		private int numChars;
  
  		public InputReader(InputStream stream) {
  			this.stream = stream;
  		}
  
  		private int read() throws IOException {
  			if (curChar >= numChars) {
  				curChar = 0;
  				numChars = stream.read(buf);
  				if (numChars <= 0) {
  					return -1;
  				}
  			}
  			return buf[curChar++];
  		}
  
  		public final String readString() throws IOException {
  			int c = read();
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			StringBuilder res = new StringBuilder();
  			do {
  				res.append((char) c);
  				c = read();
  			} while (!isSpaceChar(c));
  			return res.toString();
  		}
  
  		public final String readString(int L) throws IOException {
  			int c = read();
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			StringBuilder res = new StringBuilder(L);
  			do {
  				res.append((char) c);
  				c = read();
  			} while (!isSpaceChar(c));
  			return res.toString();
  		}
  
  		public final int readInt() throws IOException {
  			int c = read();
  			boolean neg = false;
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			char d = (char) c;
  			// log("d:"+d);
  			if (d == '-') {
  				neg = true;
  				c = read();
  			}
  			int res = 0;
  			do {
  				res *= 10;
  				res += c - '0';
  				c = read();
  			} while (!isSpaceChar(c));
  			// log("res:"+res);
  			if (neg)
  				return -res;
  			return res;
  
  		}
  
  		public final long readLong() throws IOException {
  			int c = read();
  			boolean neg = false;
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			char d = (char) c;
  			// log("d:"+d);
  			if (d == '-') {
  				neg = true;
  				c = read();
  			}
  			long res = 0;
  			do {
  				res *= 10;
  				res += c - '0';
  				c = read();
  			} while (!isSpaceChar(c));
  			// log("res:"+res);
  			if (neg)
  				return -res;
  			return res;
  
  		}
  
  		private boolean isSpaceChar(int c) {
  			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  		}
  	}
  
  }
  