// Arup Guha
// 7/24/2013
// Solution to 2013 SI@UCF Contest Question: Way Points

import java.util.*;
import java.io.*;

public class waypoints {
	
	public final static int MAX = 1000000;
	
	public static void main(String[] args) throws Exception {
		
		Scanner stdin = new Scanner(new File("waypoints.in"));
		int numCases = stdin.nextInt();
		
		// Go through each case.
		for (int loop=1; loop<=numCases; loop++) {
			
			// Read in the graph.
			int n = stdin.nextInt();
			int m = stdin.nextInt();
			int[][] adj = new int[n][n];
			for (int i=0; i<n; i++) {
				Arrays.fill(adj[i], MAX);
				adj[i][i] = 0;
			}
			
			for (int i=0; i<m; i++) {
				int v1 = stdin.nextInt();
				int v2 = stdin.nextInt();
				adj[v1][v2] = 1;
			}
			
			// Run Floyd's for each lookup of all distances.
			for (int k=0; k<n; k++)
				for (int i=0; i<n; i++)
					for (int j=0; j<n; j++)
						adj[i][j] = Math.min(adj[i][j], adj[i][k]+adj[k][j]);
						
			// Case header.
			System.out.println("Map #"+loop+":");
			
			// Go through each race for this map.
			int races = stdin.nextInt();
			for (int i=0; i<races; i++) {
				
				int pts = stdin.nextInt();
				int prev = stdin.nextInt();
				int sum = 0;
				
				// Add up all the shortest paths between the way points.
				for (int j=0; j<pts-1; j++) {
					int next = stdin.nextInt();
					sum += adj[prev][next];
					prev = next;
				}
				
				// Here is our answer.
				if (sum >= MAX)
					System.out.println("-1");
				else
					System.out.println(sum);
			}
			
			// Separate out different maps.
			System.out.println();
		}
	}
}