Coin Exchange

Find the fewest swaps along graph edges that place every black coin on a black vertex and every white coin on a white vertex.

Medium7Shortest pathGraphDynamic programmingNo attempts yetTime limit8sMemory limit256 MB

Problem

You are given an undirected graph G=(V,E)G = (V, E) with vertex set VV and edge set EE. The graph is connected, so at least one path runs between every pair of vertices. Each vertex is black or white, and every vertex holds exactly one coin. Each coin is black or white.

A coin exchange takes two vertices joined by an edge and swaps the two coins sitting on them.

Figure 1. Examples of a coin exchange (2 with 5, 5 with 6, 1 with 5). The color of a square is the color of a coin.

You want every black coin to end up on a black vertex and every white coin on a white vertex. Find the minimum number of coin exchanges that achieves this.

Input

The first line contains the number of test cases TT.

The first line of each test case contains the number of vertices nn and the number of edges mm (1n5001 \le n \le 500, n1mn(n1)/2n-1 \le m \le n(n-1)/2). Vertices are numbered 1 to nn. Each of the next mm lines contains two vertices xx and yy joined by an edge (1x<yn1 \le x < y \le n). The next line contains nn integers, each 0 or 1, where the ii-th integer is the color of vertex ii. 0 is black and 1 is white. The last line contains nn integers, each 0 or 1, where the ii-th integer is the color of the coin sitting on vertex ii.

Only inputs where every coin can be matched to a vertex of the same color are given.

Output

For each test case, print on its own line the minimum number of coin exchanges needed to put every coin on a vertex of its own color.