description: 'Twas the night before Christmas, and Santa's frantically setting up his new Christmas tree! There are n nodes in the tree, connected by n-1 edges. On each edge of the tree, there's a set of Christmas lights, which can be represented by an integer in binary representation.
  
  <image>
  
  He has m elves come over and admire his tree. Each elf is assigned two nodes, a and b, and that elf looks at all lights on the simple path between the two nodes. After this, the elf's favorite number becomes the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of the values of the lights on the edges in that path.
  
  However, the North Pole has been recovering from a nasty bout of flu. Because of this, Santa forgot some of the configurations of lights he had put on the tree, and he has already left the North Pole! Fortunately, the elves came to the rescue, and each one told Santa what pair of nodes he was assigned (a_i, b_i), as well as the parity of the number of set bits in his favorite number. In other words, he remembers whether the number of 1's when his favorite number is written in binary is odd or even.
  
  Help Santa determine if it's possible that the memories are consistent, and if it is, remember what his tree looked like, and maybe you'll go down in history!
  
  Input
  
  The first line contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t cases follow.
  
  The first line of each test case contains two integers, n and m (2 ≤ n ≤ 2 ⋅ 10^5; 1 ≤ m ≤ 2 ⋅ 10^5) — the size of tree and the number of elves respectively.
  
  The next n-1 lines of each test case each contains three integers, x, y, and v (1 ≤ x, y ≤ n; -1 ≤ v < 2^{30}) — meaning that there's an edge between nodes x and y. If 
  
    * v = -1: Santa doesn't remember what the set of lights were on for this edge. 
    * v ≥ 0: The set of lights on the edge is v. 
  
  
  
  The next m lines of each test case each contains three integers, a, b, and p (1 ≤ a, b ≤ n; a ≠ b; 0 ≤ p ≤ 1) — the nodes that the elf was assigned to, and the parity of the number of set bits in the elf's favorite number.
  
  It is guaranteed that the sum of all n and the sum of all m don't exceed 2 ⋅ 10^5 each.
  
  It is guaranteed that the given edges form a tree.
  
  Output
  
  For each test case, first print either YES or NO (in any case), whether there's a tree consistent with Santa's memory or not. 
  
  If the answer is YES, print n-1 lines each containing three integers: x, y, and v (1 ≤ x, y ≤ n; 0 ≤ v < 2^{30}) — the edge and the integer on that edge. The set of edges must be the same as in the input, and if the value of some edge was specified earlier, it can not change. You can print the edges in any order.
  
  If there are multiple answers, print any.
  
  Example
  
  Input
  
  
  4
  6 5
  1 2 -1
  1 3 1
  4 2 7
  6 3 0
  2 5 -1
  2 3 1
  2 5 0
  5 6 1
  6 1 1
  4 5 1
  5 3
  1 2 -1
  1 3 -1
  1 4 1
  4 5 -1
  2 4 0
  3 4 1
  2 3 1
  3 3
  1 2 -1
  1 3 -1
  1 2 0
  1 3 1
  2 3 0
  2 1
  1 2 1
  1 2 0
  
  
  Output
  
  
  YES
  1 2 0
  1 3 1
  2 4 7
  3 6 0
  2 5 0
  YES
  1 2 1
  1 3 0
  1 4 1
  4 5 1
  NO
  NO
  
  Note
  
  The first test case is the image in the statement.
  
  One possible answer is assigning the value of the edge (1, 2) to 5, and the value of the edge (2, 5) to 3. This is correct because: 
  
    * The first elf goes from node 2 to node 3. This elf's favorite number is 4, so he remembers the value 1 (as 4 has an odd number of 1 bits in its binary representation). 
    * The second elf goes from node 2 to node 5. This elf's favorite number is 3, so he remembers the value 0 (as 3 has an even number of 1 bits in its binary representation). 
    * The third elf goes from node 5 to node 6. This elf's favorite number is 7, so he remembers the value 1 (as 7 has an odd number of 1 bits in its binary representation). 
    * The fourth elf goes from node 6 to node 1. This elf's favorite number is 1, so he remembers the value 1 (as 1 has an odd number of 1 bits in its binary representation). 
    * The fifth elf goes from node 4 to node 5. This elf's favorite number is 4, so he remembers the number 1 (as 4 has an odd number of 1 bits in its binary representation). 
  
  
  
  Note that there are other possible answers.
