import java.io.*;
import java.util.*;
import java.math.*;

//3/25/12 12:33 PM
//by Abrackadabra

public class D {
    public static void main(String[] args) throws IOException {
        if (args.length > 0 && args[0].equals("Abra")) debugMode = true;
        new D().run();
    }

    int IOMode = 0; //0 - consoleIO, 1 - <taskName>.in/out, 2 - input.txt/output.txt, 3 - test case generator
    String taskName = "";
    
    int n, m;
    ArrayList<TreeSet<Integer>> map = new ArrayList<TreeSet<Integer>>();

    void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        for (int i = 0; i < n; i++) {
            map.add(new TreeSet<Integer>());
        }
        for (int i = 0; i < m; i++) {
            int a = nextInt() - 1, b = nextInt() - 1;
            map.get(a).add(b);
            map.get(b).add(a);
        }
        from = new long[n][n];
        depth = new long[n][n];
        sum1 = new long[n][n];
        sum2 = new long[n][n];
        for (int i = 0; i < n; i++)
            bfs(i);
        int R = nextInt();
        HashMap<Long, Integer> nums = new HashMap<Long, Integer>();
        for (int i = 0; i < R; i++) {
            int k = nextInt();
            int[] p = new int[k];
            for (int j = 0; j < k; j++)
                p[j] = nextInt() - 1;     /*
            Stack<Integer> path = new Stack<Integer>();
            path.add(p[k - 1]);
            for (int j = k - 2; j >= 0; j--) {
                int t = p[j + 1];
                while (t != p[j]) {
                    t = from[p[j]][t];
                    path.add(t);
                }
            }
            int sum = 0, c = 1;
            while (!path.empty()) {
                int t = path.pop() + 1;
                sum += t * c;
                c++;
            }              */
            long ssum = 0;
            int cd = 1;
            for (int j = 0; j < k - 1; j++) {
                ssum += sum1[p[j]][p[j + 1]] + (cd - 1) * sum2[p[j]][p[j + 1]];
                cd += depth[p[j]][p[j + 1]] - 1;
                ssum -= cd * (p[j + 1] + 1);
            }
            ssum += cd * (p[k - 1] + 1);
            if (!nums.containsKey(ssum)) {
                nums.put(ssum, 1);
            } else {
                nums.put(ssum, nums.get(ssum) + 1);
            }
            int q = nums.get(ssum);
            out.print(ssum);
            if (q > 1) out.print("#" + q);
            out.println();
        }
    }

    long[][] from;
    long[][] depth;
    long[][] sum1;
    long[][] sum2;
    
    void bfs(int x) {
        boolean[] visited = new boolean[n];
        visited[x] = true;
        from[x][x] = -1l;
        depth[x][x] = 1l;
        sum1[x][x] = (x + 1l);
        sum2[x][x] = (x + 1l);
        Queue<Integer> queue = new LinkedList<Integer>();
        queue.add(x);
        while (queue.size() > 0) {
            int a = queue.poll();
            for (int b : map.get(a)) {
                if (!visited[b]) {
                    visited[b] = true;
                    queue.add(b);
                    from[x][b] = a;
                    depth[x][b] = depth[x][a] + 1;
                    sum1[x][b] = sum1[x][a] + depth[x][b] * (b + 1);
                    sum2[x][b] = sum2[x][a] + b + 1;
                }
            }
        }
    }

    long startTime = System.nanoTime(), tempTime = startTime, finishTime = startTime;
    long startMem = Runtime.getRuntime().totalMemory(), finishMem = startMem;

    void run() throws IOException {
        init();
        if (debugMode) {
            con.println("Start");
            con.println("Console:");
        }
        solve();
        finishTime = System.nanoTime();
        finishMem = Runtime.getRuntime().totalMemory();
        out.flush();
        if (debugMode) {
            int maxSymbols = 1000;
            BufferedReader tbr = new BufferedReader(new FileReader("input.txt"));
            char[] a = new char[maxSymbols];
            tbr.read(a);
            if (a[0] != 0) {
                con.println("File input");
                con.println(a);
                if (a[maxSymbols - 1] != 0) con.println("...");
            }
            tbr = new BufferedReader(new FileReader("output.txt"));
            a = new char[maxSymbols];
            tbr.read(a);
            if (a[0] != 0) {
                con.println("File output");
                con.println(a);
                if (a[maxSymbols - 1] != 0) con.println("...");
            }
            con.println("Execution time: " + (finishTime - startTime) / 1000000000.0 + " sec");
            con.println("Used memory:    " + (finishMem - startMem) + " bytes");
            con.println("Total memory:   " + Runtime.getRuntime().totalMemory() + " bytes");
        }
    }

    boolean tick(double x) {
        if (System.nanoTime() - tempTime > x * 1e9) {
            tempTime = System.nanoTime();
            con.println("Tick at " + (tempTime - startTime) / 1000000000 + " sec");
            con.print("   ");
            return true;
        }
        return false;
    }

    static boolean debugMode = false;
    PrintStream con = System.out;

    void init() throws IOException {
        if (debugMode && IOMode != 3) {
            br = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter(new FileWriter("output.txt"));
        } else
            switch (IOMode) {
                case 0:
                    br = new BufferedReader(new InputStreamReader(System.in));
                    out = new PrintWriter(System.out);
                    break;
                case 1:
                    br = new BufferedReader(new FileReader(taskName + ".in"));
                    out = new PrintWriter(new FileWriter(taskName + ".out"));
                    break;
                case 2:
                    br = new BufferedReader(new FileReader("input.txt"));
                    out = new PrintWriter(new FileWriter("output.txt"));
                    break;
                case 3:
                    out = new PrintWriter(new FileWriter("input.txt"));
                    break;
            }
    }

    BufferedReader br;
    PrintWriter out;
    StringTokenizer in;

    boolean hasMoreTokens() throws IOException {
        while (in == null || !in.hasMoreTokens()) {
            String line = br.readLine();
            if (line == null) return false;
            in = new StringTokenizer(line);
        }
        return true;
    }

    String nextString() throws IOException {
        return hasMoreTokens() ? in.nextToken() : null;
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextString());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextString());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextString());
    }
}