import sys sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = 18446744073709551615 # inf = 4294967295 # md = 10**9+7 # md = 998244353 md = 10**8+9 def dot(aa, bb): h = len(aa) w = len(bb[0]) res = [[0]*w for _ in range(h)] for i, row in enumerate(aa): for j, col in enumerate(zip(*bb)): v = -inf for a, b in zip(row, col): cur = a+b if cur > v: v = cur res[i][j] = v return res def matpow(mat, e): n = len(mat) res = [[0 if i == j else -inf for j in range(n)] for i in range(n)] while e: if e & 1: res = dot(res, mat) mat = dot(mat, mat) e >>= 1 return res cc = LI1() kk = LI() n = II() ee = [[[-inf]*16 for _ in range(16)] for _ in range(26)] for f, c in enumerate(cc): ee[f][c][c] = 0 for _ in range(n): s, a, b, e = SI().split() a = int1(a) b = int1(b) e = int(e) for c in s: c = ord(c)-65 ee[c][a][b] = ee[c][b][a] = max(ee[c][a][b], e) # p2D(ee) ss = [0]*16 for f in range(26): c = cc[f] eef = ee[f] eef = matpow(eef, kk[f]) # p2D(eef) for i, e in enumerate(eef[c]): if e == -inf: ss[i] = -inf else: ss[i] += e ans = max(ss) if ans == -inf: ans = "Impossible" print(ans)