namespace AtCoder; #nullable enable using System.Numerics; class UnionFind { (int parent, int size)[] Verticals { get; set; } public UnionFind(int verticals) { Verticals = new (int, int)[verticals]; for (int i = 0; i < verticals; i++) Verticals[i] = (i, 1); } public int Union(int x, int y) { (x, y) = (Find(x), Find(y)); if (Size(x) < Size(y)) (x, y) = (y, x); if (x == y) return x; Verticals[x].size += Verticals[y].size; Verticals[y].parent = x; return x; } public int Find(int x) => Parent(x).parent; public int Size(int x) => Parent(x).size; (int parent, int size) Parent(int x) { var parentId = Verticals[x].parent; if (parentId == x) return Verticals[x]; var parent = Parent(parentId); Verticals[x].parent = parent.parent; return parent; } } static class Extensions { public static T[] Repeat(this int time, Func F) => Enumerable.Range(0, time).Select(_ => F()).ToArray(); public static int ToFlag(this int i) { if (i < 0 || 32 <= i) throw new IndexOutOfRangeException(); return 1 << i; } public static bool OfFlag(this int value, int i) => (value & ToFlag(i)) > 0; public static int WithFlag(this int value, int i, bool f) => f ? (value | ToFlag(i)) : (value & ~ToFlag(i)); } class AtCoder { object? Solve() { var n = Int(); var m = Int(); var edges = m.Repeat(() => (Int() - 1, Int() - 1)); var cz = n.Repeat(() => Int() - 1); var wz = 10.Repeat(Int); var q = Int(); var ans = new long[q]; var b = 1 << 10; var ufz = b.Repeat(() => new UnionFind(n)); var csumz = new long[b]; for (var i = 0; i < b; i++) { var csum = 0L; for (var j = 0; j < 10; j++) if (i.OfFlag(j)) csum += wz[j]; csumz[i] = csum; } for (var i = 0; i < b; i++) { foreach (var (u, v) in edges) { if (!i.OfFlag(cz[u]) || !i.OfFlag(cz[v])) continue; ufz[i].Union(u, v); } } for (var t = 0; t < q; t++) { var u = Int() - 1; var v = Int() - 1; var minc = long.MaxValue; for (var i = 0; i < b; i++) { if (ufz[i].Find(u) == ufz[i].Find(v)) minc = Math.Min(minc, csumz[i]); } if (minc == long.MaxValue) minc = -1; ans[t] = minc; } Out(ans); return null; } public static void Main() => new AtCoder().Run(); public void Run() { var res = Solve(); if (res != null) { if (res is bool yes) res = yes ? "Yes" : "No"; sw.WriteLine(res); } sw.Flush(); } string[] input = Array.Empty(); int iter = 0; readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false }; #pragma warning disable IDE0051 string String() { while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Split(' '), 0); return input[iter++]; } T Input() where T : IParsable => T.Parse(String(), null); int Int() => Input(); void Out(object? x, string? separator = null) { separator ??= Environment.NewLine; if (x is System.Collections.IEnumerable obj and not string) { var firstLine = true; foreach (var item in obj) { if (!firstLine) sw.Write(separator); firstLine = false; sw.Write(item); } } else sw.Write(x); sw.WriteLine(); } #pragma warning restore IDE0051 }