#P7466. [2017年杭电多校]Big Integer

[2017年杭电多校]Big Integer

Big Integer

Problem Description

Little Q likes positive big integers in base kk, but not all big integers. He doesn't like integers with zeroes, including leading zeroes. He is even particular with the occurrence of each digit. Formally it can be described as a matrix g1..k1,0..ng_{1..k-1,0..n}, for every digit ii from 11 to k1k-1, he doesn't like integers having exactly jj-digit ii when gi,j=0g_{i,j}=0. He also can't accept any digit appearing more than nn times.

Little Q's taste changes every day. There are mm days in total, on ii-th day gui,vig_{u_i,v_i} flipped(00 to 11 and 11 to 00). Let cnt(i)cnt(i) denotes the number of big integers Little Q likes after ii-th day's change, where cnt(0)cnt(0) denotes the answer before all changes. Your task is to calculate the following thing :

Input

The first line of the input contains an integer T(1T5)T(1\leq T\leq5), denoting the number of test cases. In each test case, there are 33 integers $k,n,m(3\leq k\leq 10,1\leq n\leq 14000,1\leq m\leq 200)$ in the first line, denoting the base, the upper limit and the number of days. For the next k1k-1 lines, each line contains n+1n+1 integers gi,0,gi,1,...,gi,n(0gi,j1)g_{i,0},g_{i,1},...,g_{i,n}(0\leq g_{i,j}\leq 1), denoting the matrix gg. For the next mm lines, each line contains 22 integers ui,vi(1uik1,0vin)u_i,v_i(1\leq u_i\leq k-1,0\leq v_i\leq n), denoting a changed position in gg.

Output

For each test case, print a single line containing an integer, denoting the answer.

Sample Input

1

3 2 2

101

010

1 1

1 2

Sample Output

13

Hint

cnt(0)=4 : 112,121,211,2. cnt(1)=6 : 112,121,211,2,12,21. cnt(2)=3 : 2,12,21.

Source

2017 Multi-University Training Contest - Team 4

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