import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { static List> graph; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); Road[] roads = new Road[m]; for (int i = 0; i < m; i++) { roads[i] = new Road(sc.nextInt() - 1, sc.nextInt() - 1); } int[] colors = new int[n]; for (int i = 0; i < n; i++) { colors[i] = sc.nextInt() - 1; } int[] values = new int[10]; for (int i = 0; i < 10; i++) { values[i] = sc.nextInt(); } int[] mask = new int[m]; for (int i = 0; i < m; i++) { mask[i] = (1 << colors[roads[i].left]) | (1 << colors[roads[i].right]); } int q = sc.nextInt(); Road[] queries = new Road[q]; for (int i = 0; i < q; i++) { queries[i] = new Road(sc.nextInt() - 1, sc.nextInt() - 1); } long[] ans = new long[q]; Arrays.fill(ans, Long.MAX_VALUE); UnionFindTree uft = new UnionFindTree(n); long[] dp = new long[1 << 10]; for (int i = 1; i < (1 << 10); i++) { final int d = i; int idx = IntStream.range(0, 10).filter(j -> (d & (1 << j)) > 0).findFirst().orElse(0); dp[d] = dp[d ^ (1 << idx)] + values[idx]; uft.clear(); for (int j = 0; j < m; j++) { if ((d & (mask[j])) != mask[j]) { continue; } uft.unite(roads[j]); } for (int j = 0; j < q; j++) { if (uft.same(queries[j])) { ans[j] = Math.min(ans[j], dp[d]); } } } System.out.println(Arrays.stream(ans).mapToObj(i -> String.valueOf(i == Long.MAX_VALUE ? -1 : i)).collect(Collectors.joining("\n"))); } static class Road { int left; int right; public Road(int left, int right) { this.left = left; this.right = right; } } static class UnionFindTree { int[] parents; public UnionFindTree(int size) { parents = new int[size]; } public void clear() { for (int i = 0; i < parents.length; i++) { parents[i] = i; } } public int find(int x) { if (parents[x] == x) { return x; } else { return parents[x] = find(parents[x]); } } public boolean same(int x, int y) { return find(x) == find(y); } public boolean same(Road r) { return same(r.left, r.right); } public void unite(int x, int y) { if (!same(x, y)) { parents[find(x)] = find(y); } } public void unite(Road r) { unite(r.left, r.right); } } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }