#P7471. [2017年杭电多校]Logical Chain

[2017年杭电多校]Logical Chain

Logical Chain

Problem Description

Every time you come across a problem you've never seen before, haven't you thought of something that is familiar to you? If so, you might think of something else, then more and more things will come to your mind. This is what is called ''Logical Chain''. Lu Xun's work also described such interesting phenomenon.

Assume there are nn things, labeled by 1,2,...,n1,2,...,n. Little Q's mind can be expressed by a n×nn\times n matrix gg. If he can think of jj when he comes across ii, then gi,jg_{i,j} is 11, otherwise it's 00. For two different things uu and vv, if uu can lead to vv directly or indirectly and vv can also lead to uu directly or indirectly, then the pair (u,v)(u,v) is called a ''Looping Pair''. Little Q's mind changes all the time. On ii-th day, there are kik_i positions (u,v)(u,v) in matrix gg flipped(00 to 11 and 11 to 00). Please write a program to figure out the number of ''Looping Pairs'' after each day's all changes. Note : (u,v)(u,v) and (v,u)(v,u) should not be counted twice.

Input

The first line of the input contains an integer T(1T15)T(1\leq T\leq15), denoting the number of test cases. In each test case, there are 22 integers n,m(1n250,1m25000)n,m(1\leq n\leq 250,1\leq m\leq 25000) in the first line, denoting the number of things and days.\par For the next nn lines, each line contains nn integers $g_{i,1},g_{i,2},...,g_{i,n}(0\leq g_{i,j}\leq 1,g_{i,i}=0)$, denoting the matrix gg. For the next mm parts, there is an integer ki(1ki10)k_i(1\leq k_i\leq 10) in the first line, denoting the number of changes happened on that day. For the next kik_i lines, each line contains 22 integers u,v(1u,vn,uv)u,v(1\leq u,v\leq n,u\neq v), denoting a changed position in gg. It is guaranteed that each position will be changed at most once per day.

Output

For each day, print a single line containing an integer, denoting the number of ''Looping Pairs'' after that day's all changes.

Sample Input

1

4 2

0010

1000

0000

0000

3

1 4

3 2

1 2

2

4 3

2 3

Sample Output

3

6

Source

2017 Multi-University Training Contest - Team 4

https://acm.hdu.edu.cn/showproblem.php?pid=6072