import java.io.*;
import java.util.*;
import java.math.*;
//3/25/12 12:33 PM
//by Abrackadabra
public class D {
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
int n, m;
ArrayList<TreeSet<Integer>> map = new ArrayList<TreeSet<Integer>>();
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
;
init();
if (debugMode) {
con.println("Start");
con.println("Console:");
}
solve();
finishTime
= System.
nanoTime(); finishMem
= Runtime.
getRuntime().
totalMemory(); out.flush();
if (debugMode) {
int maxSymbols = 1000;
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("...");
}
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
) { con.println("Tick at " + (tempTime - startTime) / 1000000000 + " sec");
con.print(" ");
return true;
}
return false;
}
static boolean debugMode = false;
if (debugMode && IOMode != 3) {
} else
switch (IOMode) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
}
while (in == null || !in.hasMoreTokens()) {
if (line == null) return false;
}
return true;
}
return hasMoreTokens() ? in.nextToken() : null;
}
return Integer.
parseInt(nextString
()); }
return Long.
parseLong(nextString
()); }
return Double.
parseDouble(nextString
()); }
}