[Solution] Kirill and Company solution codeforces

Table of Contents

Kirill and Company solution codeforces

Kirill and Company solution codeforces – Kirill lives on a connected undirected graph of 𝑛n vertices and 𝑚m edges at vertex 11. One fine evening he gathered 𝑓f friends, the 𝑖i-th friend lives at the vertex 𝑖hi. So all friends are now in the vertex 11, the 𝑖i-th friend must get to his home to the vertex 𝑖hi.

The evening is about to end and it is time to leave. It turned out that 𝑘k (𝑘6k≤6) of his friends have no cars, and they would have to walk if no one gives them a ride. One friend with a car can give a ride to any number of friends without cars, but only if he can give them a ride by driving along one of the shortest paths to his house.

[Solution] Kirill and Company solution codeforces

For example, in the graph below, a friend from vertex 𝑖=5hi=5 can give a ride to friends from the following sets of vertices: [2,3][2,3][2,4][2,4][2][2][3][3][4][4], but can’t give a ride to friend from vertex 66 or a set [3,4][3,4].

Kirill and Company solution codeforcesThe vertices where friends without cars live are highlighted in green, and with cars — in red.
Kirill wants as few friends as possible to have to walk. Help him find the minimum possible number.

[Solution] Kirill and Company solution codeforces

The first line of input data contains an integer 𝑡t (1𝑡1031≤t≤103) — the number of test cases in the test.

The first line of the test case contains two integers 𝑛n and 𝑚m (2𝑛1042≤n≤104𝑛1𝑚𝑚𝑖𝑛(104,n−1≤m≤min(104,𝑛(𝑛1)2n⋅(n−1)2))) — the number of vertices and edges, respectively.

The next 𝑚m lines of the test case contain a description of the edges, two integers each 𝑢u and 𝑣v (1𝑢,𝑣𝑛1≤u,v≤n𝑢𝑣u≠v) — indexes of vertices connected by an edge. It is guaranteed that there is at most one edge between any pair of vertices (i.e. no multiple edges in the graph).

Then follows line containing the number 𝑓f (1𝑓1041≤f≤104) — the number of Kirill’s friends.

The next line of the test case contains 𝑓f integers: 1,2,,𝑓h1,h2,…,hf (2𝑖𝑛2≤hi≤n) — the vertices in which they live. Some vertices may be repeated.

The next line of the set contains the number 𝑘k (1𝑘𝑚𝑖𝑛(6,𝑓)1≤k≤min(6,f)) — the number of friends without cars.

The last line of each test case contains 𝑘k integers: 𝑝1,𝑝2,,𝑝𝑘p1,p2,…,pk (1𝑝𝑖𝑓1≤pi≤f𝑝𝑖<𝑝𝑖+1pi<pi+1) — indexes of friends without cars.

It is guaranteed that the sum of 𝑛n over all cases does not exceed 104104, as well as the sums of 𝑚m and 𝑓f.

[Solution] Kirill and Company solution codeforces

Output 𝑡t lines, each of which contains the answer to the corresponding test case. As an answer, output a single integer — the minimum possible number of friends who will have to walk.

Examples

input

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

output

Copy
2
1
1

[Solution] Kirill and Company solution codeforces

Copy
3
2 1
1 2
3
2 2 2
3
1 2 3
3 3
1 2
1 3
2 3
4
2 2 2 3
3
1 2 4
4 4
3 1
3 2
1 4
2 4
5
3 2 2 4 2
3
1 3 4

output

Copy
3
1
0

[Solution] Kirill and Company solution codeforces

The first test case of the first example is explained in the statement.

In the second test case of the first example, two friends with cars live at vertex 55, one can give a ride to friends from vertices 22 and 33, and the second from vertex 44, only a friend from vertex 66 will have to walk.

Leave a Comment