QOJ.ac

QOJ

时间限制: 10 s 内存限制: 512 MB 总分: 100 难度: [显示]

#2583. Strategy Game

统计

With the provincial selection approaching, Xiao Q, feeling unmotivated to study, encourages Xiao C to slack off with him by playing a strategy game.

The map of this strategy game consists of $n$ cities and $m$ bidirectional roads connecting these cities, such that it is always possible to travel from any city to any other city along the roads. Xiao C has already occupied at least two of these cities. Xiao Q can destroy one city not occupied by Xiao C, along with all roads connected to that city. If, after destroying this city, there exist two cities $u$ and $v$ occupied by Xiao C such that it is impossible to travel from $u$ to $v$ along the remaining roads, then Xiao Q wins the game.

Xiao Q and Xiao C play $q$ rounds of the game. In each round, a set $S$ of cities occupied by Xiao C is given. You need to help Xiao Q count how many cities he can destroy to win that round.

Input

The first line contains a positive integer $T$, representing the number of test cases.

For each test case:

The first line contains two integers $n$ and $m$, representing the number of cities and roads in the map.

The next $m$ lines each contain two integers $u$ and $v$ ($1 \le u < v \le n$), representing a road between city $u$ and city $v$. There may be multiple roads between the same pair of cities.

The $(m + 1)$-th line contains an integer $q$, representing the number of game rounds.

The next $q$ lines each start with an integer $|S|$ ($2 \le |S| \le n$), representing the number of cities occupied by Xiao C, followed by $|S|$ integers $s_1, s_2, \dots, s_{|S|}$ ($1 \le s_1 < s_2 < \dots < s_{|S|} \le n$), representing the cities occupied by Xiao C.

Output

For each game round, output a single line containing an integer representing the number of cities Xiao Q can destroy to win that round.

Examples

Input 1

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

Output 1

0
1
3
0
1
2
3

Note

  • $1 \le T \le 10$
  • $2 \le n \le 10^5$ and $n - 1 \le m \le 2 \times 10^5$
  • $1 \le q \le 10^5$
  • For each test case, $\sum |S| \le 2 \times 10^5$

Subtask 1 (30 points): For each test case, $\sum |S| \le 20$.

Subtask 2 (45 points): For each query, $|S| = 2$.

Subtask 3 (25 points): No additional restrictions.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.