#Lutece1450. Magic Number

Magic Number

Migrated from Lutece 1450 Magic Number

All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.

Description

Illya has nn magic numbers. The ithi_{th} number aia_i can be represented by a binary with the length of ii. Combining these 010-1 strings gets a long string ss with the length of n(n+1)2\frac{n(n+1)}{2}. The 010-1 strings from si(i1)2s_{\frac{i(i-1)}{2}} to si(i+1)21s_{\frac{i(i+1)}{2}-1} is the binary of aia_i from low to high. One day, Rin input Illya’s magic numbers into a program to create nn new magic numbers. The ithi_{th} new number is bib_i. Here is the code of program in C++:

for(int i = 1; i <= n; i++)
	b[i] = flag[i] = 0;
	
for(int i = 1; i <= n; i++){
	for(int j = 1; j <= n; j++){
		if( (1 << (j - 1) & a[i]) > 0){
			if(!flag[i])
				b[i] = b[j], flag[i] = 1;
			else b[i] &= b[j];
		}
	}
	b[i] ^= 1 << (i - 1);
}

After that, Rin leave qq questions. The ithi_{th} question is a number cic_i, which can be represented by a binary with the length of lenilen_i. Combining these 010-1 strings gets a long string tt with the length of LqL_q (Lk=i=1kleniL_k = \sum_{i=1}^{k}len_i). The 010-1 strings from tLi1t_{L_{i-1}} to tLi1t_{L_i-1} is the binary of cic_i from low to high. For each question, Rin requires Illya to calculate number did_i.

for(int i = 1; i <= n; i++){
	d[i] = 0;
	for(int j = 1; j <= len[i]; j++)
		if(j <= n && (1 << (j - 1) & c[i]) > 0)
			d[i] |= b[j];
}

The answer of ithi_{th} question is the number of ones in the binary of did_i.

Input

There are multiple test cases. the first line contains a single integers TT, denoting the number of test cases. For each case, the first line contains two integers nn and mm, denoting the number of magic numbers and the number of ones in a long string ss. The second line contains mm integers, the ithi_{th} integer p1ip1_i denotes sp1is_{p1_i} = 1. The others are equal to 0. The third line contains two integer qq and rr, denoting the number of questions and the number of ones in a long string tt. The forth line contains qq integers, the ithi_{th} integer lenilen_i denotes the length of binary of the ithi_{th} question cic_i. The fifth line contains rr integers, the ithi_{th} integer p2ip2_i denotes tp2it_{p2_i} = 1. The others are equal to 0.

1T201 \leqslant T \leqslant 20 1n,q50001 \leqslant n,q \leqslant 5000 1m,r1061 \leqslant m,r \leqslant 10^6 0Lq1090 \leqslant L_q \leqslant 10^9 0p1i<n(n+1)20 \leqslant p1_i < \frac{n(n+1)}{2} 0p2i<Lq0 \leqslant p2_i < L_q

Output

For each case, first output "Case #x:". Each question should print one line: the ithi_{th} line of these denotes the answer of ithi_{th} question. There is no blank line between two cases.

Samples

1
3 4
0 1 4 5
3 6
2 3 3
0 1 2 4 6 7
Case #1:
2
3
3

Resources

2016 Multi-University Training Contest 6