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.util.*;
  import java.io.*;
  public class Main implements Runnable {
  	
  	static ArrayList<Integer>[]graph;
     static HashMap<Integer, Integer>hm,size;
     static boolean[]vis;
     static Boolean[]colored;
     static boolean can;
     public static pair Count(int x,boolean color) {
  	   if(colored[x]!=null) {
  		   if(colored[x]!=color) {
  			   can=false;
  		   }
  		   return new pair(0, 0);
  	   }
  	   colored[x]=color;
  	   pair ret=new pair(0,0);
  	   for(int i:graph[x]) {
  		   pair t=Count(i, !color);
  		   ret.x+=t.x;
  		   ret.y+=t.y;
  	   }
  	   if(color) {
  		   ret.x+=size.get(x);
  	   }else {
  		   ret.y+=size.get(x);
  	   }
  	   vis[x]=true;
  	   return ret;
     }
     
  	public static void main(String[] args) throws Exception {
  		 new Thread(null, new Main(), "Main", 1<<26).start();
  		
  	}
  	public void run(){
  		int t;
  		try {
  			t = sc.nextInt();
  		
  		while(t-->0) {
  			can=true;
  			int n=sc.nextInt();
  			int m=sc.nextInt();
  			
  			UnionFind u=new UnionFind(n);
  			Queue<pair>q=new LinkedList<Main.pair>();
  			for(int i=0;i<m;i++) {
  				int x=sc.nextInt()-1;
  				int y=sc.nextInt()-1;
  				char c=sc.next().charAt(0);
  				
  				if(c=='c') {
  					u.unionSet(x, y);
  				}else {
  					q.add(new pair(x, y));
  				}
  			}
  			hm=new HashMap<Integer, Integer>();
  			size=new HashMap<Integer, Integer>();
  			for(int i=0;i<n;i++) {
  				if(!hm.containsKey(u.findSet(i))) {
  					size.put(hm.size(), u.sizeOfSet(i));
  					hm.put(u.findSet(i), hm.size());
  				}
  			}
  			int N=hm.size();
  			vis=new boolean[N];
  			colored=new Boolean[N];
  			graph=new ArrayList[N];
  			for(int i=0;i<N;i++) {
  				graph[i]=new ArrayList<Integer>();
  			}
  //			System.out.println(Arrays.toString(u.p));
  			while(!q.isEmpty()) {
  				
  				int x=hm.get( u.findSet((int)q.peek().x));
  				int y=hm.get(u.findSet((int)q.poll().y));
  				graph[x].add(y);
  				graph[y].add(x);
  			}
  			long ans=0;
  			for(int i=0;i<N;i++) {
  				if(!vis[i]) {
  					pair j=Count(i,true);
  //					pw.println("*"+j);
  					ans+=Math.max(j.x,j.y );
  				}
  			}
  			pw.println((can?ans:-1));
  		}
  		pw.close();
  		} catch (IOException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
  	   }
  	public static class UnionFind {
  		int[] p, rank, setSize;
  		int numSets;
  
  		public UnionFind(int N) {
  			p = new int[numSets = N];
  			rank = new int[N];
  			setSize = new int[N];
  			for (int i = 0; i < N; i++) {
  				p[i] = i;
  				setSize[i] = 1;
  			}
  		}
  
  		public int findSet(int i) {
  			return p[i] == i ? i : (p[i] = findSet(p[i]));
  		}
  
  		public boolean isSameSet(int i, int j) {
  			return findSet(i) == findSet(j);
  		}
  
  		public void unionSet(int i, int j) {
  			if (isSameSet(i, j))
  				return;
  			numSets--;
  			int x = findSet(i), y = findSet(j);
  			if (rank[x] > rank[y]) {
  				p[y] = x;
  				setSize[x] += setSize[y];
  			} else {
  				p[x] = y;
  				setSize[y] += setSize[x];
  				if (rank[x] == rank[y])
  					rank[y]++;
  			}
  		}
  
  		public int numDisjointSets() {
  			return numSets;
  		}
  
  		public int sizeOfSet(int i) {
  			return setSize[findSet(i)];
  		}
  	}
  
  	static class Scanner {
  		StringTokenizer st;
  		BufferedReader br;
  
  		public Scanner(InputStream s) {
  			br = new BufferedReader(new InputStreamReader(s));
  		}
  
  		public Scanner(FileReader r) {
  			br = new BufferedReader(r);
  		}
  
  		public String next() throws IOException {
  			while (st == null || !st.hasMoreTokens())
  				st = new StringTokenizer(br.readLine());
  			return st.nextToken();
  		}
  
  		public int nextInt() throws IOException {
  			return Integer.parseInt(next());
  		}
  
  		public long nextLong() throws IOException {
  			return Long.parseLong(next());
  		}
  
  		public String nextLine() throws IOException {
  			return br.readLine();
  		}
  
  		public double nextDouble() throws IOException {
  			String x = next();
  			StringBuilder sb = new StringBuilder("0");
  			double res = 0, f = 1;
  			boolean dec = false, neg = false;
  			int start = 0;
  			if (x.charAt(0) == '-') {
  				neg = true;
  				start++;
  			}
  			for (int i = start; i < x.length(); i++)
  				if (x.charAt(i) == '.') {
  					res = Long.parseLong(sb.toString());
  					sb = new StringBuilder("0");
  					dec = true;
  				} else {
  					sb.append(x.charAt(i));
  					if (dec)
  						f *= 10;
  				}
  			res += Long.parseLong(sb.toString()) / f;
  			return res * (neg ? -1 : 1);
  		}
  
  		public long[] nextlongArray(int n) throws IOException {
  			long[] a = new long[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextLong();
  			return a;
  		}
  
  		public Long[] nextLongArray(int n) throws IOException {
  			Long[] a = new Long[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextLong();
  			return a;
  		}
  
  		public int[] nextIntArray(int n) throws IOException {
  			int[] a = new int[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextInt();
  			return a;
  		}
  
  		public Integer[] nextIntegerArray(int n) throws IOException {
  			Integer[] a = new Integer[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextInt();
  			return a;
  		}
  
  		public boolean ready() throws IOException {
  			return br.ready();
  		}
  
  	}
  
  	static class pair implements Comparable<pair> {
  		long x;
  		long y;
  
  		public pair(long x, long y) {
  			this.x = x;
  			this.y = y;
  		}
  
  		public String toString() {
  			return x + " " + y;
  		}
  
  		public boolean equals(Object o) {
  			if (o instanceof pair) {
  				pair p = (pair) o;
  				return p.x == x && p.y == y;
  			}
  			return false;
  		}
  
  		public int hashCode() {
  			return new Long(x).hashCode() * 31 + new Long(y).hashCode();
  		}
  
  		public int compareTo(pair other) {
  			if (this.x == other.x) {
  				return Long.compare(this.y, other.y);
  			}
  			return Long.compare(this.x, other.x);
  		}
  	}
  
  	static class tuble implements Comparable<tuble> {
  		int x;
  		int y;
  		int z;
  
  		public tuble(int x, int y, int z) {
  			this.x = x;
  			this.y = y;
  			this.z = z;
  		}
  
  		public String toString() {
  			return x + " " + y + " " + z;
  		}
  
  		public int compareTo(tuble other) {
  			if (this.x == other.x) {
  				if (this.y == other.y) {
  					return this.z - other.z;
  				}
  				return this.y - other.y;
  			} else {
  				return this.x - other.x;
  			}
  		}
  	}
  
  	static long mod = 1000000007;
  	static Random rn = new Random();
  	static Scanner sc = new Scanner(System.in);
  	static PrintWriter pw = new PrintWriter(System.out);
  }