#P9137. Static Query on Tree

Static Query on Tree

Source: Super League of Chinese College Students Algorithm Design 2022, Contest 2 by HDU.

Problem Description

In country X, there are nn cities and n1n - 1 one-way roads, and all city can reach city 1. One query will give 3 sets of cities A, B, C. Alice will choose a city x in set A, choose a city z in set C, and walk from x to z (if x can reach z). Bob will choose a city y in set B, and walk from y to z (if y can reach z). How many cities can possibly be the city where Alice and Bob meet each other?

In other words, how many cities can be reached from at least one city in set A, and can be reached from at least one city in set B, and can reach at least one city in set C?

There are TT test cases, and each case has qq queries.

Input

First line is one integer TT, indicating TT test cases. In each case:

First line is 2 integers n,qn, q, indicating nn cities and qq queries.

Next line is n1n - 1 integers r1,r2,,rn1r_1, r_2, \ldots, r_{n-1}, the ii-th integer indicates the road from city i+1i + 1 to city rir_i.

Next is qq queries, in each query:

First line is 3 integer A,B,C|A|,|B|,|C|, indicating the size of set A, B, C.

Next line is A|A| integers, indicating the set AA.

Next line is B|B| integers, indicating the set BB.

Next line is C|C| integers, indicating the set CC.

1T20,1 \le T \le 20, 1n,q,A,B,C2×105,1\le n, q, |A|, |B|, |C| \le 2\times 10^5, for all cases n2×105,\sum n\le 2\times 10^5, q2×105,\sum q\le 2\times 10^5, for all queries in all cases A+B+C2×105\sum |A|+\sum |B|+\sum|C|\le 2\times 10^5.

Output

In each case, print qq integers, one integer per line, ii-th integer indicates the answer of ii-th query.

Sample

1
7 3
1 1 2 2 3 3
2 1 1
1 2
4
1
4 4 3
4 5 6 7
4 5 6 7
2 4 6
2 1 1
4 5
6
1
2
4
1

For the firstquery, city 1,21, 2.

(11 can be reached from 11 (A), 44 (B), can reach 11(C))

(22 can be reached from 22 (A), 44 (B), can reach 11(C))

For the second query, city 2,4,5,62, 4, 5, 6.

For the third query, only city 11.