import java.util.*;

public class medo {
    static int[] comp;
    static boolean[] visited;
    static List<Integer>[] adj;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();

        adj = new ArrayList[N + 1];
        for (int i = 1; i <= N; i++) adj[i] = new ArrayList<>();

        for (int i = 0; i < M; i++) {
            int u = sc.nextInt(), v = sc.nextInt();
            adj[u].add(v);
            adj[v].add(u);
        }

        comp = new int[N + 1];
        visited = new boolean[N + 1];

        int numComp = 0;
        for (int i = 1; i <= N; i++) {
            if (!visited[i]) {
                // Iterative DFS to avoid StackOverflowError
                Deque<Integer> stack = new ArrayDeque<>();
                stack.push(i);
                visited[i] = true;
                comp[i] = numComp;
                while (!stack.isEmpty()) {
                    int u = stack.pop();
                    for (int v : adj[u]) {
                        if (!visited[v]) {
                            visited[v] = true;
                            comp[v] = numComp;
                            stack.push(v);
                        }
                    }
                }
                numComp++;
            }
        }

        int Q = sc.nextInt();
        while (Q-- > 0) {
            int k = sc.nextInt();
            int[] route = new int[k];
            for (int i = 0; i < k; i++) route[i] = sc.nextInt();

            int flights = 0;
            for (int i = 0; i + 1 < k; i++) {
                if (comp[route[i]] != comp[route[i + 1]]) {
                    flights++;
                }
            }
            System.out.println(flights);
        }
    }
}