solution: import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.util.ArrayDeque;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.StringTokenizer;
  
  public class d {
  	static final int LOG=30;
  	public static void main(String[] args) {
  		FastScanner scan=new FastScanner();
  		PrintWriter out=new PrintWriter(System.out);
  
  		/*        
  		we can reassign all edge values to bit count % 2 without changing the problem.
  		and all new values we put in the tree can just be 0 or 1 since we either need
  		to keep or flip the existing parity. after this reduction we are given the xor
  		of various paths in the tree.
  
  		and of course:) we may reduce xor(a,b) to xor(a,root) ^ xor(b,root)
  		where xor(i,j) is xor of elements on the path from i to j
  		once we have a bunch of equations we can determine all the values
  		xor(root,i) for all i
  		from which we can determine the values on the actual edges:)
  		 */
  
  		int t=scan.nextInt();
  		t:for(int tt=0;tt<t;tt++) {
  			int n=scan.nextInt(), m=scan.nextInt();
  			parent=new int[LOG][n];
  			xor=new int[LOG][n];
  			a=new ArrayList[n];
  			int[][] orig=new int[n-1][3];
  
  			for(int i=0;i<n;i++) a[i]=new ArrayList<>();
  			for(int i=0;i<n-1;i++) {
  				int u=scan.nextInt()-1, v=scan.nextInt()-1;
  				int val=scan.nextInt();
  				orig[i][0]=u;
  				orig[i][1]=v;
  				orig[i][2]=val;
  				if(val!=-1) val=Integer.bitCount(val)%2;
  				a[u].add(new edge(v,val));
  				a[v].add(new edge(u,val));
  			}
  			init(0,-1);
  			parent[0][0]=xor[0][0]=-1;
  			for(int j=1;j<LOG;j++) {
  				for(int i=0;i<n;i++) {
  					if(parent[j-1][i]==-1) parent[j][i]=-1;//-1=doesn't exist
  					else parent[j][i]=parent[j-1][parent[j-1][i]];
  
  					if(xor[j-1][i]==-1) xor[j][i]=-1;//-1=unknown
  					else if(parent[j-1][i]==-1||xor[j-1][parent[j-1][i]]==-1) xor[j][i]=-1;
  					else xor[j][i]=xor[j-1][i]^xor[j-1][parent[j-1][i]];
  				}
  			}
  
  			ArrayDeque<Integer> priority=new ArrayDeque<>();
  			r=new ArrayList[n];
  			for(int i=0;i<n;i++) r[i]=new ArrayList<>();
  			rvals=new int[n];
  			for(int i=0;i<n;i++) {
  				rvals[i]=getrval(i);
  				if(rvals[i]!=-1) priority.offer(i);
  			}
  			seen=new boolean[n];
  
  			for(int i=0;i<m;i++) {
  				int u=scan.nextInt()-1, v=scan.nextInt()-1, val=scan.nextInt();
  
  				r[u].add(new edge(v,val));
  				r[v].add(new edge(u,val));
  			}
  			for(int i=0;i<n-1;i++) {
  				if(orig[i][2]==-1) continue;
  				int val=Integer.bitCount(orig[i][2])%2;
  				int u=orig[i][0], v=orig[i][1];
  				r[u].add(new edge(v,val));
  				r[v].add(new edge(u,val));
  			}
  			while(!priority.isEmpty()) {
  				int i=priority.poll();
  				if(!seen[i]) {
  					if(!dfs(i)) {
  						out.println("NO");
  						continue t;
  					}
  				}
  			}
  			for(int i=0;i<n;i++) {
  				if(!seen[i]) {
  					rvals[i]=0;
  					if(!dfs(i)) {
  						out.println("NO");
  						continue t;
  					}
  				}
  			}
  			//			System.out.println(Arrays.toString(rvals));
  			out.println("YES");
  			for(int i=0;i<n-1;i++) {
  				int u=orig[i][0], v=orig[i][1];
  				int val=orig[i][2];
  				if(val==-1) {
  					val=rvals[u]^rvals[v];
  				}
  				out.println((u+1)+" "+(v+1)+" "+val);
  			}
  		}
  		out.close();
  	}
  	public static boolean dfs(int at) {
  		boolean res=true;
  		seen[at]=true;
  		for(edge nxt:r[at]) {
  			int target=rvals[at]^nxt.val;
  			if(rvals[nxt.v]!=-1&&rvals[nxt.v]!=target) return false;
  
  			if(seen[nxt.v]) continue;
  			rvals[nxt.v]=target;
  			res&=dfs(nxt.v);
  		}
  		return res;
  	}
  	public static int getrval(int cur) {
  		int res=0;
  		for(int j=LOG-1;j>=0;j--) {
  			if(parent[j][cur]!=-1) {
  				if(xor[j][cur]==-1) return -1;
  				res^=xor[j][cur];
  				cur=parent[j][cur];
  			}
  		}
  		return res;
  	}
  	static boolean[] seen;
  	static int[] rvals;
  	static ArrayList<edge>[] a,r;
  	static int[][] parent,xor;
  	public static void init(int at, int prev) {
  		for(edge nxt:a[at]) {
  			if(nxt.v==prev) continue;
  			parent[0][nxt.v]=at;
  			xor[0][nxt.v]=nxt.val;
  			init(nxt.v,at);
  		}
  	}
  	static class edge {
  		int v,val;
  		edge(int v, int val) {
  			this.v=v;
  			this.val=val;
  		}
  	}
  	static class FastScanner {
  		BufferedReader br;
  		StringTokenizer st;
  
  		public FastScanner() {
  			try	{
  				br = new BufferedReader(new InputStreamReader(System.in));
  				st = new StringTokenizer(br.readLine());
  			} catch (Exception e){e.printStackTrace();}
  		}
  
  		public String next() {
  			if (st.hasMoreTokens())	return st.nextToken();
  			try {st = new StringTokenizer(br.readLine());}
  			catch (Exception e) {e.printStackTrace();}
  			return st.nextToken();
  		}
  
  		public int nextInt() {return Integer.parseInt(next());}
  
  		public long nextLong() {return Long.parseLong(next());}
  
  		public double nextDouble() {return Double.parseDouble(next());}
  
  		public String nextLine() {
  			String line = "";
  			if(st.hasMoreTokens()) line = st.nextToken();
  			else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
  			while(st.hasMoreTokens()) line += " "+st.nextToken();
  			return line;
  		}
  	}
  }
  /*
  break case
  1
  5 2
  1 2 -1
  2 3 -1
  3 4 1
  4 5 -1
  1 4 0
  3 5 1
   */